/**
* list
*/
@Test
public void list() {
redisTemplate.opsForList().leftPush("list1", "1");
redisTemplate.opsForList().leftPush("list1", "2");
redisTemplate.opsForList().leftPush("list1", "3");
System.out.println(redisTemplate.opsForList().rightPop("list1"));
System.out.println(redisTemplate.opsForList().rightPop("list1"));
System.out.println(redisTemplate.opsForList().rightPop("list1"));
List<Object> range = redisTemplate.opsForList().range("list1", 0, redisTemplate.opsForList().size("list1"));
assert range != null;
System.out.println(Arrays.toString(range.toArray()));
}
/**
* hashmap
*/
@Test
public void hash() {
redisTemplate.opsForHash().put("hash1", "key1", "value1");
redisTemplate.opsForHash().put("hash1", "key2", "value2");
redisTemplate.opsForHash().put("hash1", "key3", "value3");
redisTemplate.opsForHash().put("hash1", "key1", "value4");
System.out.println(redisTemplate.opsForHash().get("hash1", "key1"));
System.out.println(redisTemplate.opsForHash().get("hash1", "key2"));
System.out.println(redisTemplate.opsForHash().get("hash1", "key3"));
}
/**
* set
*/
@Test
public void set() {
redisTemplate.opsForSet().add("set1", 1,2,3,4,5,6);
System.out.println(redisTemplate.opsForSet().pop("set1"));
System.out.println(redisTemplate.opsForSet().pop("set1"));
System.out.println(redisTemplate.opsForSet().pop("set1"));
redisTemplate.opsForSet().move("set1",1,"set2");
}
/**
* 根据权重排序
*/
@Test
public void z_set() {
redisTemplate.opsForZSet().add("z-set", "z-set1", 1);
redisTemplate.opsForZSet().add("z-set", "z-set2", 2);
redisTemplate.opsForZSet().add("z-set", "z-set3", 3);
Cursor<ZSetOperations.TypedTuple<Object>> cursor = redisTemplate.opsForZSet().scan("z-set", ScanOptions.NONE);
while (cursor.hasNext()) {
ZSetOperations.TypedTuple<Object> next = cursor.next();
System.out.println(next.getValue() + ":" + next.getScore());
}
}
/**
* 保存经纬度
*/
@Test
public void geo() {
redisTemplate.opsForGeo().add("geo1", new Point(1,2), "北京");
redisTemplate.opsForGeo().add("geo1", new Point(80,80), "上海");
redisTemplate.opsForGeo().add("geo1", new Point(60,70), "深圳");
List<Point> position = redisTemplate.opsForGeo().position("geo1", "北京", "上海", "深圳");
if (position != null) {
for (Point point : position) {
System.out.println(point.toString());;
}
}
System.out.println(redisTemplate.opsForGeo().distance("geo1", "北京", "上海").getMetric().getMultiplier());
redisTemplate.opsForGeo().remove("geo1", "北京");
}
/**
* 适用于 点赞功能
*/
@Test
public void hyperLogLog() {
redisTemplate.opsForHyperLogLog().add("HyperLogLog", "a", "b" , "c", "d", "a");
redisTemplate.opsForHyperLogLog().add("HyperLogLog2", "a");
redisTemplate.opsForHyperLogLog().add("HyperLogLog2", "z");
System.out.println(redisTemplate.opsForHyperLogLog().size("HyperLogLog"));
System.out.println(redisTemplate.opsForHyperLogLog().size("HyperLogLog2"));
}
评论区