Spring Boot 鍏ラ棬 - 杩涢樁绡囷紙五锛? 鏁版嵁缂撳瓨(@Cacheable)
Spring Boot 鍏ラ棬 - 杩涢樁绡囷紙5锛? 鏁版嵁缂撳瓨(@Cacheable)
缂撳瓨鍙互缂撹В鏁版嵁搴撹闂殑鍘嬪姏锛孲pring鑷韩涓嶆彁渚涚紦瀛樼殑瀛樺偍瀹炵幇锛岄渶瑕佸€熷姪绗笁鏂癸紝姣斿JCache銆丒hCache銆丠azelcast銆丷edis銆丟uava绛夈€係pring Boot鍙互鑷姩鍖栭厤缃悎閫傜殑缂撳瓨绠$悊鍣紙CacheManager锛夛紝榛樿閲囩敤鐨勬槸ConcurrentMapCacheManager锛坖ava.util.concurrent.ConcurrentHashMap锛夈€?
锛?锛夋坊鍔?spring-boot-starter-cache 渚濊禆
锛?锛夊紑鍚紦瀛樺姛鑳?/strong>
锛?锛夌紦瀛樻暟鎹?/strong>
瀵逛簬缂撳瓨鐨勬搷浣滐紝涓昏鏈夛細@Cacheable銆丂CachePut銆丂CacheEvict銆?
@Cacheable
Spring 鍦ㄦ墽琛?@Cacheable 鏍囨敞鐨勬柟娉曞墠鍏堟煡鐪嬬紦瀛樹腑鏄惁鏈夋暟鎹紝濡傛灉鏈夋暟鎹紝鍒欑洿鎺ヨ繑鍥炵紦瀛樻暟鎹紱鑻ユ病鏈夋暟鎹紝鎵ц璇ユ柟娉曞苟灏嗘柟娉曡繑鍥炲€兼斁杩涚紦瀛樸€?
鍙傛暟锛?value缂撳瓨鍚嶃€?key缂撳瓨閿€笺€?condition婊¤冻缂撳瓨鏉′欢銆乽nless鍚﹀喅缂撳瓨鏉′欢
@CachePut
鍜?@Cacheable 绫讳技锛屼絾浼氭妸鏂规硶鐨勮繑鍥炲€兼斁鍏ョ紦瀛樹腑, 涓昏鐢ㄤ簬鏁版嵁鏂板鍜屼慨鏀规柟娉曘€?
@CacheEvict
鏂规硶鎵ц鎴愬姛鍚庝細浠庣紦瀛樹腑绉婚櫎鐩稿簲鏁版嵁銆?
鍙傛暟锛?value缂撳瓨鍚嶃€?key缂撳瓨閿€笺€?condition婊¤冻缂撳瓨鏉′欢銆?unless鍚﹀喅缂撳瓨鏉′欢銆?allEntries鏄惁绉婚櫎鎵€鏈夋暟鎹紙璁剧疆涓簍rue鏃朵細绉婚櫎鎵€鏈夌紦瀛橈級
锛?锛夐泦鎴怑hCache
SpringBoot鍙互鑷姩閰嶇疆涓嶉渶瑕佷粈涔堢壒娈婅缃嵆鍙娇鐢ㄣ€?
src/main/resources/ehcache.xml
src\main\resources/application.properties
濡傛灉鎯宠嚜瀹氫箟璁剧疆涓€浜涗釜鎬у寲鍙傛暟鏃讹紝閫氳繃Java Config褰㈠紡閰嶇疆銆?
锛?锛夌粍鍚圕acheManager
浠庡涓狢acheManager涓疆璇㈠緱鍒扮浉搴旂殑Cache銆?
*** 璁剧疆缂撳瓨鏃犳晥鍖?spring.cache.type=none
*** 缂撳瓨鐨勫璞″繀椤诲疄鐜癝erializable
*** 闄uavaCacheManager涔嬪閮芥敮鎸丼pring浜嬪姟锛屽嵆鍥炴粴鏃禖ache鐨勬暟鎹篃浼氳绉婚櫎
缂撳瓨鍙互缂撹В鏁版嵁搴撹闂殑鍘嬪姏锛孲pring鑷韩涓嶆彁渚涚紦瀛樼殑瀛樺偍瀹炵幇锛岄渶瑕佸€熷姪绗笁鏂癸紝姣斿JCache銆丒hCache銆丠azelcast銆丷edis銆丟uava绛夈€係pring Boot鍙互鑷姩鍖栭厤缃悎閫傜殑缂撳瓨绠$悊鍣紙CacheManager锛夛紝榛樿閲囩敤鐨勬槸ConcurrentMapCacheManager锛坖ava.util.concurrent.ConcurrentHashMap锛夈€?
锛?锛夋坊鍔?spring-boot-starter-cache 渚濊禆
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
锛?锛夊紑鍚紦瀛樺姛鑳?/strong>
@Configuration @EnableCaching public class CacheConfig { }
锛?锛夌紦瀛樻暟鎹?/strong>
瀵逛簬缂撳瓨鐨勬搷浣滐紝涓昏鏈夛細@Cacheable銆丂CachePut銆丂CacheEvict銆?
@Cacheable
Spring 鍦ㄦ墽琛?@Cacheable 鏍囨敞鐨勬柟娉曞墠鍏堟煡鐪嬬紦瀛樹腑鏄惁鏈夋暟鎹紝濡傛灉鏈夋暟鎹紝鍒欑洿鎺ヨ繑鍥炵紦瀛樻暟鎹紱鑻ユ病鏈夋暟鎹紝鎵ц璇ユ柟娉曞苟灏嗘柟娉曡繑鍥炲€兼斁杩涚紦瀛樸€?
鍙傛暟锛?value缂撳瓨鍚嶃€?key缂撳瓨閿€笺€?condition婊¤冻缂撳瓨鏉′欢銆乽nless鍚﹀喅缂撳瓨鏉′欢
@Cacheable(value = "user", key = "#id") public User findById(final Long id) { System.out.println("cache miss, invoke find by id, id:" + id); for (User user : users) { if (user.getId().equals(id)) { return user; } } return null; }
@CachePut
鍜?@Cacheable 绫讳技锛屼絾浼氭妸鏂规硶鐨勮繑鍥炲€兼斁鍏ョ紦瀛樹腑, 涓昏鐢ㄤ簬鏁版嵁鏂板鍜屼慨鏀规柟娉曘€?
@CachePut(value = "user", key = "#user.id") public User save(User user) { users.add(user); return user; }
@CacheEvict
鏂规硶鎵ц鎴愬姛鍚庝細浠庣紦瀛樹腑绉婚櫎鐩稿簲鏁版嵁銆?
鍙傛暟锛?value缂撳瓨鍚嶃€?key缂撳瓨閿€笺€?condition婊¤冻缂撳瓨鏉′欢銆?unless鍚﹀喅缂撳瓨鏉′欢銆?allEntries鏄惁绉婚櫎鎵€鏈夋暟鎹紙璁剧疆涓簍rue鏃朵細绉婚櫎鎵€鏈夌紦瀛橈級
@CacheEvict(value = "user", key = "#user.id") // 绉婚櫎鎸囧畾key鐨勬暟鎹? public User delete(User user) { users.remove(user); return user; }
@CacheEvict(value = "user", allEntries = true) // 绉婚櫎鎵€鏈夋暟鎹? public void deleteAll() { users.clear(); }
锛?锛夐泦鎴怑hCache
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
SpringBoot鍙互鑷姩閰嶇疆涓嶉渶瑕佷粈涔堢壒娈婅缃嵆鍙娇鐢ㄣ€?
src/main/resources/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <cache name="user" maxEntriesLocalHeap="200" timeToLiveSeconds="600"> </cache> </ehcache>
src\main\resources/application.properties
寮曠敤
spring.cache.ehcache.config=classpath:ehcache.xml
濡傛灉鎯宠嚜瀹氫箟璁剧疆涓€浜涗釜鎬у寲鍙傛暟鏃讹紝閫氳繃Java Config褰㈠紡閰嶇疆銆?
@Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehCacheCacheManager().getObject()); } @Bean public EhCacheManagerFactoryBean ehCacheCacheManager() { EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); cmfb.setShared(true); return cmfb; } }
锛?锛夌粍鍚圕acheManager
浠庡涓狢acheManager涓疆璇㈠緱鍒扮浉搴旂殑Cache銆?
@Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager compositeCacheManager(RedisTemplate<Object, Object> redisTemplate) { CompositeCacheManager cacheManager = new CompositeCacheManager(new ConcurrentMapCacheManager(), new SimpleCacheManager()); cacheManager.setFallbackToNoOpCache(false); cacheManager.afterPropertiesSet(); return cacheManager; } }
*** 璁剧疆缂撳瓨鏃犳晥鍖?spring.cache.type=none
*** 缂撳瓨鐨勫璞″繀椤诲疄鐜癝erializable
*** 闄uavaCacheManager涔嬪閮芥敮鎸丼pring浜嬪姟锛屽嵆鍥炴粴鏃禖ache鐨勬暟鎹篃浼氳绉婚櫎