2023-11-20
导入的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
package com.sky.test; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.core.*; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; /** * @author yangLiHua * @version 1.0 * @DATE 2023-11-20 15:09:48 */ @SpringBootTest public class SpringDataRedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void test(){ System.out.println(redisTemplate); ValueOperations valueObjects = redisTemplate.opsForValue(); HashOperations hashOperations = redisTemplate.opsForHash(); ListOperations listOperations = redisTemplate.opsForList(); SetOperations setOperations = redisTemplate.opsForSet(); ZSetOperations zSetOperations = redisTemplate.opsForZSet(); } /** * 操作字符串的数据 */ @Test public void testString(){ // set get setex setnx redisTemplate.opsForValue().set("city","深圳"); String city = (String)redisTemplate.opsForValue().get("city"); System.out.println("city = " + city); redisTemplate.opsForValue().set("code","856485",3, TimeUnit.MINUTES); redisTemplate.opsForValue().setIfAbsent("lock","1"); redisTemplate.opsForValue().setIfAbsent("lock","2"); } /** * 操作哈希类型的数据 */ @Test public void testHash(){ // hset hget hdel hkeys hvals HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("100","name","tom"); hashOperations.put("100","age","22"); String name = (String)hashOperations.get("100", "name"); System.out.println("name = " + name); Set keys = hashOperations.keys("100"); System.out.println("keys = " + keys); List values = hashOperations.values("100"); System.out.println("values = " + values); hashOperations.delete("100","age"); } /** * 操作列表类型的数据 */ @Test public void testList(){ //lpush lrange rpop llen ListOperations listOperations = redisTemplate.opsForList(); listOperations.leftPushAll("mylist","a","b","c"); listOperations.leftPush("mylist","d"); List mylist = listOperations.range("mylist",0,-1); System.out.println("mylist = " + mylist); listOperations.rightPop("mylist"); Long mylist1 = listOperations.size("mylist"); System.out.println("mylist1 = " + mylist1); } /** * 操作集合类型的数据 */ @Test public void testSet(){ //sadd smembers scard sinter sunion srem SetOperations setOperations = redisTemplate.opsForSet(); setOperations.add("set1","a","b","c","d"); setOperations.add("set2","a","b","c","d"); Set set1 = setOperations.members("set1"); System.out.println("set1 = " + set1); Long size = setOperations.size("set1"); System.out.println("size = " + size); Set intersect = setOperations.intersect("set1", "set2"); System.out.println("intersect = " + intersect); Set union = setOperations.union("set1", "set2"); System.out.println("union = " + union); setOperations.remove("set1","a","b"); } /** * 操作有序集合类型的数据 */ @Test public void testZSet(){ //zadd zrange zincrby zrem ZSetOperations zSetOperations = redisTemplate.opsForZSet(); zSetOperations.add("zset1","a",10); zSetOperations.add("zset1","b",12); zSetOperations.add("zset1","c",9); Set zset1 = zSetOperations.range("zset1", 0, -1); System.out.println("zset1 = " + zset1); zSetOperations.incrementScore("zset","c",10); zSetOperations.remove("zset1","a","b"); } /** * 通用命令操作 */ @Test public void testCommon(){ //keys exists type del Set keys = redisTemplate.keys("*"); System.out.println("keys = " + keys); Boolean name = redisTemplate.hasKey("name"); Boolean set1 = redisTemplate.hasKey("set1"); for (Object key : keys) { DataType type = redisTemplate.type(key); System.out.println("type = " + type); } redisTemplate.delete("mylist"); } }
标签:keys,redis,idea,System,set1,应用,println,redisTemplate,out From: https://www.cnblogs.com/isDaHua/p/17844269.html