導(dǎo)入依賴
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 //給緩存取了一個(gè)名字
在啟動類上添加注解,表示開啟緩存
完成了這些配置之后,Spring Boot就會自動幫
1
2
3
4
5
6
7
8
9
|
@SpringBootApplication @EnableCaching public static void main(String[] args) { SpringApplication.run(RediscacheApplication. class , args); } } |
我們在后臺配置一個(gè)RedisCacheManager,相關(guān)的配置是在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()); } } |
系統(tǒng)會自動提供一個(gè)RedisCacheManger的Bean,RedisCacheManager間接實(shí)現(xiàn)了Spring中的Cache接口,有了這個(gè)Bean,我們就可以直接使用Spring中的緩存注解和接口了,而緩存數(shù)據(jù)則會被自動存儲到Redis上。
在單機(jī)的Redis中,這個(gè)Bean系統(tǒng)會自動提供,如果是Redis集群,這個(gè)Bean需要開發(fā)者來提供
緩存使用@CachaConfig
這個(gè)注解在類上使用,用來描述該類中所有方法使用的緩存名稱,當(dāng)然也可以不使用該注解,直接在具體的緩存注解上配置名稱,示例代碼如下:
1
2
3
4
|
@Service @CacheConfig (cacheNames = "c1" ) public class UserService { } |
@Cacheable
這個(gè)注解一般加載查詢方法上,表示將一個(gè)方法的返回值緩存起來,默認(rèn)情況下,緩存的key就是方法的參數(shù),緩存的value就是方法的返回值,示例代碼如下:
1
2
3
4
5
|
@Cacheable (key = "#id" ) public User getUserById(Integer id,String username) { System.out.println( "getUserById" ); return getUserFromDBById(id); } |
當(dāng)有多個(gè)參數(shù)時(shí),默認(rèn)就使用多個(gè)參數(shù)來做key,如果只需要其中某一個(gè)參數(shù)做key,則可以在@Cacheable注解中,通過key屬性來指定key,如上代碼就表示只使用id作為緩存的key,如果對key有復(fù)雜的要求,可以自定義keyGenerator。當(dāng)然,Spring Cache中提供了root對象,可以在不定義keyGenerator的情況下實(shí)現(xiàn)一些復(fù)雜的效果:
@CachePut
這個(gè)注解一般加在更新方法上,當(dāng)數(shù)據(jù)庫中的數(shù)據(jù)更新后,緩存中的數(shù)據(jù)也要跟著更新,使用該注解,可以將方法的返回值自動更新到已經(jīng)存在的key上,示例代碼如下:
1
2
3
4
|
@CachePut (key = "#user.id" ) public User updateUserById(User user) { return user; } |
@CacheEvict
這個(gè)注解一般加在刪除方法上,當(dāng)數(shù)據(jù)庫中的數(shù)據(jù)刪除后,相關(guān)的緩存數(shù)據(jù)也要自動清除,該注解在使用的時(shí)候也可以配置按照某種條件刪除(condition屬性)或者或者配置清除所有緩存(allEntries屬性),示例代碼如下:
1
2
3
4
|
@CacheEvict () public void deleteUserById(Integer id) { //在這里執(zhí)行刪除操作, 刪除是去數(shù)據(jù)庫中刪除 } |
總結(jié)
在SpringBoot中,使用Redis緩存,既可以使用RedisTemplate自己來實(shí)現(xiàn),也可以使用使用這種方式,這種方式是Spring
Cache提供的統(tǒng)一接口,實(shí)現(xiàn)既可以是Redis,也可以是Ehcache或者其他支持這種規(guī)范的緩存框架。從這個(gè)角度來說,SpringCache和Redis、Ehcache的關(guān)系就像JDBC與各種數(shù)據(jù)庫驅(qū)動的關(guān)系。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/qiuwenli/p/13442988.html