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.core.*;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@SpringBootTest
class SpringbootRedisServiceApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
/**
* String类型
* GET 获取存储在给定键中的值 GET name
* SET 设置存储在给定键中的值 SET name value
* DEL 删除存储在给定键中的值 DEL name
* INCR 将键存储的值加1 INCR key
* DECR 将键存储的值减1 DECR key
* INCRBY 将键存储的值加上整数 INCRBY key amount
* DECRBY 将键存储的值减去整数 DECRBY key amount
*/
@Test
void String(){
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name","xuxiang");
Object name = valueOperations.get("name");
System.out.println("name:"+name);
valueOperations.getAndDelete("name");
Object name_z = valueOperations.get("name");
System.out.println("name:"+name_z);
valueOperations.set("age",18);
Object age = valueOperations.get("age");
System.out.println(age);
Long age1 = valueOperations.increment("age");
System.out.println(valueOperations.get("age"));
System.out.println(age1);
Long age2 = valueOperations.decrement("name");
System.out.println(age2);
System.out.println(valueOperations.get("age"));
valueOperations.increment("age",100);
System.out.println(valueOperations.get("age"));
}
/**
* list类型 双端链表
* RPUSH 将给定值推入到列表右端 RPUSH key value
* LPUSH 将给定值推入到列表左端 LPUSH key value
* RPOP 从列表的右端弹出一个值,并返回被弹出的值 RPOP key
* LPOP 从列表的左端弹出一个值,并返回被弹出的值 LPOP key
* LRANGE 获取列表在给定范围上的所有值 LRANGE key 0 -1
* LINDEX 通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 LINEX key index
*/
@Test
void list(){
String[] user1 = new String[]{"1","pan_junbiao的博客","您好,欢迎访问 pan_junbiao的博客"};
String[] user2 = new String[]{"2","pan_junbiao的博客","https://blog.csdn.net/pan_junbiao"};
String[] user3 = new String[]{"3","pan_junbiao的博客","您好,欢迎访问 pan_junbiao的博客"};
ListOperations listOperations = redisTemplate.opsForList();
listOperations.rightPushAll("user1",user1);
listOperations.rightPushAll("user2",user2);
//List user11 = listOperations.range("user1", 0, -1);
Long user12 = listOperations.rightPushAll("user1", user3);
System.out.println(user12);
System.out.println(listOperations.range("user1", 0, -1));
}
/**
* set集合 不能重复 无序 hash结构
* SADD 向集合添加一个或多个成员 SADD key value
* SCARD 获取集合的成员数 SCARD key
* SMEMBER 返回集合中的所有成员 SMEMBER key member
* SISMEMBER 判断 member 元素是否是集合 key 的成员 SISMEMBER key member
*/
@Test
void set(){
String[] user1 = new String[]{"11111","22222222222","33333333333333333"};
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("setKey", "1","pan_junbiao的博客","您好,欢迎访问 pan_junbiao的博客");
setOperations.add("setKey", user1);
System.out.println(setOperations.randomMember("setKey"));
List setKey = setOperations.randomMembers("setKey", 10);
Set setKey1 = setOperations.distinctRandomMembers("setKey", setOperations.size("setKey"));
System.out.println(setKey1);
System.out.println(setOperations.size("setKey"));
System.out.println(setOperations.isMember("setKey","22222222"));
}
/**
* ZADD 将一个带有给定分值的成员添加到哦有序集合里面 ZADD zset-key 178 member1
* ZRANGE 根据元素在有序集合中所处的位置,从有序集合中获取多个元素 ZRANGE zset-key 0-1 withccores
* ZREM 如果给定元素成员存在于有序集合中,那么就移除这个元素 ZREM zset-key member1
*/
@Test
void zset(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zSetKey","1",1552);
zSetOperations.add("zSetKey","2",121);
zSetOperations.add("zSetKey","3",11);
zSetOperations.add("zSetKey","44",1211);
zSetOperations.add("zSetKey","5",121);
Set set = zSetOperations.distinctRandomMembers("zSetKey", zSetOperations.size("zSetKey"));
zSetOperations.remove("zSetKey","44");
System.out.println(set);
Set set1 = zSetOperations.distinctRandomMembers("zSetKey", zSetOperations.size("zSetKey"));
System.out.println(set1);
}
/**
* kay-value类型 无序 不能重复
* HSET 添加键值对 HSET hash-key sub-key1 value1
* HGET 获取指定散列键的值 HGET hash-key key1
* HGETALL 获取散列中包含的所有键值对 HGETALL hash-key
* HDEL 如果给定键存在于散列中,那么就移除这个键 HDEL hash-key sub-key1
*/
@Test
void hash(){
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("hashKey","name","xuxiang");
hashOperations.put("hashKey","1","1");
hashOperations.put("hashKey","2","xuxiang");
hashOperations.put("hashKey","3","1");
Object o = hashOperations.get("hashKey", "name");
System.out.println(o);
Object o1 = hashOperations.get("hashKey", "1");
System.out.println(o1);
}
/**
* 统计数据
*/
@Test
void hyperLogLog(){
HyperLogLogOperations hyperLogLogOperations = redisTemplate.opsForHyperLogLog();
hyperLogLogOperations.add("hyper",1,3,9,15,23);
hyperLogLogOperations.add("hyper1",3,9,23,8,695,4);
Long union = hyperLogLogOperations.union("hyper", "hyper1");
System.out.println(union);
System.out.println(hyperLogLogOperations.size("hyper"));
}
/**
* 地图
*/
@Test
void geo(){
GeoOperations geoOperations = redisTemplate.opsForGeo();
}
/**
* 集群操作
*/
@Test
void cluster(){
ClusterOperations clusterOperations = redisTemplate.opsForCluster();
}
/**
* 流
*/
@Test
void stream(){
StreamOperations streamOperations = redisTemplate.opsForStream();
}
/**
* 位示图-只有0 和 1 两个状态。
* 统计用户信息,活跃,不活跃! 登录,未登录! 打卡,不打卡! 两个状态的,都可以使用 Bitmaps
*/
@Test
void bigmap(){
ValueOperations valueOperations = redisTemplate.opsForValue();
Boolean sdfs = valueOperations.setBit("sdfs", 546, true);
System.out.println(valueOperations.getBit("sdfs",546));
}
}
springboot-redis基本用法
最新推荐文章于 2025-02-25 21:25:59 发布