Spring Cache整合Redis實(shí)現(xiàn)方法詳解
導(dǎo)入依賴
<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=6380spring.redis.host=192.168.66.128
spring.cache.cache-names=c1 //給緩存取了一個(gè)名字
在啟動(dòng)類上添加注解,表示開啟緩存
完成了這些配置之后,Spring Boot就會(huì)自動(dòng)幫
@SpringBootApplication@EnableCachingpublic class RediscacheApplication { public static void main(String[] args) { SpringApplication.run(RediscacheApplication.class, args); }}
我們?cè)诤笈_(tái)配置一個(gè)RedisCacheManager,相關(guān)的配置是在org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration類中完成的。部分源碼如下:
@Configuration@ConditionalOnClass(RedisConnectionFactory.class)@AutoConfigureAfter(RedisAutoConfiguration.class)@ConditionalOnBean(RedisConnectionFactory.class)@ConditionalOnMissingBean(CacheManager.class)@Conditional(CacheCondition.class)class RedisCacheConfiguration {@Beanpublic 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)會(huì)自動(dòng)提供一個(gè)RedisCacheManger的Bean,RedisCacheManager間接實(shí)現(xiàn)了Spring中的Cache接口,有了這個(gè)Bean,我們就可以直接使用Spring中的緩存注解和接口了,而緩存數(shù)據(jù)則會(huì)被自動(dòng)存儲(chǔ)到Redis上。
在單機(jī)的Redis中,這個(gè)Bean系統(tǒng)會(huì)自動(dòng)提供,如果是Redis集群,這個(gè)Bean需要開發(fā)者來提供
緩存使用@CachaConfig
這個(gè)注解在類上使用,用來描述該類中所有方法使用的緩存名稱,當(dāng)然也可以不使用該注解,直接在具體的緩存注解上配置名稱,示例代碼如下:
@Service@CacheConfig(cacheNames = 'c1')public class UserService {}
@Cacheable
這個(gè)注解一般加載查詢方法上,表示將一個(gè)方法的返回值緩存起來,默認(rèn)情況下,緩存的key就是方法的參數(shù),緩存的value就是方法的返回值,示例代碼如下:
@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,如果對(duì)key有復(fù)雜的要求,可以自定義keyGenerator。當(dāng)然,Spring Cache中提供了root對(duì)象,可以在不定義keyGenerator的情況下實(shí)現(xiàn)一些復(fù)雜的效果:
@CachePut
這個(gè)注解一般加在更新方法上,當(dāng)數(shù)據(jù)庫中的數(shù)據(jù)更新后,緩存中的數(shù)據(jù)也要跟著更新,使用該注解,可以將方法的返回值自動(dòng)更新到已經(jīng)存在的key上,示例代碼如下:
@CachePut(key = '#user.id')public User updateUserById(User user) { return user;}
@CacheEvict
這個(gè)注解一般加在刪除方法上,當(dāng)數(shù)據(jù)庫中的數(shù)據(jù)刪除后,相關(guān)的緩存數(shù)據(jù)也要自動(dòng)清除,該注解在使用的時(shí)候也可以配置按照某種條件刪除(condition屬性)或者或者配置清除所有緩存(allEntries屬性),示例代碼如下:
@CacheEvict()public void deleteUserById(Integer id) { //在這里執(zhí)行刪除操作, 刪除是去數(shù)據(jù)庫中刪除}
總結(jié)
在SpringBoot中,使用Redis緩存,既可以使用RedisTemplate自己來實(shí)現(xiàn),也可以使用使用這種方式,這種方式是SpringCache提供的統(tǒng)一接口,實(shí)現(xiàn)既可以是Redis,也可以是Ehcache或者其他支持這種規(guī)范的緩存框架。從這個(gè)角度來說,SpringCache和Redis、Ehcache的關(guān)系就像JDBC與各種數(shù)據(jù)庫驅(qū)動(dòng)的關(guān)系。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法3. xml中的空格之完全解說4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. asp讀取xml文件和記數(shù)6. axios和ajax的區(qū)別點(diǎn)總結(jié)7. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算8. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享9. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?10. phpstudy apache開啟ssi使用詳解
