redis队列 Redis存储/取尽值,获取最新值
作者:互联网
/** * 取尽存储的值,获取最新值 * * @param key * @return */ @Override public String popData(String key) { List<String> list = new ArrayList<>(); String popDataKey = RedisKeyPrefix.getPopDataKey(key); // 同步锁 boolean lockStatus = redisTemplate.opsForValue().setIfAbsent(popDataKey, 1, 5, TimeUnit.SECONDS); if (!lockStatus) { return null; } while (redisTemplate.hasKey(key)) { Object data = redisTemplate.opsForList().leftPop(key); if (data != null) { list.add(data.toString()); } } redisTemplate.delete(popDataKey); if (CollectionUtils.isEmpty(list)) { return null; } return list.get(list.size() - 1); }
// 存储值 public void pushData(String key, String value) { redisTemplate.opsForList().rightPush(key, value); }
// 取尽存储的值 public List<String> popAllData(String key) { List<String> list = new ArrayList<>(); while (redisTemplate.hasKey(key)) { Object data = redisTemplate.opsForList().leftPop(key); if (data != null) { list.add(data.toString()); } } return list; }
// CURD /** * 新增或更新 * * @param key * @param value */ public void setOrUpdateData(String key, String value) { redisTemplate.opsForValue().set(key, value); } /** * 获取值 * * @param key * @return */ public String getData(String key) { return (String) redisTemplate.opsForValue().get(key); } /** * 批量删除 * * @param key 前缀 */ public void deleteData(String key) { redisTemplate.delete(redisTemplate.keys(key + "*")); }
标签:return,String,redis,Redis,list,key,data,取尽,redisTemplate 来源: https://www.cnblogs.com/axibug/p/16662453.html