/** * 取尽存储的值,获取最新值 * * @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 + "*")); }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288018.html