今天就跟大家聊聊有關(guān)SpringBoot中怎么配置單點(diǎn)redis緩存,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),小程序制作,十多年建站對(duì)成都廣告設(shè)計(jì)等多個(gè)領(lǐng)域,擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)。
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.lang.reflect.Method; /** * redis 緩存配置; * <p> * 注意:RedisCacheConfig這里也可以不用繼承:CachingConfigurerSupport,也就是直接一個(gè)普通的Class就好了; * <p> * 這里主要我們之后要重新實(shí)現(xiàn) key的生成策略,只要這里修改KeyGenerator,其它位置不用修改就生效了。 * <p> * 普通使用普通類的方式的話,那么在使用@Cacheable的時(shí)候還需要指定KeyGenerator的名稱;這樣編碼的時(shí)候比較麻煩。 * * @create 2017-12-13 12:45 **/ @Configuration @EnableCaching//啟用緩存,這個(gè)注解很重要; public class RedisCacheConfig extends CachingConfigurerSupport { /** * 緩存管理器. * * @param redisTemplate * @return */ @Bean public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //設(shè)置緩存過期時(shí)間 cacheManager.setDefaultExpiration(10000); return cacheManager; } /** * redis模板操作類,類似于jdbcTemplate的一個(gè)類; * <p> * 雖然CacheManager也能獲取到Cache對(duì)象,但是操作起來沒有那么靈活; * <p> * 這里在擴(kuò)展下:RedisTemplate這個(gè)類不見得很好操作,我們可以在進(jìn)行擴(kuò)展一個(gè)我們 * <p> * 自己的緩存類,比如:RedisStorage類; * * @param factory : 通過Spring進(jìn)行注入,參數(shù)在application.properties進(jìn)行配置; * @return */ @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template);//設(shè)置序列化工具 template.afterPropertiesSet(); return template; } private void setSerializer(StringRedisTemplate template) { Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); } /** * 自定義key. * 此方法將會(huì)根據(jù)類名+方法名+所有參數(shù)的值生成唯一的一個(gè)key,即使@Cacheable中的value屬性一樣,key也會(huì)不一樣。 */ @Override public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object obj : objects) { sb.append(obj.toString()); } return sb.toString(); } }; } }
######################################################## ###REDIS (RedisProperties) redis基本配置; ######################################################## # database name spring.redis.database=0 # server host1 spring.redis.host=127.0.0.1 # server password #spring.redis.password= #connection port spring.redis.port=6379 # pool settings ... spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 # name of Redis server #spring.redis.sentinel.master= # comma-separated list of host:port pairs #spring.redis.sentinel.nodes=
看完上述內(nèi)容,你們對(duì)SpringBoot中怎么配置單點(diǎn)Redis緩存有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站標(biāo)題:SpringBoot中怎么配置單點(diǎn)Redis緩存
文章起源:http://www.chinadenli.net/article20/iijico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站改版、全網(wǎng)營銷推廣、小程序開發(fā)、網(wǎng)站維護(hù)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)