赞
踩
目录
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- @RestController
- public class RedisController {
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
- @GetMapping("/count")
- public String count(){
- Long count = stringRedisTemplate.opsForValue().increment("k1");
- return "访问了"+count+"次";
- }
- }
- @SpringBootTest
- class Boot3RedisApplicationTests {
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
-
- /**
- * 普通字符串
- */
- @Test
- void contextLoads() {
- stringRedisTemplate.opsForValue().set("test1", UUID.randomUUID().toString());
- String k = stringRedisTemplate.opsForValue().get("test1");
- System.out.println(k);
- }
-
- /**
- * list类型
- */
- @Test
- void testList(){
- stringRedisTemplate.opsForList().leftPush("a1","1");
- stringRedisTemplate.opsForList().leftPush("a1","2");
- stringRedisTemplate.opsForList().leftPush("a1","3");
- String a1 = stringRedisTemplate.opsForList().leftPop("a1");
- Assertions.assertEquals("3",a1);
- }
-
- /**
- * set类型
- */
- @Test
- void testSet(){
- stringRedisTemplate.opsForSet().add("set1","1","2","3");
- Boolean set1 = stringRedisTemplate.opsForSet().isMember("set1", "2");
- Assertions.assertTrue(set1);
- }
-
- /**
- * zset类型
- */
- @Test
- void testZSet(){
- stringRedisTemplate.opsForZSet().add("zset1","张三",12);
- stringRedisTemplate.opsForZSet().add("zset1","李四",13);
- stringRedisTemplate.opsForZSet().add("zset1","王五",14);
- ZSetOperations.TypedTuple<String> zset1 = stringRedisTemplate.opsForZSet().popMax("zset1");
- System.out.println(zset1.getValue()+":"+zset1.getScore());
- }
-
- /**
- * hash类型
- */
- @Test
- void testHash(){
- stringRedisTemplate.opsForHash().put("hash1","name","tom");
- stringRedisTemplate.opsForHash().put("hash1","age","12");
- }
- }
- @Data
- public class Person implements Serializable {
- private Integer id;
- private String name;
- private Integer age;
- }
- @GetMapping("/addPerson")
- public void addPerson(){
- Person person = new Person();
- person.setId(1);
- person.setName("tom");
- person.setAge(12);
- //序列化,将对象转为字符串
- redisTemplate.opsForValue().set("person",person);
- }
- @GetMapping("/getPerson")
- public Person getPerson(){
- Person person = (Person) redisTemplate.opsForValue().get("person");
- return person;
- }
发现存储在redis中的数据乱码,不可视
因为RedisTemplate使用的是默认序列化机制,为了方便其他系统交互应使用JOSN数据交互
解决代码如下:
- @Configuration
- public class AppRedisConfig {
- @Bean
- public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
- RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
- redisTemplate.setConnectionFactory(redisConnectionFactory);
- //把对象转为json字符串的序列化
- redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
- return redisTemplate;
- }
- }
RedisTemplate底层默认使用lettuce
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- <exclusions>
- <exclusion>
- <groupId>io.lettuce</groupId>
- <artifactId>lettuce-core</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。