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
| @RunWith(SpringRunner.class) @SpringBootTest(classes = IttestApplication.class) public class RedisTest {
@Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
@Test public void testRedisTemplate() { String prefix = "maxzhao:redisTemplate:"; HashOperations hashOperations = redisTemplate.opsForHash(); ValueOperations valueOps = redisTemplate.opsForValue(); ListOperations listOps = redisTemplate.opsForList(); SetOperations setOps = redisTemplate.opsForSet(); ZSetOperations zSetOps = redisTemplate.opsForZSet(); GeoOperations geoOperations = redisTemplate.opsForGeo(); ClusterOperations clusterOperations = redisTemplate.opsForCluster(); Map map = Arrays.stream(new String[]{"a", "B"}).collect(Collectors.toMap(Function.identity(), Function.identity())); hashOperations.putAll(prefix + "hash", map); }
@Test public void testStringRedisTemplate() { String prefix = "maxzhao:stringRedisTemplate:"; HashOperations hashOperations = stringRedisTemplate.opsForHash(); hashOperations.putAll(prefix + "hash", Arrays.stream(new String[]{"a", "b"}).collect(Collectors.toMap(Function.identity(), Function.identity()))); hashOperations.putAll(prefix + "hash", Arrays.stream(new String[]{"c", "d"}).collect(Collectors.toMap(Function.identity(), Function.identity()))); hashOperations.putAll(prefix + "hash", Arrays.stream(new String[]{"e", "f"}).collect(Collectors.toMap(Function.identity(), Function.identity()))); hashOperations.put(prefix + "hash", "max", "maxvalue"); hashOperations.get(prefix + "hash", "max"); hashOperations.delete(prefix + "hash", "f"); hashOperations.delete(prefix + "hash", "c", "d"); hashOperations.hasKey(prefix + "hash", "max"); hashOperations.values(prefix + "hash"); ValueOperations valueOperations = stringRedisTemplate.opsForValue(); valueOperations.set(prefix + "value", "value"); valueOperations.set(prefix + "value", "valueTest"); valueOperations.get(prefix + "value");
ListOperations listOps = stringRedisTemplate.opsForList();
listOps.leftPush(prefix + "list", "A"); listOps.leftPush(prefix + "list", "B"); listOps.rightPush(prefix + "list", "C", "D"); listOps.leftPush(prefix + "list", "C"); listOps.leftPush(prefix + "list", "D"); listOps.range(prefix + "list", 0, listOps.size(prefix + "list")); listOps.leftPop(prefix + "list"); listOps.leftPush(prefix + "list2", "A"); listOps.leftPush(prefix + "list2", "B"); listOps.rightPush(prefix + "list2", "C");
SetOperations setOps = stringRedisTemplate.opsForSet(); setOps.add(prefix + "set", "A"); setOps.add(prefix + "set", "A"); setOps.add(prefix + "set", "B"); setOps.difference(prefix + "set", "A"); setOps.isMember(prefix + "set", "A"); setOps.isMember(prefix + "set", "C"); setOps.members(prefix + "set"); setOps.pop(prefix + "set"); setOps.add(prefix + "set", "A","B", "C", "D", "E"); setOps.pop(prefix + "set", 2); setOps.add(prefix + "set", "A","B", "C", "D", "E"); setOps.move(prefix + "set", "D", "A"); setOps.move(prefix + "set", "C", prefix + "set1"); setOps.remove(prefix + "set", "C", "D"); ZSetOperations zSetOps = stringRedisTemplate.opsForZSet();
GeoOperations geoOperations = stringRedisTemplate.opsForGeo(); ClusterOperations clusterOperations = stringRedisTemplate.opsForCluster(); System.out.println("===================="); } }
|