1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| @SpringBootTest public class SpringDataRedisTest {
@Autowired private RedisTemplate redisTemplate;
@Test public void testRedisTemplate() { ValueOperations valueOperations = redisTemplate.opsForValue(); HashOperations hashOperations = redisTemplate.opsForHash(); ListOperations listOperations = redisTemplate.opsForList(); SetOperations setOperations = redisTemplate.opsForSet(); ZSetOperations zSetOperations = redisTemplate.opsForZSet(); }
@Test public void testString() { redisTemplate.opsForValue().set("city", "beijing"); String city = (String) redisTemplate.opsForValue().get("city"); System.out.println(city); redisTemplate.opsForValue().set("code", "1234", 3, TimeUnit.MINUTES); redisTemplate.opsForValue().setIfAbsent("lock", "1"); redisTemplate.opsForValue().setIfAbsent("lock", "2"); }
@Test public void testHash() { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("100", "name", "tom"); hashOperations.put("100", "age", "20"); String name = (String) hashOperations.get("100", "name"); System.out.println(name); hashOperations.keys("100"); hashOperations.values("100"); hashOperations.delete("100", "age"); }
@Test public void testList() { ListOperations listOperations = redisTemplate.opsForList(); listOperations.leftPush("list", "a"); listOperations.leftPush("list", "b"); listOperations.leftPush("list", "c"); listOperations.rightPush("list", "d"); listOperations.rightPush("list", "e"); listOperations.rightPush("list", "f"); String value = (String) listOperations.leftPop("list"); System.out.println(value); listOperations.range("list", 0, -1); }
@Test public void testSet() { SetOperations setOperations = redisTemplate.opsForSet(); setOperations.add("set", "a", "b", "c", "d", "e"); setOperations.remove("set", "a", "b"); setOperations.members("set"); }
@Test public void testZSet() { ZSetOperations zSetOperations = redisTemplate.opsForZSet(); zSetOperations.add("zset", "a", 1); zSetOperations.add("zset", "b", 2); zSetOperations.add("zset", "c", 3); zSetOperations.add("zset", "d", 4); zSetOperations.add("zset", "e", 5); zSetOperations.range("zset", 0, -1); zSetOperations.remove("zset", "a", "b"); }
@Test public void testCommon() { Set keys = redisTemplate.keys("*"); System.out.println(keys);
Boolean city = redisTemplate.hasKey("city"); Boolean code = redisTemplate.hasKey("code");
for (Object key : keys) { DataType type = redisTemplate.type(key); System.out.println(type.name()); } redisTemplate.delete("lock"); } }
|