1、SpringBoot整合Redis
Spring Boot Data(数据) Redis中提供了RedisTemplate和SringRedisTemplate,其中StringRedisTemplate是RedisTemplate的子类,两个方法基本一致,不同之处主要体现在操作的数据类型不同,RedisTemplate中的两个泛型都是Object,意味着存储的key和value都可以是一个对象, 而 StringRedisTemplate的两个泛型都是String,意味着StringRedisTemplate的key和value都只能是字符。
注意:使用RedisTemplate默认是将对象序列化到Redis中,所以放入的对象必须实现对象序列化接口
1.1、引入依赖
<!--引入redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.2、配置application.properties
#redis
spring.redis.host=192.168.56.105
spring.redis.port=7000
spring.redis.database=0
1.3、StringRedisTemplate
样例:
@SpringBootTest(classes = RedisBootApplication.class)
public class RedisTest {
//注入StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
//操作redi s中字符串opsForValue 实际操作就是redi s中string类型
@Test
public void testString() {
stringRedisTemplate.opsForValue().set("name", "zhangsan");
String value = stringRedisTemplate.opsForValue().get("name");
System.out.println("value=" + value);
}
//操作redis中key相关
@Test
public void testKey() {
//stringRedi sTemplate . delete ("name") ;//删除一个 key
Boolean hasKey = stringRedisTemplate.hasKey("name"); //判断某个key是否存在
System.out.println(hasKey);
DataType name = stringRedisTemplate.type("name"); //判断key所对应值的类型
System.out.println(name);
Set<String> keys = stringRedisTemplate.keys("*"); //获取redis中所有key
keys.forEach(key -> System.out.println("key = " + key));
Long expire = stringRedisTemplate . getExpire( "age"); //获取key超时时间-1 永不超时-2 key不存在
System.out.println (expire);
stringRedisTemplate . randomKey();//在redi s中随机获取一个key
//stringRedisTemplate. rename ("age", "age1");//修改key名字要求key必须存在不存在报错
//stringRedisTemplate . renameIfAbsent ( "name", "name1") ;//修改key名字判断key是否存在
stringRedisTemplate.move ( " name1", 1); //移动key到指定库
}
//操作redis中list类型 opsForList实际操作就是redis中list类型
@Test
public void testList() {
stringRedisTemplate.opsForList().leftPush("names", "小陈");//创建一一个列表并放入- 个元素
stringRedisTemplate.opsForList().leftPushAll("names", "小陈", "小张", "小王");//创建一个列表放入多个元素
List<String> names = new ArrayList<>();
names.add("xiaoming");
names.add("xiaosan");
stringRedisTemplate.opsForList().leftPushAll("names", names);
//遍历list
stringRedisTemplate.opsForList().range("names",0,-1).forEach(value-> System.out.println("name="+value));
stringRedisTemplate.opsForList().trim("names",1,3);
}
//操作redis中aset类型opsForSet 实际操作就是redi s中set类型
@Test
public void testZset() {
stringRedisTemplate.opsForZSet().add("zsets", "小黑", 20);//创建并放入元素
Set<String> zsets = stringRedisTemplate.opsForZSet().range("zsets", 0, -1);//指定范围查询
zsets.forEach(value -> System.out.println(value));
System.out.println("================");
Set<ZSetOperations.TypedTuple<String>> zsets1 = stringRedisTemplate.opsForZSet().rangeByScoreWithScores("zsets",0,100 );
zsets1.forEach(typedTuple -> {
System.out.println(typedTuple.getValue());
System.out.println(typedTuple.getScore());
});
}
//操作redi s中Hash类型opsForHash实际操作就是redi s中Hash类型
@Test
public void testHash() {
stringRedisTemplate.opsForHash().put("maps", "name", "张三");//创建一个hash类型并放入key value
Map<String, String> map = new HashMap<String, String>();
map.put("age", "12");
map.put("bir", "2012-12-12");
stringRedisTemplate.opsForHash().putAll("maps", map);//放入多个key value
List<Object> values = stringRedisTemplate.opsForHash().multiGet("maps", Arrays.asList("name", "age"));
values.forEach(value-> System.out.println(value));
String value = (String) stringRedisTemplate.opsForHash().get("maps", "name"); //获取hash中某个key的值
List<Object> vals = stringRedisTemplate.opsForHash().values("maps"); //获取所有values
Set<Object> keys = stringRedisTemplate.opsForHash().keys("maps");//获取所有keys
}
}
1.4、RedisTemplate
@SpringBootTest(classes = RedisBootApplication.class)
public class TestRedisTemples {
//注入RedisTemplate key object Value object对象序列化 name new User () name序列化对象序列化结果
@Autowired
private RedisTemplate redisTemplate;
//opsEREX8X Value String List Set Eset hash
@Test
public void testRedisTemplate() {
/**
redi sTemplate对象中key 和value 的序列化都是JdkSeriali zationRedisSerializer
key: string
*
value: object
修改默认key序列化方案: key StringRedisSerializer
*/
//修改key序列化方案string类型序列
redisTemplate.setKeySerializer (new StringRedisSerializer()) ;
//修改hash key 序列化方案
redisTemplate.setHashKeySerializer (new StringRedisSerializer()) ;
User user = new User () ;
user.setId(UUID.randomUUID().toString()).setName ("小陈") . setAge (23) .setBir (new Date()) ;
redisTemplate.opsForValue() .set ("user", user) ;
User user1 =(User) redisTemplate.opsForValue().get("user");
System.out.println(user1);
redisTemplate.opsForList() .leftPush("list",user);
redisTemplate.opsForSet() .add("set", user) ;
redisTemplate.opsForZSet() .add("zset",user, 10);
redisTemplate.opsForHash() .put("map" , "name",user);
}
}
1.5、绑定key
@SpringBootTest(classes = RedisBootApplication.class)
public class TestBoundApi {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
//spring data 为了方便我们对redis进行更友好的操作因此有提供了bound api 简化操作
@Test
public void testBound() {
redisTemplate.setKeySerializer (new StringRedisSerializer()) ;
redisTemplate.setHashKeySerializer (new StringRedisSerializer()) ;
//redisTemplate stringRedisTemplate 将一 个key多次操作进行绑定对key绑定
/*stringRedisTemplate . opsForValue() . set ("name" , "zhangsan") ;
stringRedisTemplate . opsForValue () . append ("name", "是一个好人");
String s = stringRedisTemplate . opsForValue() .get ("name") ;
System. out.println(s) ;*/
//对字符串类型key进行绑定后续所有操作都是基于这个key的操作
BoundValueOperations<String, String> nameValueOperations = stringRedisTemplate.boundValueOps ( "name") ;
nameValueOperations.set ("zhangsan");
nameValueOperations.append("是一个好人");
String s1 = nameValueOperations.get();
System. out. println(s1);
//对list set: zset hash
BoundListOperations<String, String> listsOperations = stringRedisTemplate.boundListOps( "lists") ;
listsOperations. leftPushAll( "张E", "李四", "小陈") ;
List<String> lists = listsOperations. range(0,-1) ;
lists. forEach(list-> System. out. println(list)) ;
/*stringRedisTemplate.boundHashOps();
stringRedisTemplate.boundSetOps();
stringRedisTemplate.boundZSetOps()*/
/*1.针对于日后处理key value都是string 使用stringRedisTemplate
2.针对于日后处理的key value 存在对象使用Redi sTemplate
3.针对于同一个key多次操作可以使用boundXXxOps() Value List Set ?set Hash的api简化书写*/
}
}