zoukankan      html  css  js  c++  java
  • Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存

    源代码下载: 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. }  
  • 相关阅读:
    数据库模式
    数据流模式、转换、格式与操作
    桥接模式=抽象层协作关系+继承体系注入
    php 中更简洁的三元运算符 ?:
    larave5.6 将Excel文件数据导入数据库代码实例
    Laravel获取所有的数据库表及结构
    Laravel框架数据库CURD操作、连贯操作总结
    insert into 语句的三种写法
    Laravel 上传excel,读取并写入数据库 (实现自动建表、存记录值
    laravel5.4将excel表格中的信息导入到数据库中
  • 原文地址:https://www.cnblogs.com/fx2008/p/4155146.html
Copyright © 2011-2022 走看看