日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 整合redis实现方法缓存

發布時間:2025/1/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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实现方法缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。