Redis 简介
Redis 优势
Redis与其他key-value存储有什么不同?
Spring Data Redis
1.导入配置
2. 在yml中配置
3.配置Redis模板对象
测试的demo:
操作字符串类型的数据
操作哈希类型的数据
操作列表类型的数据
操作集合类型的数据
操作有序集合类型的数据
通用操作
Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。
Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。
org.springframework.boot spring-boot-starter-data-redis
spring:
redis:
host: localhost
port: 6379
database: 0-15都可以
package com.sky.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板对象...");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
@SpringBootTest
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate(){
System.out.println(redisTemplate);
ValueOperations valueOperations = 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("name","123"); //设置
String name = (String) redisTemplate.opsForValue().get("name");//获取
System.out.println(name);
redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);//设置带有定时器
redisTemplate.opsForValue().setIfAbsent("lock","1");//在指定的 key 不存在时,为 key 设置指定的值
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","20");
String name = (String) hashOperations.get("100", "name");//获取
System.out.println(name);
Set keys = hashOperations.keys("100"); //获取所有键
System.out.println(keys);
List values = hashOperations.values("100"); //获取所有值
System.out.println(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);
listOperations.rightPop("mylist");//列表右侧移除
Long size = listOperations.size("mylist");//获取列表长度
System.out.println(size);
}
@Test
public void testSet() {
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c");//插入
setOperations.add("set2","a","y","z");
Set members = setOperations.members("set1");//获取所有元素
System.out.println(members);
Long size = setOperations.size("set1");//获取集合长度
System.out.println(size);
Set intersect = setOperations.intersect("set1", "set2");//获取集合的交集
System.out.println(intersect);
Set union = setOperations.union("set1", "set2");//获取集合的并集
System.out.println(union);
setOperations.remove("set1","a","b");//删除元素
}
@Test
public void testZset(){
//zadd zreange 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);
zSetOperations.incrementScore("zset1","c",10);//在指定元素上加n
zSetOperations.remove("zset1","a","b");//删除
}
@Test
public void testCommon(){
//keys exists type del
Set keys = redisTemplate.keys("*");//查询所有的key
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");//查询key是否存在
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key : keys) {
DataType type = redisTemplate.type(key);//查看key具体类型
System.out.println(type.name());
}
redisTemplate.delete("mylist");//删除key
}
凌晨四点,我看海棠花未眠。