代码笔记 | Redis的使用

 
public class RedisClient {
  
  public static Logger log = Logger.getLogger(RedisClient.class);
  /**
   * jedis 连接池
   */
  private static JedisPool jedisPool;
    
  @Resource
  public void setJedisPool(JedisPool jedisPool) {
    RedisClient.jedisPool = jedisPool;
  }
  
  /**
   * 释放连接对象
   * @Title:closeShardedJedis
   * @param jedis
   * @return void
   * @throws
   */
  public static void closeJedis(Jedis jedis) {
    if (null != jedis) {
      jedisPool.returnResource(jedis);
    }
  }
  
  /**
   * 释放异常的连接对象
   * @Title:closeShardedJedis
   * @param jedis
   * @return void
   * @throws
   */
  public static void closeBrokenJedis(Jedis jedis) {
    if (null != jedis) {
      jedisPool.returnBrokenResource(jedis);
    }
  }
  
  /**
   * 获取连接对象
   * @Title:getShardedJedis
   * @return ShardedJedis
   * @throws
   */
  public static Jedis getJedis() {
    return jedisPool.getResource();
  }
  
  /**
   * 根据redis的key找到对应的值
   * @param jediskey  redis键
   * @return //返回redis储存信息
   */
  public static String getJedisVal(String jediskey) {
    Jedis jedis = null;
    try {
      jedis = RedisClient.getJedis();
      String value = jedis.get(jediskey);
      return value;
    } catch (Exception e) {
      log.error("key:" + jediskey + "未能取到redis的值.");
      closeBrokenJedis(jedis);
      return null;
    } finally {
      closeJedis(jedis);
    }
  }
  
  /**
   * 存储redis的键值不设置时间
   * 
   * @param jediskey     redis键
   * @param jedisval  redis值
   */
  public static void setJedisVal(String jediskey, String jedisval) {
    setJedisVal(jediskey, jedisval, 0);
  }
  
  /**
   * 设置时间存储redis
   * @param jediskey  redis键
   * @param jedisval     redis值
   * @param seconds   时间(秒)
   */
  public static void setJedisVal(String jediskey, String jedisval, int seconds) {
    Jedis jedis = null;
    try {
      jedis = RedisClient.getJedis();
  
      if (seconds > 0) {
        jedis.setex(jediskey, seconds, jedisval);
      } else {
        jedis.set(jediskey, jedisval);
      }
    } catch (Exception e) {
      e.printStackTrace();
      closeBrokenJedis(jedis);
    } finally {
      closeJedis(jedis);
    }
  }
  
  /**
   * 根据redis键值删除redis
   * @param jediskey    键值
   */
  public static void delJedis(String jediskey) {
    Jedis jedis = null;
    try {
      jedis = RedisClient.getJedis();
      jedis.del(jediskey);
    } catch (Exception e) {
      e.printStackTrace();
      closeBrokenJedis(jedis);
    } finally {
      closeJedis(jedis);
    }
  }
    
  /**
   * 序列化
   * @author renzhenjiang 
   * @date 2014-9-30 下午04:09:03 
   * @param object
   * @return
   * @see [类、类#方法、类#成员]
   */
   public static byte[] serialize(Object object) {
     ObjectOutputStream oos = null;
     ByteArrayOutputStream baos = null;
     byte[] bytes = null;
     try {
  
       //序列化
       baos = new ByteArrayOutputStream();
       oos = new ObjectOutputStream(baos);
       oos.writeObject(object);
       bytes = baos.toByteArray();
     } catch (Exception e) {
       System.out.println("序列化运行异常");
     }
     return bytes;
  
  }
   /**
    * 反序列化
    * @author renzhenjiang 
    * @date 2014-9-30 下午04:10:06 
    * @param bytes
    * @return
    * @see [类、类#方法、类#成员]
    */
  public static Object unserialize(byte[] bytes) {
     ByteArrayInputStream bais = null;
     Object obj = null;
     try {
       //反序列化
       bais = new ByteArrayInputStream(bytes);
       ObjectInputStream ois = new ObjectInputStream(bais);
       obj = ois.readObject();
     } catch (Exception e) {
       System.out.println("反序列化异常");
     }
     return obj;
  }
  public static void main(String[] args) {
      
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxActive(666300);
    config.setMaxIdle(20);
    config.setMaxWait(1000);
    jedisPool = new JedisPool(config, "192.168.1.250", 6379);
    Jedis jedis = jedisPool.getResource();
      
    System.out.println("statistics_18310813001reg1_count === "+jedis.get("statistics_18310813001reg1"));
    System.out.println("statistics_18310813001reg2_count === "+jedis.get("statistics_18310813001reg2"));
    System.out.println("statistics_18310813001pwd1_count === "+jedis.get("statistics_18310813001pwd1"));
    System.out.println("statistics_18310813001pwd2_count === "+jedis.get("statistics_18310813001pwd2"));
    //jedis.del("statistics_18310813001reg1");
    //System.out.println("statistics_18310813001reg1_count === "+jedis.get("statistics_18310813001reg1"));
  }
    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值