1.在SpringBoot2.x版本之后,以下依赖默认使用Lettuce实现对redis的操纵。
<!-- spring-data-redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.添加对象池依赖
<!-- commons-pool2 对象池依赖--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
3.配置redis和哨兵。前提在服务器端配置好主从复用和哨兵模式,并放行相应的端口。
spring.redis.host=服务器ip spring.redis.port=redis主服务器端口 spring.redis.password=redis主服务器登录密码 #默认redis数据库 spring.redis.database=0 spring.redis.timeout=10000ms spring.redis.lettuce.pool.max-active=1024 spring.redis.lettuce.pool.max-wait=10000ms spring.redis.lettuce.pool.max-idle=200 spring.redis.lettuce.pool.min-idle=5 #哨兵模式 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=ip:port,ip:port,....
4.防止存储到redis中的键值对是二进制字符串形式,需进行序列化操作
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); //为string类型的key设置序列化 redisTemplate.setKeySerializer(new StringRedisSerializer()); //为string类型的value设置序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //为hash类型的key设置序列化 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //为hash类型的value设置序列化 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(lettuceConnectionFactory); return redisTemplate; } // @Bean // public RedisSentinelConfiguration redisSentinelConfiguration() { // RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration() // .master("mymaster") // .sentinel("", 26379) // .sentinel("", 26380) // .sentinel("", 26381); // redisSentinelConfiguration.setPassword(""); // return redisSentinelConfiguration; // } }
5.通过Lettuce对5种数据类型的操作方式
1 @SpringBootTest 2 3 public class DemoApplicationTests { 4 5 @Autowired 6 private RedisTemplate redisTemplate; 7 8 // @Autowired 9 // private StringRedisTemplate stringRedisTemplate; 10 11 /** 12 * 测试连接 13 */ 14 @Test 15 public void initConn() { 16 ValueOperations ops = redisTemplate.opsForValue(); 17 ops.set("name", "zhangsan"); 18 19 // ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue(); 20 // stringStringValueOperations.set("sex", "男"); 21 // System.out.println(stringStringValueOperations.get("sex")); 22 } 23 24 /** 25 * 测试序列化 26 */ 27 @Test 28 public void testSerial() { 29 User user = new User(); 30 user.setId(1); 31 user.setName("zhangsan"); 32 user.setAge(20); 33 ValueOperations ops = redisTemplate.opsForValue(); 34 ops.set("user", user); 35 Object user1 = ops.get("user"); 36 System.out.println(user1); 37 } 38 39 /** 40 * 操作string 41 */ 42 @Test 43 public void testString() { 44 ValueOperations operations = redisTemplate.opsForValue(); 45 //添加一条数据 46 operations.set("name", "zhangsan"); 47 //获取一条数据 48 String name = (String) operations.get("name"); 49 System.out.println(name); 50 //层级目录 51 operations.set("user:01", "lisi"); 52 operations.set("user:02", "wangwu"); 53 //添加多条数据 54 Map<String, String> map = new HashMap<>(); 55 map.put("age", "20"); 56 map.put("address", "hs"); 57 operations.multiSet(map); 58 //获取多条数据 59 List<String> keys = new ArrayList<>(); 60 keys.add("name"); 61 keys.add("age"); 62 keys.add("address"); 63 List list = operations.multiGet(keys); 64 list.forEach(System.out::println); 65 //删除数据 66 redisTemplate.delete("name"); 67 } 68 69 /** 70 * 操作 hash 71 */ 72 @Test 73 public void testHash() { 74 HashOperations hashOperations = redisTemplate.opsForHash(); 75 //添加一条数据 76 hashOperations.put("user", "name", "zhangsan"); 77 //获取一条数据 78 String name = (String) hashOperations.get("user", "name"); 79 System.out.println(name); 80 //添加多条数据 81 Map<String, String> map = new HashMap<>(); 82 map.put("age", "20"); 83 map.put("address", "hs"); 84 hashOperations.putAll("user", map); 85 //获取多条数据 86 List<String> keys = new ArrayList<>(); 87 keys.add("name"); 88 keys.add("age"); 89 keys.add("address"); 90 List list = hashOperations.multiGet("user", keys); 91 list.forEach(System.out::println); 92 //获取所有数据 93 Map<String, String> entries = hashOperations.entries("user"); 94 entries.entrySet().forEach(e -> { 95 System.out.println(e.getKey() + "-->" + e.getValue()); 96 }); 97 hashOperations.delete("user", "name", "age"); 98 } 99 100 /** 101 * 操作列表 102 */ 103 @Test 104 public void testList() { 105 ListOperations listOperations = redisTemplate.opsForList(); 106 //左添加 107 listOperations.leftPush("students", "wangwu"); 108 listOperations.leftPush("students", "lisi"); 109 //在v参数左边插入一个v1参数 110 listOperations.leftPush("students","zhaoliu", "zhangsan"); 111 //右添加 112 listOperations.rightPush("students", "zhaoliu"); 113 listOperations.rightPush("students", "zhangsan", "aaa"); 114 //获取数据 115 List list = listOperations.range("students", 0, 3); 116 list.forEach(System.out::println); 117 //获取总条数 118 Long size = listOperations.size("students"); 119 System.out.println(size); 120 121 //删除1个 lisi 122 listOperations.remove("students", 1, "lisi"); 123 //删除最左边的 124 listOperations.leftPop("students"); 125 //删除最右边的 126 listOperations.rightPop("students"); 127 } 128 129 /** 130 * 操作 Set 131 */ 132 @Test 133 public void testSet() { 134 SetOperations setOperations = redisTemplate.opsForSet(); 135 //添加数据 136 String[] letters = new String[]{"aaa", "bbb", "ccc"}; 137 setOperations.add("letters", letters); 138 //获取数据 139 Set set = setOperations.members("letters"); 140 set.forEach(System.out::println); 141 setOperations.remove("letters", "aaa", "bbb"); 142 } 143 144 @Test 145 public void testSortedSet() { 146 ZSetOperations zSetOperations = redisTemplate.opsForZSet(); 147 //添加数据 148 ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zhangsan", 7D); 149 ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("lisi", 3D); 150 ZSetOperations.TypedTuple<Object> objectTypedTuple3 = new DefaultTypedTuple<>("wangwu", 5D); 151 ZSetOperations.TypedTuple<Object> objectTypedTuple4 = new DefaultTypedTuple<>("zhaoliu", 1D); 152 ZSetOperations.TypedTuple<Object> objectTypedTuple5 = new DefaultTypedTuple<>("tyyianqi", 10D); 153 Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>(); 154 tuples.add(objectTypedTuple1); 155 tuples.add(objectTypedTuple2); 156 tuples.add(objectTypedTuple3); 157 tuples.add(objectTypedTuple4); 158 tuples.add(objectTypedTuple5); 159 zSetOperations.add("score", tuples); 160 161 //获取数据 162 Set set = zSetOperations.range("score", 0, 100); 163 set.forEach(System.out::println); 164 //获取总条数 165 Long size = zSetOperations.size("score"); 166 System.out.println(size); 167 //删除 168 zSetOperations.remove("score", "zhangsan", "lisi"); 169 } 170 171 /** 172 * 所处当前数据库中所有的 key 173 */ 174 @Test 175 public void testAllKey() { 176 //获取当前数据库所有key 177 Set keys = redisTemplate.keys("*"); 178 keys.forEach(System.out::println); 179 } 180 181 /** 182 * 失效时间 183 */ 184 @Test 185 public void testExpire() { 186 ValueOperations ops = redisTemplate.opsForValue(); 187 //方法一 ,添加key的时候设置失效时间 188 ops.set("code", "test", 30, TimeUnit.SECONDS); 189 //方法二,给已经存在的key设置失效时间 190 redisTemplate.expire("address", 30, TimeUnit.SECONDS); 191 //查看失效时间 192 Long expire = redisTemplate.getExpire("code"); 193 System.out.println(expire); 194 195 } 196 }