Redis:存储对象的两种方式(序列化和json字符串)

方式一:序列化操作

  1.  
    public class SerializeUtil {
  2.  
    /*
  3.  
    * 序列化
  4.  
    * */
  5.  
    public static byte[] serizlize(Object object){
  6.  
    ObjectOutputStream oos = null;
  7.  
    ByteArrayOutputStream baos = null;
  8.  
    try {
  9.  
    baos = new ByteArrayOutputStream();
  10.  
    oos = new ObjectOutputStream(baos);
  11.  
    oos.writeObject(object);
  12.  
    byte[] bytes = baos.toByteArray();
  13.  
    return bytes;
  14.  
    } catch (Exception e) {
  15.  
    e.printStackTrace();
  16.  
    }finally {
  17.  
    try {
  18.  
    if(baos != null){
  19.  
    baos.close();
  20.  
    }
  21.  
    if (oos != null) {
  22.  
    oos.close();
  23.  
    }
  24.  
    } catch (Exception e2) {
  25.  
    e2.printStackTrace();
  26.  
    }
  27.  
    }
  28.  
    return null;
  29.  
    }
  30.  
    /*
  31.  
    * 反序列化
  32.  
    * */
  33.  
    public static Object deserialize(byte[] bytes){
  34.  
    ByteArrayInputStream bais = null;
  35.  
    ObjectInputStream ois = null;
  36.  
    try{
  37.  
    bais = new ByteArrayInputStream(bytes);
  38.  
    ois = new ObjectInputStream(bais);
  39.  
    return ois.readObject();
  40.  
    }catch(Exception e){
  41.  
    e.printStackTrace();
  42.  
    }finally {
  43.  
    try {
  44.  
     
  45.  
    } catch (Exception e2) {
  46.  
    e2.printStackTrace();
  47.  
    }
  48.  
    }
  49.  
    return null;
  50.  
    }
  51.  
    }

获取jedis实例

  1.  
    public class RedisConnection {
  2.  
    private static String HOST = "127.0.0.1";
  3.  
    private static int PORT = 6379;
  4.  
    private static int MAX_ACTIVE = 1024;
  5.  
    private static int MAX_IDLE = 200;
  6.  
    private static int MAX_WAIT = 10000;
  7.  
     
  8.  
    private static JedisPool jedisPool = null;
  9.  
     
  10.  
    /*
  11.  
    * 初始化redis连接池
  12.  
    * */
  13.  
    private static void initPool(){
  14.  
    try {
  15.  
    JedisPoolConfig config = new JedisPoolConfig();
  16.  
    config.setMaxTotal(MAX_ACTIVE);//最大连接数
  17.  
    config.setMaxIdle(MAX_IDLE);//最大空闲连接数
  18.  
    config.setMaxWaitMillis(MAX_WAIT);//获取可用连接的最大等待时间
  19.  
     
  20.  
    jedisPool = new JedisPool(config, HOST, PORT);
  21.  
    } catch (Exception e) {
  22.  
    e.printStackTrace();
  23.  
    }
  24.  
    }
  25.  
     
  26.  
    /*
  27.  
    * 获取jedis实例
  28.  
    * */
  29.  
    public synchronized static Jedis getJedis() {
  30.  
    try {
  31.  
    if(jedisPool == null){
  32.  
    initPool();
  33.  
    }
  34.  
    Jedis jedis = jedisPool.getResource();
  35.  
    jedis.auth("redis");//密码
  36.  
    return jedis;
  37.  
    } catch (Exception e) {
  38.  
    e.printStackTrace();
  39.  
    return null;
  40.  
    }
  41.  
    }
  42.  
    }

redis操作

  1.  
    public class RedisOps {
  2.  
    public static void set(String key,String value){
  3.  
    Jedis jedis = RedisConnection.getJedis();
  4.  
    jedis.set(key, value);
  5.  
    jedis.close();
  6.  
    }
  7.  
    public static String get(String key){
  8.  
    Jedis jedis = RedisConnection.getJedis();
  9.  
    String value = jedis.get(key);
  10.  
    jedis.close();
  11.  
    return value;
  12.  
    }
  13.  
    public static void setObject(String key,Object object){
  14.  
    Jedis jedis = RedisConnection.getJedis();
  15.  
    jedis.set(key.getBytes(), SerializeUtil.serizlize(object));
  16.  
    jedis.close();
  17.  
    }
  18.  
    public static Object getObject(String key){
  19.  
    Jedis jedis = RedisConnection.getJedis();
  20.  
    byte[] bytes = jedis.get(key.getBytes());
  21.  
    jedis.close();
  22.  
    return SerializeUtil.deserialize(bytes);
  23.  
    }
  24.  
    }

实体类

  1.  
    public class User implements Serializable{
  2.  
    private static final long serialVersionUID = -3210884885630038713L;
  3.  
    private int id;
  4.  
    private String name;
  5.  
    public User(){
  6.  
     
  7.  
    }
  8.  
    public User(int id,String name){
  9.  
    this.id = id;
  10.  
    this.name = name;
  11.  
    }
  12.  
    //setter和getter方法
  13.  
    }

测试用例

  1.  
    public class RedisTest {
  2.  
     
  3.  
    @Test
  4.  
    public void testString(){
  5.  
    RedisOps.set("user:1", "sisu");
  6.  
    String user = RedisOps.get("user:1");
  7.  
    Assert.assertEquals("sisu", user);
  8.  
    }
  9.  
     
  10.  
    @Test
  11.  
    public void testObject(){
  12.  
    RedisOps.setObject("user:2",new User(2,"lumia"));
  13.  
    User user = (User)RedisOps.getObject("user:2");
  14.  
    Assert.assertEquals("lumia", user.getName());
  15.  
    }
  16.  
     
  17.  
    }

方式二:使用fastjson将对象转为json字符串后存储

  1.  
    public class RedisOps {
  2.  
    public static void setJsonString(String key,Object object){
  3.  
    Jedis jedis = RedisConnection.getJedis();
  4.  
    jedis.set(key, JSON.toJSONString(object));
  5.  
    jedis.close();
  6.  
    }
  7.  
    public static Object getJsonObject(String key,Class clazz){
  8.  
    Jedis jedis = RedisConnection.getJedis();
  9.  
    String value = jedis.get(key);
  10.  
    jedis.close();
  11.  
    return JSON.parseObject(value,clazz);
  12.  
    }
  13.  
    }

测试:

  1.  
    @Test
  2.  
    public void testObject2(){
  3.  
    RedisOps.setJsonString("user:3", new User(3,"xiaoming"));
  4.  
    User user = (User)RedisOps.getJsonObject("user:3",User.class);
  5.  
    Assert.assertEquals("xiaoming", user.getName());
  6.  
上一篇:Quartz将Job保存在数据库中所需表的说明


下一篇:MyBatis入门实例-包括实体类与数据库字段对应&CLOB字段处理