- yaml配置信息
spring: application: name: springdataredis_demo #Redis相关配置 redis: host: localhost port: 6379 #password: 123456 database: 0 #操作的是0号数据库 jedis: #Redis连接池配置 pool: max-active: 8 #最大连接数 max-wait: 1ms #连接池最大阻塞等待时间 max-idle: 4 #连接池中的最大空闲连接 min-idle: 0 #连接池中的最小空闲连接
- 配置Redis配置类解决序列化问题
package com.itheima.config;import org.springframework.cache.annotation.CachingConfigurerSupport;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;/** * Redis配置类 */@Configurationpublic class RedisConfig extends CachingConfigurerSupport { // 我们只需要对key进行序列化即可,我们在使用keys *查看key时可以直接查看 // 针对value,我们存入时会进行序列化,在我们采用方法读取时同样会进行反序列化,所以无需优化 @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); //默认的Key序列化器为:JdkSerializationRedisSerializer redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; }}
- 启动类书写
package com.itheima;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class,args); }}
- 我们采用测试的形式给出所有案例
package com.itheima.test;import org.junit.Test;import org.junit.runner.RunWith;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 org.springframework.test.context.junit4.SpringRunner;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;@SpringBootTest@RunWith(SpringRunner.class)public class SpringDataRedisTest { // 我们在配置了yaml配置后,就可以自动装配RedisTemplate供我们使用 // 我们采用redisTemplate的方法来获得其他五类实体,也可以直接用redisTemplate调用通用命令 @Autowired private RedisTemplate redisTemplate; /** * 操作String类型数据 */ @Test public void testString(){ // redisTemplate.opsForValue()获得ValueOperations // SET redisTemplate.opsForValue().set("city123","beijing"); // GET String value = (String) redisTemplate.opsForValue().get("city123"); System.out.println(value); // 这里使用的是SETEX,注意时间是long型的 redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS); // 这里使用的是SETNX Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing"); System.out.println(aBoolean); } /** * 操作Hash类型数据 */ @Test public void testHash(){ HashOperations hashOperations = redisTemplate.opsForHash(); //存值 HSET hashOperations.put("002","name","xiaoming"); hashOperations.put("002","age","20"); hashOperations.put("002","address","bj"); //取值 HGET String age = (String) hashOperations.get("002", "age"); System.out.println(age); //获得hash结构中的所有字段 HEKYS Set keys = hashOperations.keys("002"); for (Object key : keys) { System.out.println(key); } //获得hash结构中的所有值 HVALS List values = hashOperations.values("002"); for (Object value : values) { System.out.println(value); } } /** * 操作List类型的数据 */ @Test public void testList(){ ListOperations listOperations = redisTemplate.opsForList(); //存值 LPUSH listOperations.leftPush("mylist","a"); listOperations.leftPushAll("mylist","b","c","d"); //取值 RPOP List<String> mylist = listOperations.range("mylist", 0, -1); for (String value : mylist) { System.out.println(value); } //获得列表长度 llen Long size = listOperations.size("mylist"); int lSize = size.intValue(); for (int i = 0; i < lSize; i++) { //出队列 String element = (String) listOperations.rightPop("mylist"); System.out.println(element); } } /** * 操作Set类型的数据 */ @Test public void testSet(){ SetOperations setOperations = redisTemplate.opsForSet(); //存值 SADD setOperations.add("myset","a","b","c","a"); //取值 SMEMBERS Set<String> myset = setOperations.members("myset"); for (String o : myset) { System.out.println(o); } //删除成员 SREM setOperations.remove("myset","a","b"); //取值 SMEMBERS myset = setOperations.members("myset"); for (String o : myset) { System.out.println(o); } } /** * 操作ZSet类型的数据 */ @Test public void testZset(){ ZSetOperations zSetOperations = redisTemplate.opsForZSet(); //存值 ZADD zSetOperations.add("myZset","a",10.0); zSetOperations.add("myZset","b",11.0); zSetOperations.add("myZset","c",12.0); zSetOperations.add("myZset","a",13.0); //取值 ZRANGE Set<String> myZset = zSetOperations.range("myZset", 0, -1); for (String s : myZset) { System.out.println(s); } //修改分数 ZINCRBY zSetOperations.incrementScore("myZset","b",20.0); //取值 ZRANGE myZset = zSetOperations.range("myZset", 0, -1); for (String s : myZset) { System.out.println(s); } //删除成员 ZREM zSetOperations.remove("myZset","a","b"); //取值 ZRANGE myZset = zSetOperations.range("myZset", 0, -1); for (String s : myZset) { System.out.println(s); } } /** * 通用操作,针对不同的数据类型都可以操作 */ @Test public void testCommon(){ //获取Redis中所有的key KEYS * Set<String> keys = redisTemplate.keys("*"); for (String key : keys) { System.out.println(key); } //判断某个key是否存在 EXISTS Boolean itcast = redisTemplate.hasKey("itcast"); System.out.println(itcast); //删除指定key DEL redisTemplate.delete("myZset"); //获取指定key对应的value的数据类型 TYPE DataType dataType = redisTemplate.type("myset"); System.out.println(dataType.name()); }}
经验总结扩展阅读
-
2022年10月24日是出火的黄道吉日吗 2022年农历九月廿九宜出火吗
-
-
-
-
-
亚麻衣服怎么洗不缩水,亚麻面料的衣服这样清洗不会缩水?
-
-
-
-
-
-
-
-
导语:心仪的他就在眼前 心仪的男人不唐突的4种搭讪方式,教你把握机会,迈出追爱第一步
-
-
再过4天,鸿运当头,高人一等,喜事接踵,横财不断,尽享富贵
-
-
六款2023年最佳手机推荐 手机好用排行榜2023
-
-
疾病 40岁是肠道疾病“高发期”,若想肠道更健康,3个坏习惯要改掉