在SpringBoot中添加Redis及配置方法
在實際的開發(fā)中,會有這樣的場景。有一個微服務需要提供一個查詢的服務,但是需要查詢的數(shù)據(jù)庫表的數(shù)據(jù)量十分龐大,查詢所需要的時間很長。 此時就可以考慮在項目中加入緩存。
引入依賴
在maven項目中引入如下依賴。并且需要在本地安裝redis。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.5.RELEASE</version> </dependency>
配置redis
在SpringBoot的配置文件中添加如下代碼。
redis: host: 127.0.0.1 port: 6379 timeout: 5000 database: 0 jedis: pool: max-idle: 8 max-wait: min-idle: 0
添加redis配置文件
新建名為RedisConfig的配置類。
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.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; 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.time.Duration; /** * RedisConfig * * @author detectiveHLH * @date 2018-10-11 14:39 **/ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean @Override public KeyGenerator keyGenerator() { return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); }; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //redis序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisTemplate template = new StringRedisTemplate(factory); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } /** * 自定義CacheManager */ @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { //全局redis緩存過期時間 RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1)); RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); } }
添加緩存配置
在項目的service層中的實現(xiàn)類中,添加@Cacheable注解。
import java.util.HashMap; /** * UserLoginServiceImpl * * @author detectiveHLH * @date 2018-10-10 17:20 **/ @Service public class UserLoginServiceImpl implements UserLoginService { @Autowired private UserLoginMapper userLoginMapper; @Override @Cacheable(value = "usercache") public HashMap getByUserName(String userName) { System.out.println("此時沒有走緩存"); return userLoginMapper.getByUserName(userName); } }
然后調(diào)用一次該接口。就可以在redis中看到如下的key。
"usercache::com.detectiveHLH.api.service.impl.UserLoginServiceImplgetByUserNameSolarFarm"
同時,可以在控制臺中看到有"此時沒有走緩存"的輸出。然后再次調(diào)用該接口,就可以看到返回的速度明顯變快,并且沒有"此時沒有走緩存"輸出。說明 此時的接口走的是緩存。
總結
以上所述是小編給大家介紹的在SpringBoot中添加Redis及配置方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
圖解Java經(jīng)典算法插入排序的原理與實現(xiàn)
插入排序的算法描述是一種簡單直觀的排序算法。其原理是通過構建有序序列,對于未排序數(shù)據(jù),在已排序序列中從后向前掃描,找到相應位置并插入。本文將用Java語言實現(xiàn)插入排序算法并進行可視化,感興趣的可以了解一下2022-09-09Java實現(xiàn)獲取cpu、內(nèi)存、硬盤、網(wǎng)絡等信息的方法示例
這篇文章主要介紹了Java實現(xiàn)獲取cpu、內(nèi)存、硬盤、網(wǎng)絡等信息的方法,涉及java使用第三方jar包針對本機硬件的cpu、內(nèi)存、硬盤、網(wǎng)絡信息等的讀取相關操作技巧,需要的朋友可以參考下2018-06-06Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果
這篇文章主要為大家詳細介紹了Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07spring cloud gateway中如何讀取請求參數(shù)
這篇文章主要介紹了spring cloud gateway中如何讀取請求參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java?MultipartFile文件上傳重命名詳細代碼示例
在文件上傳功能開發(fā)中,為防止文件重名導致數(shù)據(jù)覆蓋,常見的做法是在文件名前加上UUID或時間戳來區(qū)分,這篇文章主要介紹了java?MultipartFile?multipartFile文件上傳重命名的相關資料,需要的朋友可以參考下2024-09-09