目录
- 引入Redis依赖
- StringRedisTemplate
- 使用String
- 使用List
- 使用Set
- 使用hash
- 使用zset
引入Redis依赖
StringRedisTemplate
此处RedisTemplate是把这些操作Redis的方法,分成了几个类别,分门别类的来组织的。
此处提供的一些接口风格,和原生的Redis命令就有一定的差异。
使用String
@GetMapping("/testString")@ResponseBodypublic String testString() {redisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须 return 语句,返回个东西connection.flushAll();return null;});redisTemplate.opsForValue().set("key", "111");redisTemplate.opsForValue().set("key2", "222");redisTemplate.opsForValue().set("key3", "333");String value = redisTemplate.opsForValue().get("key");System.out.println("value:" + value);return "OK";}
使用List
@GetMapping("/testList")@ResponseBodypublic String testList() {// 先清除之前的数据.redisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须 return 语句,返回个东西connection.flushAll();return null;});redisTemplate.opsForList().leftPush("key", "111");redisTemplate.opsForList().leftPush("key", "222");redisTemplate.opsForList().leftPush("key", "333");String value = redisTemplate.opsForList().rightPop("key");System.out.println("value:" + value);value = redisTemplate.opsForList().rightPop("key");System.out.println("value:" + value);value = redisTemplate.opsForList().rightPop("key");System.out.println("value:" + value);return "OK";}
使用Set
@GetMapping("/testSet")@ResponseBodypublic String testSet() {redisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须 return 语句,返回个东西connection.flushAll();return null;});redisTemplate.opsForSet().add("key", "111", "222", "333");Set<String> result = redisTemplate.opsForSet().members("key");System.out.println("result:" + result);Boolean exists = redisTemplate.opsForSet().isMember("key", "111");System.out.println("exists:" + exists);Long count = redisTemplate.opsForSet().size("key");System.out.println("count:" + count);redisTemplate.opsForSet().remove("key", "111", "222");result = redisTemplate.opsForSet().members("key");System.out.println("result:" + result);return "OK";}
使用hash
@GetMapping("/testHash")@ResponseBodypublic String testHash() {redisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须 return 语句,返回个东西connection.flushAll();return null;});redisTemplate.opsForHash().put("key", "f1", "111");redisTemplate.opsForHash().put("key", "f2", "222");redisTemplate.opsForHash().put("key", "f3", "333");String value = (String) redisTemplate.opsForHash().get("key", "f1");System.out.println("value:" + value);Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");System.out.println("exists:" + exists);redisTemplate.opsForHash().delete("key", "f1", "f2");Long size = redisTemplate.opsForHash().size("key");System.out.println("size:" + size);value = (String) redisTemplate.opsForHash().get("key", "f1");System.out.println("value:" + value);return "OK";}
使用zset
@GetMapping("/testZSet")@ResponseBodypublic String testZSet() {redisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须 return 语句,返回个东西connection.flushAll();return null;});redisTemplate.opsForZSet().add("key", "zhangsan", 50.0);redisTemplate.opsForZSet().add("key", "lisi", 80.0);redisTemplate.opsForZSet().add("key", "wangwu", 100.0);Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);System.out.println("members:" + members);Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);System.out.println("membersWithScore:" + membersWithScore);Double score = redisTemplate.opsForZSet().score("key", "zhangsan");System.out.println("score:" + score);redisTemplate.opsForZSet().remove("key", "zhangsan");Long size = redisTemplate.opsForZSet().size("key");System.out.println("size:" + size);Long rank = redisTemplate.opsForZSet().rank("key", "lisi");System.out.println("rank:" + rank);return "OK";}