✨Redis结构体
Redis 有 5 种基础数据结构:
string (字符串) 、 list (列表) 、 hash (字典) 、 set (集合) 和 zset (有序集合)
🎊Spring Boot 使用Redis
概念补充:
Redis缓存穿透,缓存击穿,缓存雪崩
- 穿透: 不存在
- 击穿:一个热点的key失效了,这时大量的并发请求直接到达数据库.
- 雪崩:大量key同时失效
Redis常用配置参数
# Redis服务器地址
spring.redis.host=192.168.0.104
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器连接端口
spring.redis.port=6380
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=5
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=5
## 连接超时时间(毫秒)
spring.redis.timeout=30000
🎉String(字符串)
@SpringBootTest
class AppTests_String {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String key = "A";
@Test
public void test1() {
//设KEY为A,value为100,30秒过期
stringRedisTemplate.opsForValue().set(key, "100", 30, TimeUnit.SECONDS);
//获取A的值
String value = stringRedisTemplate.opsForValue().get(key);
System.out.println(value);
//拼接
stringRedisTemplate.opsForValue().set("B","aaa");
stringRedisTemplate.opsForValue().append("B", "--bbb");
System.out.println(stringRedisTemplate.opsForValue().get("B"));
//获取后删除
String b = stringRedisTemplate.opsForValue().getAndDelete("B");
System.out.println(b);
//加指定值,默认1
Long c = stringRedisTemplate.opsForValue().increment("C");
System.out.println(c);
Long c1 = stringRedisTemplate.opsForValue().increment("C", 9);
System.out.println(c1);
//setnx锁
Boolean d1 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
System.out.println(d1);
Boolean d2 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");
System.out.println(d2);
//设置声明周期
String d3 = stringRedisTemplate.opsForValue().getAndExpire("D", 60, TimeUnit.SECONDS);
System.out.println(d1);
//批量设置
Map<String,String> map = new HashMap<>();
map.put("F1","1");
map.put("F2","2");
map.put("F3","3");
stringRedisTemplate.opsForValue().multiSet(map);
//批量获取
List<String> keys = new ArrayList<>();
keys.add("F1");
keys.add("F2");
keys.add("F3");
List<String> values = stringRedisTemplate.opsForValue().multiGet(keys);
System.out.println(values);
}
}
🎉List(列表)
@SpringBootTest
class AppTests_List {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String name = "wangwu";
@Test
void test1() {
//左推
stringRedisTemplate.opsForList().leftPush(name, "aaa");
//批量左推
Long aLong = stringRedisTemplate.opsForList().leftPushAll(name, "aaa", "bbb", "ccc");
System.out.println(aLong);
//左弹
String v = stringRedisTemplate.opsForList().leftPop(name);
System.out.println(v);
//右弹,限时30秒后结束监听
String v1 = stringRedisTemplate.opsForList().rightPop(name, 30, TimeUnit.MINUTES);
System.out.println("--------------v1------------");
System.out.println(v1);
//一直监听,有值就弹出
//while (true) {
// String v2 = stringRedisTemplate.opsForList().rightPop(name, 10, TimeUnit.SECONDS);
// if (v2 != null) {
// System.out.println("------------获取到数据------------");
// System.out.println(v2);
// } else {
// System.out.println("-----------没有拉取到新数据,继续监听----------");
// }
//}
//长度
Long size = stringRedisTemplate.opsForList().size(name);
System.out.println(size);
}
}
🎉Set(集合)
@SpringBootTest
class AppTests_Set {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String name1 = "zhangsan";
private static final String name2 = "lisi";
@Test
void test1() {
//添加
stringRedisTemplate.opsForSet().add(name1, "张三", "李四", "王五");
stringRedisTemplate.opsForSet().add(name2, "张三丰", "李四", "王五");
//交集
Set<String> intersect = stringRedisTemplate.opsForSet().intersect(name1, name2);
//stringRedisTemplate.opsForSet().intersectAndStore()
System.out.println(intersect);
//并集
Set<String> union = stringRedisTemplate.opsForSet().union(name1, name2);
//stringRedisTemplate.opsForSet().unionAndStore()
System.out.println(union);
//差集
Set<String> difference = stringRedisTemplate.opsForSet().difference(name1, name2);
//stringRedisTemplate.opsForSet().differenceAndStore()
System.out.println(difference);
//是否存在
Boolean a = stringRedisTemplate.opsForSet().isMember(name1, "张三");
Boolean b = stringRedisTemplate.opsForSet().isMember(name1, "张三丰");
System.out.println(a);
System.out.println(b);
}
}
🎉ZSet(有序集合)
@SpringBootTest
class AppTests_Zset {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String name1 = "zhangsan";
@Test
void test1() {
stringRedisTemplate.opsForZSet().add(name1, "语文", 110);
stringRedisTemplate.opsForZSet().add(name1, "数学", 130);
stringRedisTemplate.opsForZSet().add(name1, "英语", 90);
//数量
Long size = stringRedisTemplate.opsForZSet().size(name1);
System.out.println(size);
//范围数量
Long count = stringRedisTemplate.opsForZSet().count(name1, 100, 150);
System.out.println(count);
//弹出最值
//ZSetOperations.TypedTuple<String> max = stringRedisTemplate.opsForZSet().popMax(name1);
//ZSetOperations.TypedTuple<String> min = stringRedisTemplate.opsForZSet().popMin(name1);
//System.out.println(max+"---"+min);
//范围内的值,根据分数升序
Set<String> strings = stringRedisTemplate.opsForZSet().rangeByScore(name1, 0, 150);
System.out.println(strings);
//排名
Long rank = stringRedisTemplate.opsForZSet().rank(name1, "数学");
System.out.println(rank);
//倒叙
Set<String> strings1 = stringRedisTemplate.opsForZSet().reverseRangeByScore(name1, 0, 150);
System.out.println(strings1);
}
}
🎉Hash(字典)
@SpringBootTest
class AppTests_Hash {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String name = "zhangsan";
private static final String name2 = "lisi";
@Test
void test1() {
//放
stringRedisTemplate.opsForHash().put(name, "20221107", "洛阳");
stringRedisTemplate.opsForHash().put(name, "20221108", "新乡");
stringRedisTemplate.opsForHash().put(name, "20221109", "郑州");
//取
Object o = stringRedisTemplate.opsForHash().get(name, "20221108");
System.out.println(o);
//所有value
List<Object> values = stringRedisTemplate.opsForHash().values(name);
System.out.println(values);
//所有hashKey
Set<Object> keys = stringRedisTemplate.opsForHash().keys(name);
System.out.println(keys);
//所有hashKey和value
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(name);
System.out.println(entries);
//是否存在指定hashKey
Boolean b = stringRedisTemplate.opsForHash().hasKey(name, "20221108-");
System.out.println(b);
// stringRedisTemplate.opsForHash().put(name,"20221109","开封");
//如果没有.则放
Boolean b1 = stringRedisTemplate.opsForHash().putIfAbsent(name, "20221110", "南阳");
System.out.println(b1);
//删除
// stringRedisTemplate.opsForHash().delete(name,"20221110");
//批量获取
List<Object> objects = stringRedisTemplate.opsForHash().multiGet(name, CollUtil.newArrayList("20221107", "20221109"));
System.out.println(objects);
//批量放
Map<String, String> map = new HashMap<>();
map.put("20221107", "驻马店");
map.put("20221108", "信阳");
map.put("20221109", "平顶山");
stringRedisTemplate.opsForHash().putAll(name2, map);
//加减
Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 5);
//Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 100);
//Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", -8);
System.out.println(qty);
//随机获取
List<Object> keys = stringRedisTemplate.opsForHash().randomKeys(name2, 2);
System.out.println(keys);
}
}
🎉BitMap(String)
@SpringBootTest
class AppTests_BitMap {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String name1 = "张三";
@Test
public void test1() {
stringRedisTemplate.opsForValue().setBit(name1, 1, true);
stringRedisTemplate.opsForValue().setBit(name1, 100, true);
stringRedisTemplate.opsForValue().setBit(name1, 365, true);
Boolean bit = stringRedisTemplate.opsForValue().getBit(name1, 1);
Boolean bit1 = stringRedisTemplate.opsForValue().getBit(name1, 2);
System.out.println(bit + "---" + bit1);
//原生统计范围内1/true的个数
RedisCallback<Long> callback = (connection) ->
connection.bitCount(name1.getBytes(StandardCharsets.UTF_8), 0, 366);
Long aLong = stringRedisTemplate.execute(callback);
System.out.println(aLong);
//stringRedisTemplate.opsForValue().getOperations().execute(callback);
}
}

















