spring boot 整合redis实现方法缓存
生活随笔
收集整理的這篇文章主要介紹了
spring boot 整合redis实现方法缓存
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
redis單機搭建:https://www.cnblogs.com/zuidongfeng/p/8032505.html
redis集群搭建請參考:https://blog.csdn.net/qq_42815754/article/details/82912130
?1、pom文件添加redis引用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.sl</groupId><artifactId>spring-boot-redis-cluster</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><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><!-- redis依賴commons-pool 這個依賴一定要添加 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.8.1</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.56</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>2、redis緩存配置
@Configuration public class RedisCacheConfig {@Autowiredprivate RedisConnectionFactory redisConnectionFactory;@Beanpublic RedisCacheManager redisCacheManager() {Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>();RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("zhao:").disableCachingNullValues().entryTtl(Duration.ofMinutes(30));configurationMap.put("c1",redisCacheConfiguration);RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);RedisCacheManager redisCacheManager = new RedisCacheManager(cacheWriter,RedisCacheConfiguration.defaultCacheConfig(),configurationMap);return redisCacheManager;} }?
3、redis集群配置
@Configuration @ConfigurationProperties(prefix = "spring.redis.cluster") public class RedisClusterConfig {List<Integer> ports;String host;JedisPoolConfig poolConfig;@Beanpublic RedisClusterConfiguration redisClusterConfiguration() {RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();List<RedisNode> nodes = new ArrayList<>();for (Integer port:ports) {nodes.add(new RedisNode(host,port));}clusterConfiguration.setClusterNodes(nodes);return clusterConfiguration;}@Beanpublic JedisConnectionFactory jedisConnectionFactory() {JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguration(),poolConfig);return factory;}@Beanpublic RedisTemplate redisTemplate() {RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(jedisConnectionFactory());redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());return redisTemplate;}@Beanpublic StringRedisTemplate stringRedisTemplate() {StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());stringRedisTemplate.setKeySerializer(new StringRedisSerializer());stringRedisTemplate.setValueSerializer(new StringRedisSerializer());return stringRedisTemplate;}public List<Integer> getPorts() {return ports;}public void setPorts(List<Integer> ports) {this.ports = ports;}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public JedisPoolConfig getPoolConfig() {return poolConfig;}public void setPoolConfig(JedisPoolConfig poolConfig) {this.poolConfig = poolConfig;} }?
4、配置文件application.yml
spring:redis:cluster:ports:- 7001- 7002- 7003- 7004- 7005- 7006host: 192.168.43.116poolConfig:max-total: 8max-idle : 8max-wait-millis: -1min-idle: 0cache:cache-names: c1,c2redis:time-to-live: 1800s?
5、實體類和dao層
public class User implements Serializable {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;} } @Component public class BookDao {@Cacheable(value = "c1")public String getBookId(Integer id) {System.out.println("getBookId");return "張三";}@CachePut(value = "c1")public String updateBookId(Integer id) {return "這是李四";}@CacheEvict(value = "c1")public void deleteById(Integer id) {System.out.println("deleteById");}@Cacheable(value = "c1")public String getBookById2(Integer id) {System.out.println("getBookById2");return "這是李四";}}?
6、測試結果類
@RunWith(SpringRunner.class) @SpringBootTest public class RedisClusterCacheApplicationTest {@Autowiredprivate BookDao bookDao;@Testpublic void test() {bookDao.getBookId(10);String book = bookDao.getBookId(100);System.out.println(book);bookDao.updateBookId(100);String bookId = bookDao.getBookId(100);System.out.println(bookId);bookDao.deleteById(100);bookDao.getBookId(100);bookDao.getBookById2(99);} }?
運行結果:?
張三
這是李四
deleteById
getBookId?
具體代碼參考GitHub:https://github.com/FadeHub/spring-boot-learn/tree/master/spring-boot-redis-cluster
總結
以上是生活随笔為你收集整理的spring boot 整合redis实现方法缓存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot 整合RESTFU
- 下一篇: spring boot 整合securi