zoukankan      html  css  js  c++  java
  • redis整合spring(redisTemplate工具类)

     
     

    原文地址,转载请注明出处:http://blog.csdn.net/qq_34021712/article/details/75949706   ©王赛超

    前言

    关于哨兵模式的配置,我是参考网上的,只是把配置拿到了这里,本人并没有亲测是否有效,代码是注释掉的,需要配置哨兵模式的,可以参考一下。

    完整项目下载:
    在测试包下有一个TestRedis.java这个类,可以使用这个类main方法测试

    maven依赖

    [html] view plain copy
     
    1. <!-- Redis客户端 -->    
    2. <dependency>    
    3.     <groupId>redis.clients</groupId>     
    4.     <artifactId>jedis</artifactId>    
    5.     <version>2.9.0</version>    
    6. </dependency>    
    7. <!-- redis Spring 基于注解配置 -->     
    8. <dependency>      
    9.   <groupId>org.springframework.data</groupId>      
    10.   <artifactId>spring-data-redis</artifactId>      
    11.   <version>1.7.2.RELEASE</version>      
    12. </dependency>   

    redis.properties

    [ruby] view plain copy
     
    1. #ip地址  
    2. redis.hostName=172.20.1.205  
    3. #端口号  
    4. redis.port=6379  
    5. #如果有密码  
    6. redis.password=123456  
    7. #客户端超时时间单位是毫秒 默认是2000  
    8. redis.timeout=10000  
    9.   
    10.   
    11. #最大空闲数  
    12. redis.maxIdle=300  
    13. #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal  
    14. #redis.maxActive=600  
    15. #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性  
    16. redis.maxTotal=1000  
    17. #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。  
    18. redis.maxWaitMillis=1000  
    19. #连接的最小空闲时间 默认1800000毫秒(30分钟)  
    20. redis.minEvictableIdleTimeMillis=300000  
    21. #每次释放连接的最大数目,默认3  
    22. redis.numTestsPerEvictionRun=1024  
    23. #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1  
    24. redis.timeBetweenEvictionRunsMillis=30000  
    25. #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个  
    26. redis.testOnBorrow=true  
    27. #在空闲时检查有效性, 默认false  
    28. redis.testWhileIdle=true  
    29.   
    30.   
    31. #redis.sentinel.host1=172.20.1.230  
    32. #redis.sentinel.port1=26379  
    33.   
    34.   
    35. #redis.sentinel.host2=172.20.1.231  
    36. #redis.sentinel.port2=26379  
    37.   
    38.   
    39. #redis.sentinel.host3=172.20.1.232  
    40. #redis.sentinel.port3=26379  

    applicationContext-redis.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>    
    2. <beans xmlns="http://www.springframework.org/schema/beans"      
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"      
    4.     xmlns:context="http://www.springframework.org/schema/context"      
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc"      
    6.     xmlns:cache="http://www.springframework.org/schema/cache"    
    7.     xsi:schemaLocation="http://www.springframework.org/schema/beans        
    8.                         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        
    9.                         http://www.springframework.org/schema/context        
    10.                         http://www.springframework.org/schema/context/spring-context-4.2.xsd        
    11.                         http://www.springframework.org/schema/mvc        
    12.                         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd    
    13.                         http://www.springframework.org/schema/cache     
    14.                         http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">  
    15.   
    16.   
    17.       
    18.     <!-- 加载配置文件 -->  
    19.     <context:property-placeholder location="classpath:properties/*.properties" />  
    20.     <!-- redis连接池配置-->    
    21.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >    
    22.         <!--最大空闲数-->    
    23.         <property name="maxIdle" value="${redis.maxIdle}" />    
    24.         <!--连接池的最大数据库连接数  -->  
    25.         <property name="maxTotal" value="${redis.maxTotal}" />  
    26.         <!--最大建立连接等待时间-->    
    27.         <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />    
    28.         <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->  
    29.         <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />   
    30.         <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->  
    31.         <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />   
    32.         <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->  
    33.         <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />   
    34.         <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->    
    35.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />    
    36.         <!--在空闲时检查有效性, 默认false  -->  
    37.         <property name="testWhileIdle" value="${redis.testWhileIdle}" />    
    38.     </bean >  
    39.       
    40.       
    41.     <!-- redis集群配置 哨兵模式 -->  
    42.     <!-- <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">  
    43.         <property name="master">  
    44.             <bean class="org.springframework.data.redis.connection.RedisNode">  
    45.                 这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的  
    46.                 <property name="name" value="mymaster"></property>  
    47.             </bean>  
    48.         </property>  
    49.         记住了,这里是指定Sentinel的IP和端口,不是Master和Slave的  
    50.         <property name="sentinels">  
    51.             <set>  
    52.                 <bean class="org.springframework.data.redis.connection.RedisNode">  
    53.                     <constructor-arg name="host" value="${redis.sentinel.host1}"></constructor-arg>  
    54.                     <constructor-arg name="port" value="${redis.sentinel.port1}"></constructor-arg>  
    55.                 </bean>  
    56.                 <bean class="org.springframework.data.redis.connection.RedisNode">  
    57.                     <constructor-arg name="host" value="${redis.sentinel.host2}"></constructor-arg>  
    58.                     <constructor-arg name="port" value="${redis.sentinel.port2}"></constructor-arg>  
    59.                 </bean>  
    60.                 <bean class="org.springframework.data.redis.connection.RedisNode">  
    61.                     <constructor-arg name="host" value="${redis.sentinel.host3}"></constructor-arg>  
    62.                     <constructor-arg name="port" value="${redis.sentinel.port3}"></constructor-arg>  
    63.                 </bean>  
    64.             </set>  
    65.         </property>  
    66.     </bean>  
    67.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
    68.         <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>  
    69.         <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>  
    70.     </bean> -->  
    71.       
    72.     <!--redis连接工厂 -->  
    73.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">   
    74.         <property name="poolConfig" ref="jedisPoolConfig"></property>   
    75.         <!--IP地址 -->  
    76.         <property name="hostName" value="${redis.hostName}"></property>   
    77.         <!--端口号  -->  
    78.         <property name="port" value="${redis.port}"></property>   
    79.         <!--如果Redis设置有密码  -->  
    80.         <property name="password" value="${redis.password}" />  
    81.         <!--客户端超时时间单位是毫秒  -->  
    82.         <property name="timeout" value="${redis.timeout}"></property>   
    83.     </bean>    
    84.       
    85.     <!--redis操作模版,使用该对象可以操作redis  -->  
    86.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >    
    87.         <property name="connectionFactory" ref="jedisConnectionFactory" />    
    88.         <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->    
    89.         <property name="keySerializer" >    
    90.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
    91.         </property>    
    92.         <property name="valueSerializer" >    
    93.             <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />    
    94.         </property>    
    95.         <property name="hashKeySerializer">    
    96.             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>    
    97.         </property>    
    98.         <property name="hashValueSerializer">    
    99.             <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>    
    100.         </property>    
    101.         <!--开启事务  -->  
    102.         <property name="enableTransactionSupport" value="true"></property>  
    103.     </bean >    
    104.       
    105.     <!--自定义redis工具类,在需要缓存的地方注入此类  -->  
    106.     <bean id="redisUtil" class="com.ryx.global.util.RedisUtil">  
    107.         <property name="redisTemplate" ref="redisTemplate" />  
    108.     </bean>  
    109.       
    110. </beans>  

    RedisUtil.java

    [java] view plain copy
     
    1. package com.ryx.global.util;  
    2.   
    3.   
    4. import java.util.List;  
    5. import java.util.Map;  
    6. import java.util.Set;  
    7. import java.util.concurrent.TimeUnit;  
    8.   
    9.   
    10. import org.springframework.data.redis.core.RedisTemplate;  
    11. import org.springframework.util.CollectionUtils;  
    12.   
    13.   
    14. /** 
    15.  *  
    16.  * @author 王赛超 
    17.  * 基于spring和redis的redisTemplate工具类 
    18.  * 针对所有的hash 都是以h开头的方法 
    19.  * 针对所有的Set 都是以s开头的方法                    不含通用方法 
    20.  * 针对所有的List 都是以l开头的方法 
    21.  */  
    22. public class RedisUtil {  
    23.   
    24.   
    25.     private RedisTemplate<String, Object> redisTemplate;  
    26.       
    27.     public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {  
    28.         this.redisTemplate = redisTemplate;  
    29.     }  
    30.     //=============================common============================  
    31.     /** 
    32.      * 指定缓存失效时间 
    33.      * @param key 键 
    34.      * @param time 时间(秒) 
    35.      * @return 
    36.      */  
    37.     public boolean expire(String key,long time){  
    38.         try {  
    39.             if(time>0){  
    40.                 redisTemplate.expire(key, time, TimeUnit.SECONDS);  
    41.             }  
    42.             return true;  
    43.         } catch (Exception e) {  
    44.             e.printStackTrace();  
    45.             return false;  
    46.         }  
    47.     }  
    48.       
    49.     /** 
    50.      * 根据key 获取过期时间 
    51.      * @param key 键 不能为null 
    52.      * @return 时间(秒) 返回0代表为永久有效 
    53.      */  
    54.     public long getExpire(String key){  
    55.         return redisTemplate.getExpire(key,TimeUnit.SECONDS);  
    56.     }  
    57.       
    58.     /** 
    59.      * 判断key是否存在 
    60.      * @param key 键 
    61.      * @return true 存在 false不存在 
    62.      */  
    63.     public boolean hasKey(String key){  
    64.         try {  
    65.             return redisTemplate.hasKey(key);  
    66.         } catch (Exception e) {  
    67.             e.printStackTrace();  
    68.             return false;  
    69.         }  
    70.     }  
    71.       
    72.     /** 
    73.      * 删除缓存 
    74.      * @param key 可以传一个值 或多个 
    75.      */  
    76.     @SuppressWarnings("unchecked")  
    77.     public void del(String ... key){  
    78.         if(key!=null&&key.length>0){  
    79.             if(key.length==1){  
    80.                 redisTemplate.delete(key[0]);  
    81.             }else{  
    82.                 redisTemplate.delete(CollectionUtils.arrayToList(key));  
    83.             }  
    84.         }  
    85.     }  
    86.       
    87.     //============================String=============================  
    88.     /** 
    89.      * 普通缓存获取 
    90.      * @param key 键 
    91.      * @return 值 
    92.      */  
    93.     public Object get(String key){  
    94.         return key==null?null:redisTemplate.opsForValue().get(key);  
    95.     }  
    96.       
    97.     /** 
    98.      * 普通缓存放入 
    99.      * @param key 键 
    100.      * @param value 值 
    101.      * @return true成功 false失败 
    102.      */  
    103.     public boolean set(String key,Object value) {  
    104.          try {  
    105.             redisTemplate.opsForValue().set(key, value);  
    106.             return true;  
    107.         } catch (Exception e) {  
    108.             e.printStackTrace();  
    109.             return false;  
    110.         }  
    111.           
    112.     }  
    113.       
    114.     /** 
    115.      * 普通缓存放入并设置时间 
    116.      * @param key 键 
    117.      * @param value 值 
    118.      * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 
    119.      * @return true成功 false 失败 
    120.      */  
    121.     public boolean set(String key,Object value,long time){  
    122.         try {  
    123.             if(time>0){  
    124.                 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);  
    125.             }else{  
    126.                 set(key, value);  
    127.             }  
    128.             return true;  
    129.         } catch (Exception e) {  
    130.             e.printStackTrace();  
    131.             return false;  
    132.         }  
    133.     }  
    134.       
    135.     /** 
    136.      * 递增 
    137.      * @param key 键 
    138.      * @param by 要增加几(大于0) 
    139.      * @return 
    140.      */  
    141.     public long incr(String key, long delta){    
    142.         if(delta<0){  
    143.             throw new RuntimeException("递增因子必须大于0");  
    144.         }  
    145.         return redisTemplate.opsForValue().increment(key, delta);  
    146.     }  
    147.       
    148.     /** 
    149.      * 递减 
    150.      * @param key 键 
    151.      * @param by 要减少几(小于0) 
    152.      * @return 
    153.      */  
    154.     public long decr(String key, long delta){    
    155.         if(delta<0){  
    156.             throw new RuntimeException("递减因子必须大于0");  
    157.         }  
    158.         return redisTemplate.opsForValue().increment(key, -delta);    
    159.     }    
    160.       
    161.     //================================Map=================================  
    162.     /** 
    163.      * HashGet 
    164.      * @param key 键 不能为null 
    165.      * @param item 项 不能为null 
    166.      * @return 值 
    167.      */  
    168.     public Object hget(String key,String item){  
    169.         return redisTemplate.opsForHash().get(key, item);  
    170.     }  
    171.       
    172.     /** 
    173.      * 获取hashKey对应的所有键值 
    174.      * @param key 键 
    175.      * @return 对应的多个键值 
    176.      */  
    177.     public Map<Object,Object> hmget(String key){  
    178.         return redisTemplate.opsForHash().entries(key);  
    179.     }  
    180.       
    181.     /** 
    182.      * HashSet 
    183.      * @param key 键 
    184.      * @param map 对应多个键值 
    185.      * @return true 成功 false 失败 
    186.      */  
    187.     public boolean hmset(String key, Map<String,Object> map){    
    188.         try {  
    189.             redisTemplate.opsForHash().putAll(key, map);  
    190.             return true;  
    191.         } catch (Exception e) {  
    192.             e.printStackTrace();  
    193.             return false;  
    194.         }  
    195.     }  
    196.       
    197.     /** 
    198.      * HashSet 并设置时间 
    199.      * @param key 键 
    200.      * @param map 对应多个键值 
    201.      * @param time 时间(秒) 
    202.      * @return true成功 false失败 
    203.      */  
    204.     public boolean hmset(String key, Map<String,Object> map, long time){    
    205.         try {  
    206.             redisTemplate.opsForHash().putAll(key, map);  
    207.             if(time>0){  
    208.                 expire(key, time);  
    209.             }  
    210.             return true;  
    211.         } catch (Exception e) {  
    212.             e.printStackTrace();  
    213.             return false;  
    214.         }  
    215.     }  
    216.       
    217.     /** 
    218.      * 向一张hash表中放入数据,如果不存在将创建 
    219.      * @param key 键 
    220.      * @param item 项 
    221.      * @param value 值 
    222.      * @return true 成功 false失败 
    223.      */  
    224.     public boolean hset(String key,String item,Object value) {  
    225.          try {  
    226.             redisTemplate.opsForHash().put(key, item, value);  
    227.             return true;  
    228.         } catch (Exception e) {  
    229.             e.printStackTrace();  
    230.             return false;  
    231.         }  
    232.     }  
    233.       
    234.     /** 
    235.      * 向一张hash表中放入数据,如果不存在将创建 
    236.      * @param key 键 
    237.      * @param item 项 
    238.      * @param value 值 
    239.      * @param time 时间(秒)  注意:如果已存在的hash表有时间,这里将会替换原有的时间 
    240.      * @return true 成功 false失败 
    241.      */  
    242.     public boolean hset(String key,String item,Object value,long time) {  
    243.          try {  
    244.             redisTemplate.opsForHash().put(key, item, value);  
    245.             if(time>0){  
    246.                 expire(key, time);  
    247.             }  
    248.             return true;  
    249.         } catch (Exception e) {  
    250.             e.printStackTrace();  
    251.             return false;  
    252.         }  
    253.     }  
    254.       
    255.     /** 
    256.      * 删除hash表中的值 
    257.      * @param key 键 不能为null 
    258.      * @param item 项 可以使多个 不能为null 
    259.      */  
    260.     public void hdel(String key, Object... item){    
    261.         redisTemplate.opsForHash().delete(key,item);  
    262.     }   
    263.       
    264.     /** 
    265.      * 判断hash表中是否有该项的值 
    266.      * @param key 键 不能为null 
    267.      * @param item 项 不能为null 
    268.      * @return true 存在 false不存在 
    269.      */  
    270.     public boolean hHasKey(String key, String item){  
    271.         return redisTemplate.opsForHash().hasKey(key, item);  
    272.     }   
    273.       
    274.     /** 
    275.      * hash递增 如果不存在,就会创建一个 并把新增后的值返回 
    276.      * @param key 键 
    277.      * @param item 项 
    278.      * @param by 要增加几(大于0) 
    279.      * @return 
    280.      */  
    281.     public double hincr(String key, String item,double by){    
    282.         return redisTemplate.opsForHash().increment(key, item, by);  
    283.     }  
    284.       
    285.     /** 
    286.      * hash递减 
    287.      * @param key 键 
    288.      * @param item 项 
    289.      * @param by 要减少记(小于0) 
    290.      * @return 
    291.      */  
    292.     public double hdecr(String key, String item,double by){    
    293.         return redisTemplate.opsForHash().increment(key, item,-by);    
    294.     }    
    295.       
    296.     //============================set=============================  
    297.     /** 
    298.      * 根据key获取Set中的所有值 
    299.      * @param key 键 
    300.      * @return 
    301.      */  
    302.     public Set<Object> sGet(String key){  
    303.         try {  
    304.             return redisTemplate.opsForSet().members(key);  
    305.         } catch (Exception e) {  
    306.             e.printStackTrace();  
    307.             return null;  
    308.         }  
    309.     }  
    310.       
    311.     /** 
    312.      * 根据value从一个set中查询,是否存在 
    313.      * @param key 键 
    314.      * @param value 值 
    315.      * @return true 存在 false不存在 
    316.      */  
    317.     public boolean sHasKey(String key,Object value){  
    318.         try {  
    319.             return redisTemplate.opsForSet().isMember(key, value);  
    320.         } catch (Exception e) {  
    321.             e.printStackTrace();  
    322.             return false;  
    323.         }  
    324.     }  
    325.       
    326.     /** 
    327.      * 将数据放入set缓存 
    328.      * @param key 键 
    329.      * @param values 值 可以是多个 
    330.      * @return 成功个数 
    331.      */  
    332.     public long sSet(String key, Object...values) {  
    333.         try {  
    334.             return redisTemplate.opsForSet().add(key, values);  
    335.         } catch (Exception e) {  
    336.             e.printStackTrace();  
    337.             return 0;  
    338.         }  
    339.     }  
    340.       
    341.     /** 
    342.      * 将set数据放入缓存 
    343.      * @param key 键 
    344.      * @param time 时间(秒) 
    345.      * @param values 值 可以是多个 
    346.      * @return 成功个数 
    347.      */  
    348.     public long sSetAndTime(String key,long time,Object...values) {  
    349.         try {  
    350.             Long count = redisTemplate.opsForSet().add(key, values);  
    351.             if(time>0) expire(key, time);  
    352.             return count;  
    353.         } catch (Exception e) {  
    354.             e.printStackTrace();  
    355.             return 0;  
    356.         }  
    357.     }  
    358.       
    359.     /** 
    360.      * 获取set缓存的长度 
    361.      * @param key 键 
    362.      * @return 
    363.      */  
    364.     public long sGetSetSize(String key){  
    365.         try {  
    366.             return redisTemplate.opsForSet().size(key);  
    367.         } catch (Exception e) {  
    368.             e.printStackTrace();  
    369.             return 0;  
    370.         }  
    371.     }  
    372.       
    373.     /** 
    374.      * 移除值为value的 
    375.      * @param key 键 
    376.      * @param values 值 可以是多个 
    377.      * @return 移除的个数 
    378.      */  
    379.     public long setRemove(String key, Object ...values) {  
    380.         try {  
    381.             Long count = redisTemplate.opsForSet().remove(key, values);  
    382.             return count;  
    383.         } catch (Exception e) {  
    384.             e.printStackTrace();  
    385.             return 0;  
    386.         }  
    387.     }  
    388.     //===============================list=================================  
    389.       
    390.     /** 
    391.      * 获取list缓存的内容 
    392.      * @param key 键 
    393.      * @param start 开始 
    394.      * @param end 结束  0 到 -1代表所有值 
    395.      * @return 
    396.      */  
    397.     public List<Object> lGet(String key,long start, long end){  
    398.         try {  
    399.             return redisTemplate.opsForList().range(key, start, end);  
    400.         } catch (Exception e) {  
    401.             e.printStackTrace();  
    402.             return null;  
    403.         }  
    404.     }  
    405.       
    406.     /** 
    407.      * 获取list缓存的长度 
    408.      * @param key 键 
    409.      * @return 
    410.      */  
    411.     public long lGetListSize(String key){  
    412.         try {  
    413.             return redisTemplate.opsForList().size(key);  
    414.         } catch (Exception e) {  
    415.             e.printStackTrace();  
    416.             return 0;  
    417.         }  
    418.     }  
    419.       
    420.     /** 
    421.      * 通过索引 获取list中的值 
    422.      * @param key 键 
    423.      * @param index 索引  index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 
    424.      * @return 
    425.      */  
    426.     public Object lGetIndex(String key,long index){  
    427.         try {  
    428.             return redisTemplate.opsForList().index(key, index);  
    429.         } catch (Exception e) {  
    430.             e.printStackTrace();  
    431.             return null;  
    432.         }  
    433.     }  
    434.       
    435.     /** 
    436.      * 将list放入缓存 
    437.      * @param key 键 
    438.      * @param value 值 
    439.      * @param time 时间(秒) 
    440.      * @return 
    441.      */  
    442.     public boolean lSet(String key, Object value) {  
    443.         try {  
    444.             redisTemplate.opsForList().rightPush(key, value);  
    445.             return true;  
    446.         } catch (Exception e) {  
    447.             e.printStackTrace();  
    448.             return false;  
    449.         }  
    450.     }  
    451.       
    452.     /** 
    453.      * 将list放入缓存 
    454.      * @param key 键 
    455.      * @param value 值 
    456.      * @param time 时间(秒) 
    457.      * @return 
    458.      */  
    459.     public boolean lSet(String key, Object value, long time) {  
    460.         try {  
    461.             redisTemplate.opsForList().rightPush(key, value);  
    462.             if (time > 0) expire(key, time);  
    463.             return true;  
    464.         } catch (Exception e) {  
    465.             e.printStackTrace();  
    466.             return false;  
    467.         }  
    468.     }  
    469.       
    470.     /** 
    471.      * 将list放入缓存 
    472.      * @param key 键 
    473.      * @param value 值 
    474.      * @param time 时间(秒) 
    475.      * @return 
    476.      */  
    477.     public boolean lSet(String key, List<Object> value) {  
    478.         try {  
    479.             redisTemplate.opsForList().rightPushAll(key, value);  
    480.             return true;  
    481.         } catch (Exception e) {  
    482.             e.printStackTrace();  
    483.             return false;  
    484.         }  
    485.     }  
    486.       
    487.     /** 
    488.      * 将list放入缓存 
    489.      * @param key 键 
    490.      * @param value 值 
    491.      * @param time 时间(秒) 
    492.      * @return 
    493.      */  
    494.     public boolean lSet(String key, List<Object> value, long time) {  
    495.         try {  
    496.             redisTemplate.opsForList().rightPushAll(key, value);  
    497.             if (time > 0) expire(key, time);  
    498.             return true;  
    499.         } catch (Exception e) {  
    500.             e.printStackTrace();  
    501.             return false;  
    502.         }  
    503.     }  
    504.       
    505.     /** 
    506.      * 根据索引修改list中的某条数据 
    507.      * @param key 键 
    508.      * @param index 索引 
    509.      * @param value 值 
    510.      * @return 
    511.      */  
    512.     public boolean lUpdateIndex(String key, long index,Object value) {  
    513.         try {  
    514.             redisTemplate.opsForList().set(key, index, value);  
    515.             return true;  
    516.         } catch (Exception e) {  
    517.             e.printStackTrace();  
    518.             return false;  
    519.         }  
    520.     }   
    521.       
    522.     /** 
    523.      * 移除N个值为value  
    524.      * @param key 键 
    525.      * @param count 移除多少个 
    526.      * @param value 值 
    527.      * @return 移除的个数 
    528.      */  
    529.     public long lRemove(String key,long count,Object value) {  
    530.         try {  
    531.             Long remove = redisTemplate.opsForList().remove(key, count, value);  
    532.             return remove;  
    533.         } catch (Exception e) {  
    534.             e.printStackTrace();  
    535.             return 0;  
    536.         }  
    537.     }  
    538.       
    539. }  

    TestRedis.java

    [java] view plain copy
     
    1. package com.ryx.test;  
    2.   
    3.   
    4. import java.util.ArrayList;  
    5. import java.util.HashMap;  
    6. import java.util.List;  
    7. import java.util.Map;  
    8.   
    9.   
    10. import org.springframework.context.ApplicationContext;  
    11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    12.   
    13.   
    14. import com.ryx.global.util.RedisUtil;  
    15.   
    16.   
    17. public class TestRedis {  
    18.       
    19.     public static void main(String[] args) throws Exception {  
    20.         @SuppressWarnings("resource")  
    21.         ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");  
    22.         RedisUtil redisUtil=(RedisUtil) context.getBean("redisUtil");  
    23.           
    24.         //=====================testString======================  
    25.         //redisUtil.set("name", "王赛超");  
    26.         //redisUtil.set("age", 24);  
    27.         //redisUtil.set("address", "河北邯郸");  
    28.           
    29.         //System.out.println(redisUtil.set("address", "河北邯郸", 50));  
    30.           
    31.         //System.out.println(redisUtil.get("age"));  
    32.           
    33.           
    34.         //redisUtil.set("age", 1000);  
    35.           
    36.         //Object object = redisUtil.get("user2");  
    37.           
    38.         //System.out.println(object);  
    39.           
    40.         //redisUtil.del("address");  
    41.         //redisUtil.set("class", 15);  
    42.         //long incr = redisUtil.incr("a", 1);  
    43.         //System.out.println(incr);  
    44.           
    45.         //Thread.sleep(5000);  
    46.         /*Map<String,Object> map=new HashMap<>(); 
    47.         map.put("name", "王赛超"); 
    48.         map.put("age", 24); 
    49.         map.put("address", "河北邯郸666"); 
    50.         redisUtil.hmset("15532002725", map,1000);*/  
    51.           
    52.         //redisUtil.del("15532002725");  
    53.         //redisUtil.hset("15532002725","address","河北邯郸",1000);  
    54.         //redisUtil.hdel("15532002725", "name");  
    55.         //System.out.println(redisUtil.sSetAndTime("15532002727",1000,"haha"));  
    56.         //System.out.println(redisUtil.sGet("15532002727"));  
    57.         //System.out.println(redisUtil.sHasKey("15532002727","name"));  
    58.         System.out.println(redisUtil.lRemove("15532002728",1,2));  
    59.         System.out.println(redisUtil.lGet("15532002728",0,-1));  
    60.         System.out.println(redisUtil.lGetListSize("15532002728"));  
    61.         System.out.println(redisUtil.lGetIndex("15532002728",1));  
    62.           
    63.           
    64.         //System.out.println(redisUtil.getExpire("15532002725"));  
    65.           
    66.         //System.out.println(redisUtil.hget("15532002725","name"));  
    67.         //System.out.println(redisUtil.hmget("15532002725"));  
    68.           
    69.     }  
    70.       
    71.       
    72.       
    73. }  
    74. class User{  
    75.     private String name;  
    76.     private Integer age;  
    77.     private String address;  
    78.     private Double classz;  
    79.     private Float classz2;  
    80.     public User() {  
    81.         super();  
    82.     }  
    83.     public User(String name, Integer age, String address, Double classz,  
    84.             Float classz2) {  
    85.         super();  
    86.         this.name = name;  
    87.         this.age = age;  
    88.         this.address = address;  
    89.         this.classz = classz;  
    90.         this.classz2 = classz2;  
    91.     }  
    92.     public String getName() {  
    93.         return name;  
    94.     }  
    95.     public void setName(String name) {  
    96.         this.name = name;  
    97.     }  
    98.     public Integer getAge() {  
    99.         return age;  
    100.     }  
    101.     public void setAge(Integer age) {  
    102.         this.age = age;  
    103.     }  
    104.     public String getAddress() {  
    105.         return address;  
    106.     }  
    107.     public void setAddress(String address) {  
    108.         this.address = address;  
    109.     }  
    110.     public Double getClassz() {  
    111.         return classz;  
    112.     }  
    113.     public void setClassz(Double classz) {  
    114.         this.classz = classz;  
    115.     }  
    116.     public Float getClassz2() {  
    117.         return classz2;  
    118.     }  
    119.     public void setClassz2(Float classz2) {  
    120.         this.classz2 = classz2;  
    121.     }  
    122. }  
  • 相关阅读:
    防止表单重复提交
    tp5中的配置机制
    PHP remove,empty和detach区别
    jquery data方法
    webstrom使用记录
    input checkbox问题和li里面包含checkbox
    【转】HTML中A标签与click事件的前世今生
    jquery toggle方法
    webstore+nodejs
    web storm使用和配置
  • 原文地址:https://www.cnblogs.com/wojiaochuichui/p/8668602.html
Copyright © 2011-2022 走看看