zoukankan      html  css  js  c++  java
  • Jedis Client的使用以及序列化

    JedisPool pool = new JedisPool(poolConfig, IP, PORT, timeout);
    public String set(String key,String value) {
    Jedis jedis = null;
    boolean success = true;
    try {
    jedis = this.pool.getResource();
    return jedis.set(key, value);
    }catch (JedisException e) {
    success  = false;
    if(jedis != null){
    //jedis异常,销毁
    pool.returnBrokenResource(jedis);
    }
    throw e;
    }catch (Exception e) {
    system.out.println("jedis  exception");
    }finally{
    if(success && jedis != null){
    //需要还回给pool
    this.pool.returnResource(jedis);
    }
    }
    }

    ------------------------jedis序列化RedisSerializeUtil --------------

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    public class RedisSerializeUtil {
    //序列化
    public static byte[] serialize(Object object){
    ObjectOutputStream objectOutputStream = null;
       ByteArrayOutputStream byteArrayOutputStream = null;
       try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        byte[] bytes = byteArrayOutputStream.toByteArray();
                return bytes;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    // 反序列化
    public static Object deSeialize(byte[] bytes) {
    ByteArrayInputStream byteArrayOutputStream = null;
    try {
    byteArrayOutputStream = new ByteArrayInputStream(bytes);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayOutputStream);
    return objectInputStream.readObject();
    } catch (Exception e) {
    system.out.println("deserialize exception");
    
    }
    return null;
    }
    public static void main(String[] args) {
    String str = "tobytes";
    System.out.print(RedisSerializeUtil.deSeialize(RedisSerializeUtil.serialize(str)));
    }
    }


  • 相关阅读:
    简历的快速复制
    使用stringstream对象简化类型转换
    猴子吃桃
    new和delete运算符
    绘制正余弦曲线
    计算学生的平均成绩
    判断是否为回文字符串
    统计各种字符个数
    验证用户名
    回溯法(挑战编程)
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3181813.html
Copyright © 2011-2022 走看看