码迷,mamicode.com
首页 > 其他好文 > 详细

Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存

时间:2014-12-10 13:54:24      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   sp   for   java   strong   

源代码下载: http://download.csdn.net/detail/jiangtao_st/7623113

 

1、Maven配置

 

  1. <dependency>  
  2.     <groupId>redis.clients</groupId>  
  3.     <artifactId>jedis</artifactId>  
  4.     <version>2.5.0</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>com.alibaba</groupId>  
  8.     <artifactId>fastjson</artifactId>  
  9.     <version>1.1.41</version>  
  10. </dependency></span>  


2、Properties 配置文件

 

  redis.pool.maxActive=100

  redis.pool.maxIdle=20

  redis.pool.maxWait=3000

 

  redis.ip=localhost

  redis.port=6379

 

3、代码具体实现的Client

 

  1. /** 
  2.  *  
  3.  * <p> 
  4.  *  Redis客户端访问 
  5.  * </p> 
  6.  *  
  7.  * @author 卓轩 
  8.  * @创建时间:2014年7月11日 
  9.  * @version: V1.0 
  10.  */  
  11. public class RedisClient {  
  12.       
  13.     public  static  JedisPool jedisPool; // 池化管理jedis链接池  
  14.       
  15.     static {  
  16.           
  17.         //读取相关的配置  
  18.         ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");  
  19.         int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));  
  20.         int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));  
  21.         int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));  
  22.           
  23.         String ip = resourceBundle.getString("redis.ip");  
  24.         int port = Integer.parseInt(resourceBundle.getString("redis.port"));  
  25.           
  26.         JedisPoolConfig config = new JedisPoolConfig();    
  27.         //设置最大连接数  
  28.         config.setMaxTotal(maxActive);  
  29.         //设置最大空闲数  
  30.         config.setMaxIdle(maxIdle);  
  31.         //设置超时时间  
  32.         config.setMaxWaitMillis(maxWait);  
  33.           
  34.         //初始化连接池  
  35.         jedisPool = new JedisPool(config, ip, port);   
  36.     }  
  37.       
  38.     /** 
  39.      * 向缓存中设置字符串内容 
  40.      * @param key key 
  41.      * @param value value 
  42.      * @return 
  43.      * @throws Exception 
  44.      */  
  45.     public static boolean  set(String key,String value) throws Exception{  
  46.         Jedis jedis = null;  
  47.         try {  
  48.             jedis = jedisPool.getResource();  
  49.             jedis.set(key, value);  
  50.             return true;  
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.             return false;  
  54.         }finally{  
  55.             jedisPool.returnResource(jedis);  
  56.         }  
  57.     }  
  58.       
  59.     /** 
  60.      * 向缓存中设置对象 
  61.      * @param key  
  62.      * @param value 
  63.      * @return 
  64.      */  
  65.     public static boolean  set(String key,Object value){  
  66.         Jedis jedis = null;  
  67.         try {  
  68.             String objectJson = JSON.toJSONString(value);  
  69.             jedis = jedisPool.getResource();  
  70.             jedis.set(key, objectJson);  
  71.             return true;  
  72.         } catch (Exception e) {  
  73.             e.printStackTrace();  
  74.             return false;  
  75.         }finally{  
  76.             jedisPool.returnResource(jedis);  
  77.         }  
  78.     }  
  79.       
  80.     /** 
  81.      * 删除缓存中得对象,根据key 
  82.      * @param key 
  83.      * @return 
  84.      */  
  85.     public static boolean del(String key){  
  86.         Jedis jedis = null;  
  87.         try {  
  88.             jedis = jedisPool.getResource();  
  89.             jedis.del(key);  
  90.             return true;  
  91.         } catch (Exception e) {  
  92.             e.printStackTrace();  
  93.             return false;  
  94.         }finally{  
  95.             jedisPool.returnResource(jedis);  
  96.         }  
  97.     }  
  98.       
  99.     /** 
  100.      * 根据key 获取内容 
  101.      * @param key 
  102.      * @return 
  103.      */  
  104.     public static Object get(String key){  
  105.         Jedis jedis = null;  
  106.         try {  
  107.             jedis = jedisPool.getResource();  
  108.             Object value = jedis.get(key);  
  109.             return value;  
  110.         } catch (Exception e) {  
  111.             e.printStackTrace();  
  112.             return false;  
  113.         }finally{  
  114.             jedisPool.returnResource(jedis);  
  115.         }  
  116.     }  
  117.       
  118.   
  119.     /** 
  120.      * 根据key 获取对象 
  121.      * @param key 
  122.      * @return 
  123.      */  
  124.     public static <T> T get(String key,Class<T> clazz){  
  125.         Jedis jedis = null;  
  126.         try {  
  127.             jedis = jedisPool.getResource();  
  128.             String value = jedis.get(key);  
  129.             return JSON.parseObject(value, clazz);  
  130.         } catch (Exception e) {  
  131.             e.printStackTrace();  
  132.             return null;  
  133.         }finally{  
  134.             jedisPool.returnResource(jedis);  
  135.         }  
  136.     }  
  137.   
  138.   
  139. }  

 

 

4、Sharding 分片管理

 

  1. /** 
  2.  *  
  3.  * <p> 
  4.  * Sharding Redis Client 工具类 
  5.  * </p> 
  6.  *  
  7.  * @author 卓轩 
  8.  * @创建时间:2014年7月11日 
  9.  * @version: V1.0 
  10.  */  
  11. public class ShardingRedisClient {  
  12.   
  13.     private static ShardedJedisPool shardedJedisPool;  
  14.   
  15.     static {  
  16.         // 读取相关的配置  
  17.         ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");  
  18.         int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));  
  19.         int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));  
  20.         int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));  
  21.   
  22.         String ip = resourceBundle.getString("redis.ip");  
  23.         int port = Integer.parseInt(resourceBundle.getString("redis.port"));  
  24.           
  25.         //设置配置  
  26.         JedisPoolConfig config = new JedisPoolConfig();  
  27.         config.setMaxTotal(maxActive);  
  28.         config.setMaxIdle(maxIdle);  
  29.         config.setMaxWaitMillis(maxWait);  
  30.           
  31.         //设置分片元素信息  
  32.         JedisShardInfo shardInfo1 = new JedisShardInfo(ip,port);  
  33.         JedisShardInfo shardInfo2 = new JedisShardInfo(ip,port);  
  34.         List<JedisShardInfo> list = new ArrayList<JedisShardInfo>();  
  35.         list.add(shardInfo1);  
  36.         list.add(shardInfo2);  
  37.         shardedJedisPool = new ShardedJedisPool(config, list);  
  38.     }  
  39.   
  40.       
  41.     /** 
  42.      * 向缓存中设置字符串内容 
  43.      * @param key key 
  44.      * @param value value 
  45.      * @return 
  46.      * @throws Exception 
  47.      */  
  48.     public static boolean  set(String key,String value) throws Exception{  
  49.         ShardedJedis jedis = null;  
  50.         try {  
  51.             jedis = shardedJedisPool.getResource();  
  52.             jedis.set(key, value);  
  53.             return true;  
  54.         } catch (Exception e) {  
  55.             e.printStackTrace();  
  56.             return false;  
  57.         }finally{  
  58.             shardedJedisPool.returnResource(jedis);  
  59.         }  
  60.     }  
  61.       
  62.     /** 
  63.      * 向缓存中设置对象 
  64.      * @param key  
  65.      * @param value 
  66.      * @return 
  67.      */  
  68.     public static boolean  set(String key,Object value){  
  69.         ShardedJedis jedis = null;  
  70.         try {  
  71.             String objectJson = JSON.toJSONString(value);  
  72.             jedis = shardedJedisPool.getResource();  
  73.             jedis.set(key, objectJson);  
  74.             return true;  
  75.         } catch (Exception e) {  
  76.             e.printStackTrace();  
  77.             return false;  
  78.         }finally{  
  79.             shardedJedisPool.returnResource(jedis);  
  80.         }  
  81.     }  
  82.       
  83.     /** 
  84.      * 删除缓存中得对象,根据key 
  85.      * @param key 
  86.      * @return 
  87.      */  
  88.     public static boolean del(String key){  
  89.         ShardedJedis jedis = null;  
  90.         try {  
  91.             jedis = shardedJedisPool.getResource();  
  92.             jedis.del(key);  
  93.             return true;  
  94.         } catch (Exception e) {  
  95.             e.printStackTrace();  
  96.             return false;  
  97.         }finally{  
  98.             shardedJedisPool.returnResource(jedis);  
  99.         }  
  100.     }  
  101.       
  102.     /** 
  103.      * 根据key 获取内容 
  104.      * @param key 
  105.      * @return 
  106.      */  
  107.     public static Object get(String key){  
  108.         ShardedJedis jedis = null;  
  109.         try {  
  110.             jedis = shardedJedisPool.getResource();  
  111.             Object value = jedis.get(key);  
  112.             return value;  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.             return false;  
  116.         }finally{  
  117.             shardedJedisPool.returnResource(jedis);  
  118.         }  
  119.     }  
  120.       
  121.   
  122.     /** 
  123.      * 根据key 获取对象 
  124.      * @param key 
  125.      * @return 
  126.      */  
  127.     public static <T> T get(String key,Class<T> clazz){  
  128.         ShardedJedis jedis = null;  
  129.         try {  
  130.             jedis = shardedJedisPool.getResource();  
  131.             String value = jedis.get(key);  
  132.             return JSON.parseObject(value, clazz);  
  133.         } catch (Exception e) {  
  134.             e.printStackTrace();  
  135.             return null;  
  136.         }finally{  
  137.             shardedJedisPool.returnResource(jedis);  
  138.         }  
  139.     }  
  140.       
  141. }  



 

 

5、 单元测试、保存对象、写入对象

 

 

    1. /** 
    2.  *  
    3.  * <p> 
    4.  *  测试独立redis 客户端 
    5.  * </p> 
    6.  *  
    7.  * @author 卓轩 
    8.  * @创建时间:2014年7月11日 
    9.  * @version: V1.0 
    10.  */  
    11. public class SimpleClient {  
    12.       
    13.     @Test  
    14.     public void userCache(){  
    15.           
    16.         //向缓存中保存对象  
    17.         UserDO zhuoxuan = new UserDO();  
    18.         zhuoxuan.setUserId(113445);  
    19.         zhuoxuan.setSex(1);  
    20.         zhuoxuan.setUname("卓轩");  
    21.         zhuoxuan.setUnick("zhuoxuan");  
    22.         zhuoxuan.setEmail("zhuoxuan@mogujie.com");  
    23.         //调用方法处理  
    24.         boolean reusltCache = RedisClient.set("zhuoxuan", zhuoxuan);  
    25.         if (reusltCache) {  
    26.             System.out.println("向缓存中保存对象成功。");  
    27.         }else{  
    28.             System.out.println("向缓存中保存对象失败。");  
    29.         }  
    30.     }  
    31.       
    32.       
    33.     @Test  
    34.     public void getUserInfo(){  
    35.           
    36.         UserDO zhuoxuan = RedisClient.get("zhuoxuan",UserDO.class);  
    37.         if(zhuoxuan != null){  
    38.             System.out.println("从缓存中获取的对象," + zhuoxuan.getUname() + "@" + zhuoxuan.getEmail());  
    39.         }  
    40.           
    41.     }  
    42.       
    43.       
    44.   
    45. }  

Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存

标签:blog   http   io   ar   os   sp   for   java   strong   

原文地址:http://www.cnblogs.com/steven9801/p/4155146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!