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