RedisTemplate通用工具类RedisUtils

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

猜你喜欢

转载自blog.csdn.net/yangaliang/article/details/80258588