Spring Cache與Redis結(jié)合的使用方式
前不久做了一個(gè)需要查詢多,更新少的功能,老司機(jī)同事建議用Redis來做緩存,同時(shí)結(jié)合Spring Cache來做,特來總結(jié)下。
Redis
Redis 是一個(gè)高性能key-value數(shù)據(jù)庫,個(gè)人感覺就像java中的Map,不過比它更加強(qiáng)大。
由于我用的是Mac,下面介紹如何安裝Redis。
brew update brew install redis
開啟服務(wù)
brew services start redis brew services list
下面是我本機(jī)的運(yùn)行截圖
創(chuàng)建Spring項(xiàng)目
我這邊為了簡單方便,直接使用了Spring Boot,直接用IntelJ Idea,需要添加Redis、Cache和Lombok庫。
集成Redis
集成Redis,直接在配置文件配置即可。
application.properties
#redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=0
然后測(cè)試下Redis是否集成功。
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class SpringcacheApplicationTests { @Autowired StringRedisTemplate redisTemplate; @Test public void contextLoads() { Assert.assertNotNull(redisTemplate); redisTemplate.opsForValue().set("hello", "world"); String value = redisTemplate.opsForValue().get("hello"); log.info("value = " + value); redisTemplate.delete("hello"); value = redisTemplate.opsForValue().get("hello"); log.info("value = " + value); } }
運(yùn)行結(jié)果如下,如果沒有出錯(cuò),則表示集成功。
2017-11-19 14:56:10.075 INFO 73896 --- [ main] c.m.s.SpringcacheApplicationTests : value = world
2017-11-19 14:56:10.076 INFO 73896 --- [ main] c.m.s.SpringcacheApplicationTests : value = null
Cache部分代碼
配置CacheManager,它的實(shí)現(xiàn)部分是由RedisCacheManager來實(shí)現(xiàn)的,我們先設(shè)置緩存時(shí)間為3s,超過這個(gè)時(shí)間,緩存自動(dòng)失效。
@Configuration @EnableCaching public class CachingConfig { @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); redisCacheManager.setDefaultExpiration(3); return redisCacheManager; } @Bean public CacheErrorHandler errorHandler() { return new RedisCacheErrorHandler(); } @Slf4j private static class RedisCacheErrorHandler extends SimpleCacheErrorHandler { @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) { log.error("handleCacheGetError key = {}, value = {}", key, cache); log.error("cache get error", exception); } @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) { log.error("handleCachePutError key = {}, value = {}", key, cache); log.error("cache put error", exception); } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) { log.error("handleCacheEvictError key = {}, value = {}", key, cache); log.error("cache evict error", exception); } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) { log.error("handleCacheClearError value = {}", cache); log.error("cache clear error", exception); } } }
添加一個(gè)簡單的實(shí)體,然后添加服務(wù)接口和實(shí)現(xiàn)類。
@Data是lombok提供的,可以減少簡潔代碼。注意實(shí)體必須實(shí)現(xiàn)Serializable接口。
@Data public class User implements Serializable { private int id; private String name; private String email; }
public interface UserService { void addUser(User user); User findById(int id); void delete(int id); }
@Slf4j @Service public class UserServiceImpl implements UserService { private final Map<Integer, User> db = new HashMap<>(); @Override public void addUser(User user) { log.info("addUser.user = " + user); db.put(user.getId(), user); } @Cacheable(cacheNames = "user_cache", key = "#id") @Override public User findById(int id) { log.info("findById.id = " + id); return db.get(id); } @CacheEvict(cacheNames = "user_cache", key = "#id") @Override public void delete(int id) { log.info("delete.id = " + id); db.remove(id); } }
上面Cacheable和CacheEvict就是Spring Cache提供的注解。具體說明如下。
@Cacheable 作用和配置方法
value
、cacheNames
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè)
例如: @Cacheable(value=”mycache”) @Cacheable(value={”cache1”,”cache2”}
key
緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合
例如: @Cacheable(value=”testcache”,key=”#userName”)
condition
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進(jìn)行緩存
例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”)
@CacheEvict 作用和配置方法
value
緩存的名稱,在 spring 配置文件中定義,必須指定至少一個(gè)
例如: @CacheEvict(value=”my cache”)
key
緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合
例如: @CacheEvict(value=”testcache”,key=”#userName”)
condition
緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進(jìn)行緩存
例如: @CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries
是否清空所有緩存內(nèi)容,缺省為 false,如果指定為 true,則方法調(diào)用后將立即清空所有緩存
例如: @CachEvict(value=”testcache”,allEntries=true)
beforeInvocation
是否在方法執(zhí)行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執(zhí)行的時(shí)候就清空緩存,缺省情況下,如果方法執(zhí)行拋出異常,則不會(huì)清空緩存
例如: @CachEvict(value=”testcache”,beforeInvocation=true)
測(cè)試用例:
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class CacheTest { @Autowired UserService userService; @Test public void contextLoads() { Assert.assertNotNull(userService); // 創(chuàng)建一個(gè)實(shí)體 User user = new User(); user.setId(100); user.setName("admin"); user.setEmail("admin@123.com"); // 添加一個(gè) userService.addUser(user); // 根據(jù)Id查詢 log.info("user1 = " + userService.findById(100)); sleep(1); // 等1s再次查詢 log.info("user2 = " + userService.findById(100)); sleep(5); // 等5s再次查詢 log.info("user3 = " + userService.findById(100)); // 添加一個(gè) userService.addUser(user); // 根據(jù)Id查詢 log.info("user4 = " + userService.findById(100)); // 刪除 userService.delete(100); // 根據(jù)Id查詢 log.info("user5 = " + userService.findById(100)); } private void sleep(int i) { try { Thread.sleep(i * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
測(cè)試結(jié)果
2017-11-19 15:08:35.732 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : addUser.user = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:35.921 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : findById.id = 100
2017-11-19 15:08:35.951 INFO 76558 --- [ main] cn.mycommons.springcache.CacheTest : user1 = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:37.016 INFO 76558 --- [ main] cn.mycommons.springcache.CacheTest : user2 = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:42.019 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : findById.id = 100
2017-11-19 15:08:42.021 INFO 76558 --- [ main] cn.mycommons.springcache.CacheTest : user3 = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:42.021 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : addUser.user = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:42.022 INFO 76558 --- [ main] cn.mycommons.springcache.CacheTest : user4 = User(id=100, name=admin, email=admin@123.com)
2017-11-19 15:08:42.023 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : delete.id = 100
2017-11-19 15:08:42.025 INFO 76558 --- [ main] c.m.s.service.impl.UserServiceImpl : findById.id = 100
2017-11-19 15:08:42.025 INFO 76558 --- [ main] cn.mycommons.springcache.CacheTest : user5 = null
從結(jié)果來看,添加一個(gè)數(shù)據(jù)后,第一次,查詢是從UserServiceImpl中獲取,再次查詢,則沒有直接調(diào)用UserServiceImpl,直接返回了緩存結(jié)果。
當(dāng)超過緩存時(shí)間后,再次去查詢,我們這邊設(shè)置緩存時(shí)間為3s,等待5s后,再次查詢,發(fā)現(xiàn)又從UserServiceImpl中獲取數(shù)據(jù)。
當(dāng)我們主動(dòng)調(diào)用刪除記錄,同時(shí)同步清楚緩存數(shù)據(jù)后,發(fā)現(xiàn)查詢是沒有數(shù)據(jù)的。說明刪除和清楚緩存操作實(shí)現(xiàn)了同步。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java SpringCache+Redis緩存數(shù)據(jù)詳解
- 詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時(shí)間解決方案
- SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)
- Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案
- SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問題)
- Spring Cache整合Redis實(shí)現(xiàn)方法詳解
- Spring cache整合redis代碼實(shí)例
相關(guān)文章
java web中的servlet3 upload上傳文件實(shí)踐
這篇文章主要介紹了servlet3 upload上傳文件實(shí)踐,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11springboot2.2.2集成dubbo的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot2.2.2集成dubbo的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例
這篇文章主要介紹了SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java?詳細(xì)分析四個(gè)經(jīng)典鏈表面試題
兄弟們,編程,當(dāng)我們學(xué)習(xí)完數(shù)據(jù)結(jié)構(gòu)的時(shí)候,你就會(huì)有一種豁然開朗的感覺。算是真正的入了編程的門,所以打好數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ)是特別特別重要的2022-03-03Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-03-03