Redis有很多数据结构:string、hash、list、set等,但是在实际来发中,我们往往保存的是对象在redis中,所以键值对更加常用。
Redis在保存对象时,需要满足序列化,由于java的原生序列化方式效率较低,故更加常用的是Protostuff这个序列化框架。 |
一、封装Protostuff工具类
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
public class SerializeUtil {
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
public static <T> byte[] serializer(T obj) {
Class<T> clazz = (Class<T>) obj.getClass();
Schema<T> schema = getSchema(clazz);
return ProtobufIOUtil.toByteArray(obj, schema, LinkedBuffer.allocate(256));
}
public static <T> T deSerializer(byte[] bytes, Class<T> clazz) {
T message;
try {
message = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
Schema<T> schema = getSchema(clazz);
ProtobufIOUtil.mergeFrom(bytes, message, schema);
return message;
}
@SuppressWarnings("unchecked")
public static <T> Schema<T> getSchema(Class<T> clazz) {
Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
if (schema == null) {
schema = RuntimeSchema.createFrom(clazz);
if (schema != null) {
cachedSchema.put(clazz, schema);
}
}
return schema;
}
}
二、封装RedisUtil工具类
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
private static String ADDR = "localhost";
private static int PORT = 6379;
private static String AUTH = "123456";
// 最大连接实例数,默认为8,-1表示无限制,如果pool已经分配了超过max_active个jedis实例,则此时pool为耗尽
private static int MAX_ACTIVE = 1024;
// 最大空闲实例,默认为8
private static int MAX_IDLE = 200;
// 最大等待连接时间,单位毫秒默认为-1,表示永不超时,超时会抛出JedisConnectionException
private static int MAX_WAIT = 10 * 1000;
private static int TIMEOUT = 10 * 1000;
// 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化连接池
*/
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
}
/**
* 获取jedis实例
*/
public synchronized static Jedis getJedis() {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
}
return null;
}
/**
* 是否资源
*
* @param jedis
*/
public static void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/**
* 保存对象(键-值)
*
* @param <T>
*
* @param key
* @param object
* @return
*/
public static <T> String setObject(String key, T object) {
Jedis jedis = getJedis();
try {
return jedis.set(key.getBytes(), SerializeUtil.serializer(object));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}
/**
* 带时间保存(键值)
*
* @param key
* @param object
* @param expiretime
* @return
*/
public static <T> String setObject(String key, T object, long expiretime) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.psetex(key.getBytes(), expiretime, SerializeUtil.serializer(object));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}
/**
* 读取对象(键-值)
*
* @param key
* @param clazz
* @return
*/
public static <T> T getObject(String key, Class<T> clazz) {
Jedis jedis = getJedis();
try {
byte[] bytes = jedis.get(key.getBytes());
T object = SerializeUtil.deSerializer(bytes, clazz);
return object;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}
/**
* 删除key
*/
public static Long deleteObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.del(key.getBytes());
} catch (Exception e) {
return null;
} finally {
close(jedis);
}
}
/**
* 检查存在
*
* @param key
* @return
*/
public static Boolean existsObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key.getBytes());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}
}
三、测试
User:
public class User{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
Main:
public class Main {
public static void main(String[] args) {
User user = new User();
user.setName("旭旭宝宝");
user.setAge(33);
RedisUtil.setObject("user", user);
User obj = RedisUtil.getObject("user", User.class);
System.out.println(obj);
System.out.println(RedisUtil.existsObject("user"));
}
}
测试结果:
对于序列化的详解,参照:
https://blog.csdn.net/qq_35890572/article/details/81630052