springboot整合redis lettuce

偏执的太偏执、 2023-03-01 09:49 136阅读 0赞

需要配置的jar

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <optional>true</optional>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>org.junit.vintage</groupId>
  21. <artifactId>junit-vintage-engine</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <artifactId>junit</artifactId>
  28. <scope>test</scope>
  29. </dependency>

配置

  1. server.port=9901
  2. ##redis配置
  3. spring.redis.database=0
  4. spring.redis.host=192.168.99.92
  5. spring.redis.port=6379
  6. spring.redis.password=8HD5agiwfJrx0ptc@MRD
  7. spring.redis.timeout=10000
  8. ##redis线程池配置
  9. spring.redis.lettuce.pool.max-active=8
  10. spring.redis.lettuce.pool.max-wait=-1
  11. spring.redis.lettuce.pool.max-idle=8
  12. spring.redis.jedis.pool.min-idle=0

如果存储对象可以写一下

  1. package com.jackray.redis.configure;
  2. import java.io.Serializable;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9. @Configuration
  10. public class LettuceRedisConfig {
  11. @Bean
  12. public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
  13. RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
  14. redisTemplate.setKeySerializer(new StringRedisSerializer());
  15. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  16. redisTemplate.setConnectionFactory(lettuceConnectionFactory);
  17. return redisTemplate;
  18. }
  19. }

测试用例 整体要不要太简单

  1. package com.jackray.redis.redis;
  2. import com.jackray.redis.bean.User;
  3. import java.io.Serializable;
  4. import java.util.concurrent.TimeUnit;
  5. import org.junit.Assert;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.omg.CORBA.PRIVATE_MEMBER;
  9. import org.omg.CORBA.ServerRequest;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. /**
  15. * redisTemplate.opsForValue();//操作字符串
  16. * redisTemplate.opsForHash();//操作hash
  17. * redisTemplate.opsForList();//操作list
  18. * redisTemplate.opsForSet();//操作set
  19. * redisTemplate.opsForZSet();//操作有序set
  20. */
  21. @RunWith(SpringRunner.class)
  22. @SpringBootTest
  23. public class RedisTest {
  24. @Autowired
  25. private RedisTemplate<String, String> strRedisTemplate;
  26. @Autowired
  27. private RedisTemplate<String, Serializable> serializableRedisTemplate;
  28. @Test
  29. public void testString(){
  30. strRedisTemplate.opsForValue().set("sad", "sad");
  31. System.out.println(strRedisTemplate.opsForValue().get("sad"));
  32. }
  33. @Test
  34. public void testSerializables(){
  35. String ss = "sad";
  36. User user = new User();
  37. user.setId(1);
  38. user.setAddress("sad");
  39. serializableRedisTemplate.opsForValue().set(ss, user, 2, TimeUnit.HOURS);
  40. User sad = (User) serializableRedisTemplate.opsForValue().get(ss);
  41. System.out.println(sad.toString());
  42. }
  43. }

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读