導入依賴
1
2
3
4
5
6
7
8
9
10
11
12
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-cache</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-redis</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > |
基本配置
spring.redis.port=6380
spring.redis.host=192.168.66.128spring.cache.cache-names=c1 //給緩存取了一個名字
在啟動類上添加注解,表示開啟緩存
完成了這些配置之后,Spring Boot就會自動幫
1
2
3
4
5
6
7
8
9
|
@SpringBootApplication @EnableCaching public static void main(String[] args) { SpringApplication.run(RediscacheApplication. class , args); } } |
我們在后臺配置一個RedisCacheManager,相關的配置是在org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration類中完成的。部分源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Configuration @ConditionalOnClass (RedisConnectionFactory. class ) @AutoConfigureAfter (RedisAutoConfiguration. class ) @ConditionalOnBean (RedisConnectionFactory. class ) @ConditionalOnMissingBean (CacheManager. class ) @Conditional (CacheCondition. class ) class RedisCacheConfiguration { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager .builder(redisConnectionFactory) .cacheDefaults(determineConfiguration(resourceLoader.getClassLoader())); List<String> cacheNames = this .cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames( new LinkedHashSet<>(cacheNames)); } return this .customizerInvoker.customize(builder.build()); } } |
系統會自動提供一個RedisCacheManger的Bean,RedisCacheManager間接實現了Spring中的Cache接口,有了這個Bean,我們就可以直接使用Spring中的緩存注解和接口了,而緩存數據則會被自動存儲到Redis上。
在單機的Redis中,這個Bean系統會自動提供,如果是Redis集群,這個Bean需要開發者來提供
緩存使用@CachaConfig
這個注解在類上使用,用來描述該類中所有方法使用的緩存名稱,當然也可以不使用該注解,直接在具體的緩存注解上配置名稱,示例代碼如下:
1
2
3
4
|
@Service @CacheConfig (cacheNames = "c1" ) public class UserService { } |
@Cacheable
這個注解一般加載查詢方法上,表示將一個方法的返回值緩存起來,默認情況下,緩存的key就是方法的參數,緩存的value就是方法的返回值,示例代碼如下:
1
2
3
4
5
|
@Cacheable (key = "#id" ) public User getUserById(Integer id,String username) { System.out.println( "getUserById" ); return getUserFromDBById(id); } |
當有多個參數時,默認就使用多個參數來做key,如果只需要其中某一個參數做key,則可以在@Cacheable注解中,通過key屬性來指定key,如上代碼就表示只使用id作為緩存的key,如果對key有復雜的要求,可以自定義keyGenerator。當然,Spring Cache中提供了root對象,可以在不定義keyGenerator的情況下實現一些復雜的效果:
@CachePut
這個注解一般加在更新方法上,當數據庫中的數據更新后,緩存中的數據也要跟著更新,使用該注解,可以將方法的返回值自動更新到已經存在的key上,示例代碼如下:
1
2
3
4
|
@CachePut (key = "#user.id" ) public User updateUserById(User user) { return user; } |
@CacheEvict
這個注解一般加在刪除方法上,當數據庫中的數據刪除后,相關的緩存數據也要自動清除,該注解在使用的時候也可以配置按照某種條件刪除(condition屬性)或者或者配置清除所有緩存(allEntries屬性),示例代碼如下:
1
2
3
4
|
@CacheEvict () public void deleteUserById(Integer id) { //在這里執行刪除操作, 刪除是去數據庫中刪除 } |
總結
在SpringBoot中,使用Redis緩存,既可以使用RedisTemplate自己來實現,也可以使用使用這種方式,這種方式是Spring
Cache提供的統一接口,實現既可以是Redis,也可以是Ehcache或者其他支持這種規范的緩存框架。從這個角度來說,SpringCache和Redis、Ehcache的關系就像JDBC與各種數據庫驅動的關系。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/qiuwenli/p/13442988.html