标签:数据库 param base init cat factor 时间 unit seconds
1 /** 2 * @author yanming.zhang 3 * @date 2019/1/25 21:15 4 */ 5 @Component 6 public class RedisHelp { 7 8 @Autowired
9 private RedisTemplate redisTemplate; 10 11 /** 12 * 切换redis数据库 13 * @param i 0-15 14 * @return 15 */ 16 public boolean changeDatabase(int i) { 17 boolean result = false; 18 try { 19 LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory)redisTemplate.getConnectionFactory(); 20 lettuceConnectionFactory.setDatabase(i); 21 result = true; 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 return result; 26 } 27 /** 28 * 写入缓存 29 * @param key 30 * @param value 31 * @return 32 */ 33 public boolean set(final String key, Object value) { 34 boolean result = false; 35 try { 36 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 37 operations.set(key, value); 38 result = true; 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 return result; 43 } 44 /** 45 * 写入缓存设置时效时间 46 * @param key 47 * @param value 48 * @return 49 */ 50 public boolean set(final String key, Object value, Long expireTime) { 51 boolean result = false; 52 try { 53 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 54 operations.set(key, value,expireTime, TimeUnit.SECONDS); 55 result = true; 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 return result; 60 } 61 /** 62 * 重新设置时效时间 63 * @param key 64 * @param expireTime 65 * @return 66 */ 67 public boolean setExpire(final String key,Long expireTime){ 68 boolean result = false; 69 try { 70 redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 71 result = true; 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 return result; 76 } 77 /** 78 * 批量删除对应的value 79 * @param keys 80 */ 81 public void remove(String... keys) { 82 for (String key : keys) { 83 remove(key); 84 } 85 } 86 87 /** 88 * 批量删除key 89 * @param pattern 90 */ 91 public void removePattern(String pattern) { 92 Set<Serializable> keys = redisTemplate.keys(pattern); 93 if (keys.size() > 0){ 94 redisTemplate.delete(keys); 95 } 96 } 97 /** 98 * 删除对应的value 99 * @param key 100 */ 101 public void remove(String key) { 102 if (exists(key)) { 103 redisTemplate.delete(key); 104 } 105 } 106 /** 107 * 判断缓存中是否有对应的value 108 * @param key 109 * @return 110 */ 111 public boolean exists(String key) { 112 if(key==null){ 113 return false; 114 } 115 return redisTemplate.hasKey(key); 116 } 117 /** 118 * 读取缓存 119 * @param key 120 * @return 121 */ 122 public Object get(String key) { 123 Object result = null; 124 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 125 result = operations.get(key); 126 return result; 127 } 128 /** 129 * 哈希 添加 130 * @param key 131 * @param hashKey 132 * @param value 133 */ 134 public void hmSet(String key, Object hashKey, Object value){ 135 HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); 136 hash.put(key,hashKey,value); 137 } 138 139 /** 140 * 哈希获取数据 141 * @param key 142 * @param hashKey 143 * @return 144 */ 145 public Object hmGet(String key, Object hashKey){ 146 HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); 147 return hash.get(key,hashKey); 148 } 149 150 /** 151 * 列表添加 152 * @param k 153 * @param v 154 */ 155 public void lPush(String k,Object v){ 156 ListOperations<String, Object> list = redisTemplate.opsForList(); 157 list.rightPush(k,v); 158 } 159 160 /** 161 * 列表获取 162 * @param k 163 * @param l 164 * @param l1 165 * @return 166 */ 167 public List<Object> lRange(String k, long l, long l1){ 168 ListOperations<String, Object> list = redisTemplate.opsForList(); 169 return list.range(k,l,l1); 170 } 171 172 /** 173 * 无序,不重复集合添加 174 * @param key 175 * @param value 176 */ 177 public void add(String key,Object value){ 178 SetOperations<String, Object> set = redisTemplate.opsForSet(); 179 set.add(key,value); 180 } 181 /** 182 * 无序,删除指定集合中的指定元素 183 * @param s 184 */ 185 public void pop(String s,String o){ 186 SetOperations<String, Object> set = redisTemplate.opsForSet(); 187 set.remove(s,o); 188 189 } 190 191 /** 192 *无序,不重复 集合获取 193 * @param key 194 * @return 195 */ 196 public Set<Object> getMembers(String key){ 197 SetOperations<String, Object> set = redisTemplate.opsForSet(); 198 return set.members(key); 199 } 200 201 /** 202 * 有序,不重复集合添加 203 * @param key 204 * @param value 205 * @param scoure 206 */ 207 public void zAdd(String key,Object value,double scoure){ 208 ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); 209 zset.add(key,value,scoure); 210 } 211 212 /** 213 * 有序,不重复集合获取 214 * @param key 215 * @param scoure 216 * @param scoure1 217 * @return 218 */ 219 public Set<Object> rangeByScore(String key,double scoure,double scoure1){ 220 ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); 221 return zset.rangeByScore(key, scoure, scoure1); 222 } 223 @PostConstruct 224 private void init() { 225 redisTemplate.setKeySerializer(new StringRedisSerializer()); 226 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 227 redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)); 228 redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)); 229 } 230 }
标签:数据库 param base init cat factor 时间 unit seconds
原文地址:https://www.cnblogs.com/yanmingzhang/p/10331391.html