RedisTemplate使用
String类型
@Override
public void testString() {
String key = "k1";
String currentNum;
Boolean value = client.hasKey(key);
log.info("[{}]是否存在[{}]", key, value);
client.opsForValue().set(key, "v1", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
String getValue = client.opsForValue().get(key);
log.info("getValue : [{}]", getValue);
String counter = "counter:key";
client.opsForValue().set(counter, "0", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
client.opsForValue().increment(counter);
client.opsForValue().increment(counter);
currentNum = client.opsForValue().get(counter);
log.info("currentNum : [{}]", currentNum);
client.opsForValue().decrement(counter);
currentNum = client.opsForValue().get(counter);
log.info("currentNum : [{}]", currentNum);
List<Map<String, String>> multiMapList = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
LinkedHashMap<String, String> itemMap = Maps.newLinkedHashMap();
itemMap.put("name", "jack" + i);
if (i % 2 == 0) {
itemMap.put("age", String.valueOf(10 + i));
itemMap.put("sex", "男");
} else {
itemMap.put("age", String.valueOf(11 + i));
itemMap.put("sex", "女");
}
multiMapList.add(itemMap);
}
String multiMapStr = JSON.toJSONString(multiMapList);
client.opsForValue().set("str:multiusers", multiMapStr, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
String userList = client.opsForValue().get("str:multiusers");
List<Map<String, String>> maps = CastBeanUtil.castListMap(JSON.parse(userList), String.class, String.class);
log.info("maps : [{}]", maps);
List<TzArea> areaList = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
TzArea item = new TzArea();
item.setAreaId((long) i);
item.setAreaName("江苏省");
item.setLevel(1);
item.setParentId(1L);
areaList.add(item);
}
client.opsForValue().set("str:multiareas", JSON.toJSONString(areaList), DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
String res = client.opsForValue().get("str:multiareas");
List<TzArea> tzAreas = JSON.parseArray(res, TzArea.class);
log.info("tzAreas : [{}]", JSON.toJSONString(tzAreas));
}
Hash类型
@Override
public void testHash() {
client.opsForHash().put("hash:user:single", "name", "pmb");
client.expire("user", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
Map<Object, Object> result = client.opsForHash().entries("hash:user:single");
log.info("result : [{}]", result);
Map<Object, Object> handleMap = Maps.newLinkedHashMap();
handleMap.put("name", "jack");
handleMap.put("age", "18");
handleMap.put("sex", "男");
String key = "hash:user:all";
client.opsForHash().putAll(key, handleMap);
Map<Object, Object> allElements = client.opsForHash().entries(key);
log.info("allElements : [{}]", allElements);
Set<Object> keyList = client.opsForHash().keys(key);
log.info("keyList : [{}]", keyList);
List<Object> valueList = client.opsForHash().values(key);
log.info("valueList : [{}]", valueList);
}
List类型
@Override
public void testList() {
String key = "list:phoneList";
client.opsForList().rightPush(key, "16607024161");
client.opsForList().rightPush(key, "16607024162");
client.opsForList().rightPush(key, "16607024163");
client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
client.opsForList().rightPop(key);
Long size = client.opsForList().size(key);
assert size !=null;
log.info(SIZE_FORMAT, size);
String firstItem = client.opsForList().index(key, 0);
log.info("firstItem : [{}]", firstItem);
String secondItem = client.opsForList().index(key, 1);
log.info("secondItem : [{}]", secondItem);
String thirdItem = client.opsForList().index(key, 2);
log.info("thirdItem : [{}]", thirdItem);
List<String> res = client.opsForList().range(key, 0, size - 1);
log.info("res : [{}]", res);
client.opsForList().set(key, 0, "12");
client.opsForList().leftPush(key, "1");
client.opsForList().leftPush(key, "2");
client.opsForList().leftPush(key, "3");
client.opsForList().leftPop(key);
client.opsForList().leftPop(key);
client.opsForList().leftPop(key);
client.opsForList().leftPush(key, "one");
client.opsForList().leftPush(key, "two");
client.opsForList().rightPop(key);
client.opsForList().rightPop(key);
}
Set类型
@Override
public void testSet() {
String key = "set:nums";
client.opsForSet().add(key, "1", "2", "3");
client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
Set<String> members = client.opsForSet().members(key);
log.info("members : [{}]", members);
Boolean member = client.opsForSet().isMember(key, "2");
log.info("member : [{}]", member);
String intersection = "set:nums:intersection";
client.opsForSet().add(intersection, "1", "2");
client.expire(intersection, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
Set<String> intersectList = client.opsForSet().intersect(key, intersection);
log.info("intersectList : [{}]", intersectList);
Set<String> unionList = client.opsForSet().union(key, intersection);
log.info("unionList : [{}]", unionList);
Set<String> differenceList = client.opsForSet().difference(key, intersection);
log.info("differenceList : [{}]", differenceList);
}
Zset类型
@Override
public void testZset() {
String key = "zset:nums";
client.opsForZSet().add(key, "one", 1);
client.opsForZSet().add(key, "three", 30);
client.opsForZSet().add(key, "two", 20);
client.opsForZSet().add(key, "four", 44);
client.opsForZSet().add(key, "five", 55);
client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
Long one = client.opsForZSet().rank(key, "five");
log.info("rank : [{}]", one);
Long size = client.opsForZSet().size(key);
assert size !=null;
log.info(SIZE_FORMAT, size);
Set<String> range = client.opsForZSet().range(key, 0, size - 1);
log.info("range : [{}]", range);
Set<String> userList = client.opsForZSet().rangeByScore(key, 1, 60);
log.info("userList : [{}]", userList);
client.opsForZSet().remove(key, "two");
log.info(SIZE_FORMAT, client.opsForZSet().size(key));
Long count = client.opsForZSet().count(key, 1, 20);
log.info("count : [{}]", count);
Set<String> invertedOrder = client.opsForZSet().reverseRange(key, 0, -1);
log.info("InvertedOrder : [{}]", invertedOrder);
Set<ZSetOperations.TypedTuple<String>> allLis = client.opsForZSet().rangeWithScores(key, 0, -1);
assert allLis != null;
for (ZSetOperations.TypedTuple<String> next : allLis) {
String value = next.getValue();
Double score = next.getScore();
log.info("通过rangeWithScores(K key, long start, long end)方法获取RedisZSetCommands.Tuples的区间值:[{}],[{}]", value, score);
}
String lexKey = "zset:lex";
client.delete(lexKey);
client.opsForZSet().add(lexKey, "zs", 55);
client.opsForZSet().add(lexKey, "ls", 55);
client.opsForZSet().add(lexKey, "ww", 54);
client.opsForZSet().add(lexKey, "zl", 55);
client.expire(lexKey, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);
Set<ZSetOperations.TypedTuple<String>> typedTuples = client.opsForZSet().rangeWithScores(lexKey, 0, -1);
if(Objects.isNull(typedTuples)){
throw new IllegalArgumentException("出现异常了");
}
Map<String, Double> cachedMap = new ConcurrentHashMap<>(16);
Iterator<ZSetOperations.TypedTuple<String>> scoreIterator = typedTuples.iterator();
String firstUser = null;
while (scoreIterator.hasNext()) {
ZSetOperations.TypedTuple<String> item = scoreIterator.next();
Double score = item.getScore();
String value = item.getValue();
if (cachedMap.containsValue(score)) {
firstUser = value;
}
cachedMap.put(value, score);
}
log.info("firstUser : [{}]", firstUser);
}