{"id":880,"date":"2023-03-09T21:53:08","date_gmt":"2023-03-09T13:53:08","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=880"},"modified":"2023-04-29T16:41:20","modified_gmt":"2023-04-29T08:41:20","slug":"spring-boot-integrate-ehcache","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-integrate-ehcache\/","title":{"rendered":"Spring Boot\u6574\u5408EhCache"},"content":{"rendered":"<p>\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u7684\u538b\u529b\uff0c\u4f7f\u7528\u7f13\u5b58\u662f\u975e\u5e38\u597d\u7684\u624b\u6bb5\u4e4b\u4e00\u3002<\/p>\n<h2>\u58f0\u660e\u5f0f\u7f13\u5b58<\/h2>\n<p>Spring \u5b9a\u4e49 CacheManager \u548c Cache \u63a5\u53e3\u7528\u6765\u7edf\u4e00\u4e0d\u540c\u7684\u7f13\u5b58\u6280\u672f\u3002\u4f8b\u5982 JCache\u3001 EhCache\u3001 Hazelcast\u3001 Guava\u3001 Redis \u7b49\u3002\u5728\u4f7f\u7528 Spring \u96c6\u6210 Cache \u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u518c\u5b9e\u73b0\u7684 CacheManager \u7684 Bean\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u5982\u679c\u60f3\u66f4\u6df1\u5165\u7406\u89e3 Spring \u7684 Cache \u673a\u5236\uff0c\u63a8\u8350\u4e24\u7bc7\u4e0d\u9519\u7684\u6587\u7ae0\uff1a<\/p>\n<p><a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/jinnianshilongnian.iteye.com\/blog\/2001040\" title=\"Spring Cache \u62bd\u8c61\u8be6\u89e3\">Spring Cache \u62bd\u8c61\u8be6\u89e3<\/a><br \/>\n<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/jinnianshilongnian.iteye.com\/blog\/2105367\" title=\"Spring 4.1 \u65b0\u7279\u6027 - Spring \u7f13\u5b58\u6846\u67b6\u589e\u5f3a \">Spring 4.1 \u65b0\u7279\u6027 &#8211; Spring \u7f13\u5b58\u6846\u67b6\u589e\u5f3a <\/a><\/p>\n<h2>Spring Boot\u9ed8\u8ba4\u96c6\u6210CacheManager<\/h2>\n<p>Spring Boot \u4e3a\u6211\u4eec\u81ea\u52a8\u914d\u7f6e\u4e86\u591a\u4e2a CacheManager \u7684\u5b9e\u73b0\u3002\u5305\u62ec<code>JcacheCacheConfiguration<\/code>\u3001<code>EhCacheCacheConfiguration<\/code>\u3001<code>HazelcastCacheConfiguration<\/code>\u3001<code>GuavaCacheConfiguration<\/code>\u3001<code>RedisCacheConfiguration<\/code>\u3001<code>SimpleCacheConfiguration<\/code>\u7b49\u3002<\/p>\n<h2>\u9ed8\u8ba4\u7684 ConcurrenMapCacheManager<\/h2>\n<p>Spring \u4ece Spring 3.1 \u5f00\u59cb\u57fa\u4e8e<code>java.util.concurrent.ConcurrentHashMap<\/code>\u5b9e\u73b0\u7684\u7f13\u5b58\u7ba1\u7406\u5668\u3002\u6240\u4ee5\uff0cSpring Boot \u9ed8\u8ba4\u4f7f\u7528<code>ConcurrentMapCacheManager<\/code>\u4f5c\u4e3a\u7f13\u5b58\u6280\u672f\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u4eec\u4e0d\u5f15\u5165\u5176\u4ed6\u7f13\u5b58\u4f9d\u8d56\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u53f0\u6253\u5370\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<pre><code>Bean &#039;cacheManager&#039; of type [class org.springframework.cache.concurrent.ConcurrentMapCacheManager]<\/code><\/pre>\n<h2>Spring Boot \u7f13\u5b58<\/h2>\n<p>\u5728 Spring Boot\u4e2d\uff0c\u901a\u8fc7@EnableCaching\u6ce8\u89e3\u81ea\u52a8\u5316\u914d\u7f6e\u5408\u9002\u7684\u7f13\u5b58\u7ba1\u7406\u5668\uff08CacheManager\uff09\uff0cSpring Boot\u6839\u636e\u4e0b\u9762\u7684\u987a\u5e8f\u53bb\u4fa6\u6d4b\u7f13\u5b58\u63d0\u4f9b\u8005\uff1a<\/p>\n<ul>\n<li>Generic<\/li>\n<li>JCache (JSR-107)<\/li>\n<li>EhCache 2.x<\/li>\n<li>Hazelcast<\/li>\n<li>Infinispan<\/li>\n<li>Redis<\/li>\n<li>Guava<\/li>\n<li>Simple<\/li>\n<\/ul>\n<p>\u5173\u4e8e Spring Boot \u7684\u7f13\u5b58\u673a\u5236\uff1a\u9ad8\u901f\u7f13\u5b58\u62bd\u8c61\u4e0d\u63d0\u4f9b\u5b9e\u9645\u5b58\u50a8\uff0c\u5e76\u4e14\u4f9d\u8d56\u4e8e\u7531<code>org.springframework.cache.Cache<\/code>\u548c<code>org.springframework.cache.CacheManager<\/code>\u63a5\u53e3\u5b9e\u73b0\u7684\u62bd\u8c61\u3002Spring Boot\u6839\u636e\u5b9e\u73b0\u81ea\u52a8\u914d\u7f6e\u5408\u9002\u7684CacheManager\uff0c\u53ea\u8981\u7f13\u5b58\u652f\u6301\u901a\u8fc7<code>@EnableCaching<\/code>\u6ce8\u91ca\u542f\u7528\u5373\u53ef\u3002<\/p>\n<h2>EhCache<\/h2>\n<p>JAVA\u7f13\u5b58\u5b9e\u73b0\u65b9\u6848\u6709\u5f88\u591a\uff0c\u6700\u57fa\u672c\u7684\u81ea\u5df1\u4f7f\u7528Map\u53bb\u6784\u5efa\u7f13\u5b58\uff0c\u6216\u8005\u4f7f\u7528Memcached\u6216Redis\uff0c\u4f46\u662f\u4e0a\u8ff0\u4e24\u79cd\u7f13\u5b58\u6846\u67b6\u90fd\u8981\u642d\u5efa\u670d\u52a1\u5668\uff0c\u800cMap\u81ea\u884c\u6784\u5efa\u7684\u7f13\u5b58\u53ef\u80fd\u6ca1\u6709\u5f88\u9ad8\u7684\u4f7f\u7528\u6548\u7387\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b\u4f7f\u7528Ehcache\u7f13\u5b58\u6846\u67b6\u3002<\/p>\n<p>Ehcache\u4e3b\u8981\u57fa\u4e8e\u5185\u5b58\u7f13\u5b58\uff0c\u78c1\u76d8\u7f13\u5b58\u4e3a\u8f85\u7684\uff0c\u4f7f\u7528\u8d77\u6765\u65b9\u4fbf\u3002<\/p>\n<h2>Springboot\u6574\u5408EhCache<\/h2>\n<p>\u6574\u5408\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u6dfb\u52a0pom\u6587\u4ef6maven\u4f9d\u8d56<\/li>\n<li>\u914d\u7f6eehcache.xml<\/li>\n<li>\u5f00\u542f\u7f13\u5b58\u652f\u6301<\/li>\n<li>\u9879\u76ee\u4e2d\u4f7f\u7528<\/li>\n<\/ul>\n<h3>\u6dfb\u52a0pom\u6587\u4ef6maven\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;!--\u5f00\u542f cache \u7f13\u5b58 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-cache&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- ehcache\u7f13\u5b58 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;net.sf.ehcache&lt;\/groupId&gt;\n    &lt;artifactId&gt;ehcache&lt;\/artifactId&gt;\n    &lt;version&gt;2.9.1&lt;\/version&gt;&lt;!--$NO-MVN-MAN-VER$ --&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u914d\u7f6eehcache.xml<\/h3>\n<p>\u9ed8\u8ba4\u81ea\u52a8\u52a0\u8f7dresources\u76ee\u5f55\u4e0b\u7684<code>ehcache.xml<\/code>\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;ehcache xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n    xsi:noNamespaceSchemaLocation=&quot;ehcache.xsd&quot;&gt;\n    &lt;!--timeToIdleSeconds \u5f53\u7f13\u5b58\u95f2\u7f6en\u79d2\u540e\u9500\u6bc1 --&gt;\n    &lt;!--timeToLiveSeconds \u5f53\u7f13\u5b58\u5b58\u6d3bn\u79d2\u540e\u9500\u6bc1 --&gt;\n    &lt;!-- \u7f13\u5b58\u914d\u7f6e\n        name: \u7f13\u5b58\u540d\u79f0\n        maxElementsInMemory\uff1a\u7f13\u5b58\u6700\u5927\u4e2a\u6570\n        eternal: \u5bf9\u8c61\u662f\u5426\u6c38\u4e45\u6709\u6548\uff0c\u4e00\u4f46\u8bbe\u7f6e\u4e86\uff0ctimeout\u5c06\u4e0d\u8d77\u4f5c\u7528\n        timeToIdleSeconds\uff1a\u8bbe\u7f6e\u5bf9\u8c61\u5728\u5931\u6548\u524d\u7684\u5141\u8bb8\u95f2\u7f6e\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002\u4ec5\u5f53eternal=false\u5bf9\u8c61\u4e0d\u662f\u6c38\u4e45\u6709\u6548\u65f6\u4f7f\u7528\uff0c\u53ef\u9009\u5c5e\u6027\uff0c\u9ed8\u8ba4\u503c\u662f0\uff0c\u4e5f\u5c31\u662f\u53ef\u95f2\u7f6e\u65f6\u95f4\u65e0\u7a77\u5927\n        timeToLiveSeconds\uff1a\u8bbe\u7f6e\u5bf9\u8c61\u5728\u5931\u6548\u524d\u5141\u8bb8\u5b58\u6d3b\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002\u6700\u5927\u65f6\u95f4\u4ecb\u4e8e\u521b\u5efa\u65f6\u95f4\u548c\u5931\u6548\u65f6\u95f4\u4e4b\u95f4\u3002\u4ec5\u5f53eternal=false\u5bf9\u8c61\u4e0d\u662f\u6c38\u4e45\u6709\u6548\u65f6\u4f7f\u7528\uff0c\u9ed8\u8ba4\u662f0\uff0c\u4e5f\u5c31\u662f\u5bf9\u8c61\u5b58\u6d3b\u65f6\u95f4\u65e0\u7a77\u5927\n        overflowToDisk\uff1a\u5f53\u5185\u5b58\u4e2d\u5bf9\u8c61\u6570\u91cf\u8fbe\u5230maxElementsInMemory\u65f6\uff0cEhcache\u5c06\u4f1a\u5bf9\u8c61\u5199\u5230\u78c1\u76d8\u4e2d\n        diskSpoolBufferSizeMB\uff1a\u8fd9\u4e2a\u53c2\u6570\u8bbe\u7f6eDiskStore\uff08\u78c1\u76d8\u7f13\u5b58\uff09\u7684\u7f13\u5b58\u533a\u5927\u5c0f\u3002\u9ed8\u8ba4\u662f30MB\u3002\u6bcf\u4e2aCache\u90fd\u5e94\u8be5\u6709\u81ea\u5df1\u7684\u4e00\u4e2a\u7f13\u51b2\u533a\n        maxElementsOnDisk\uff1a\u786c\u76d8\u6700\u5927\u7f13\u5b58\u4e2a\u6570\n        diskPersistent\uff1a\u662f\u5426\u7f13\u5b58\u865a\u62df\u673a\u91cd\u542f\u671f\u6570\u636e Whether the disk \n        store persists between restarts of the Virtual Machine. The default value \n        is false. \n        diskExpiryThreadIntervalSeconds\uff1a\u78c1\u76d8\u5931\u6548\u7ebf\u7a0b\u8fd0\u884c\u65f6\u95f4\u95f4\u9694\uff0c\u9ed8\u8ba4\u662f120\u79d2\n        memoryStoreEvictionPolicy\uff1a\u5f53\u8fbe\u5230maxElementsInMemory\u9650\u5236\u65f6\uff0cEhcache\u5c06\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u7b56\u7565\u53bb\u6e05\u7406\u5185\u5b58\u3002\u9ed8\u8ba4\u7b56\u7565\u662f \nLRU\uff08\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff09\u3002\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u4e3aFIFO\uff08\u5148\u8fdb\u5148\u51fa\uff09\u6216\u662fLFU\uff08\u8f83\u5c11\u4f7f\u7528\uff09\n        clearOnFlush\uff1a\u5185\u5b58\u6570\u91cf\u6700\u5927\u65f6\u662f\u5426\u6e05\u9664 --&gt;\n    &lt;!-- \u78c1\u76d8\u7f13\u5b58\u4f4d\u7f6e --&gt;\n    &lt;diskStore path=&quot;java.io.tmpdir&quot; \/&gt;\n    &lt;!-- \u9ed8\u8ba4\u7f13\u5b58 --&gt;\n    &lt;defaultCache \n        maxElementsInMemory=&quot;10000&quot; \n        eternal=&quot;false&quot;\n        timeToIdleSeconds=&quot;120&quot; \n        timeToLiveSeconds=&quot;120&quot; \n        maxElementsOnDisk=&quot;10000000&quot;\n        diskExpiryThreadIntervalSeconds=&quot;120&quot; \n        memoryStoreEvictionPolicy=&quot;LRU&quot;&gt;\n\n        &lt;persistence strategy=&quot;localTempSwap&quot; \/&gt;\n    &lt;\/defaultCache&gt;\n\n    &lt;!-- \u6d4b\u8bd5 --&gt;\n    &lt;cache name=&quot;GoodsType&quot; \n        eternal=&quot;false&quot; \n        timeToIdleSeconds=&quot;2400&quot;\n        timeToLiveSeconds=&quot;2400&quot; \n        maxEntriesLocalHeap=&quot;10000&quot;\n        maxEntriesLocalDisk=&quot;10000000&quot; \n        diskExpiryThreadIntervalSeconds=&quot;120&quot;\n        overflowToDisk=&quot;false&quot; \n        memoryStoreEvictionPolicy=&quot;LRU&quot;&gt;\n    &lt;\/cache&gt;\n&lt;\/ehcache&gt;<\/code><\/pre>\n<p>\u5bf9\u4e8eEhCache\u7684\u914d\u7f6e\u6587\u4ef6\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>application.yml<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528<code>spring.cache.ehcache.config<\/code>\u5c5e\u6027\u6765\u6307\u5b9a\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"language-yml\">spring:\n  cache:\n    ehcache:\n      config: classpath:ehcache.xml<\/code><\/pre>\n<p>ehcache.xml\u914d\u7f6e\u6587\u4ef6\u8be6\u89e3<\/p>\n<ul>\n<li><code>diskStore<\/code>\uff1a\u4e3a\u7f13\u5b58\u8def\u5f84\uff0cehcache\u5206\u4e3a\u5185\u5b58\u548c\u78c1\u76d8\u4e24\u7ea7\uff0c\u6b64\u5c5e\u6027\u5b9a\u4e49\u78c1\u76d8\u7684\u7f13\u5b58\u4f4d\u7f6e<\/li>\n<li><code>defaultCache<\/code>\uff1a\u9ed8\u8ba4\u7f13\u5b58\u7b56\u7565\uff0c\u5f53ehcache\u627e\u4e0d\u5230\u5b9a\u4e49\u7684\u7f13\u5b58\u65f6\uff0c\u5219\u4f7f\u7528\u8fd9\u4e2a\u7f13\u5b58\u7b56\u7565\u3002\u53ea\u80fd\u5b9a\u4e49\u4e00\u4e2a<\/li>\n<li><code>name<\/code>: \u7f13\u5b58\u540d\u79f0<\/li>\n<li><code>maxElementsInMemory<\/code>: \u7f13\u5b58\u6700\u5927\u6570\u76ee<\/li>\n<li><code>maxElementsOnDisk<\/code>\uff1a\u786c\u76d8\u6700\u5927\u7f13\u5b58\u4e2a\u6570<\/li>\n<li><code>eternal<\/code>: \u5bf9\u8c61\u662f\u5426\u6c38\u4e45\u6709\u6548\uff0c\u4e00\u4f46\u8bbe\u7f6e\u4e86\uff0ctimeout\u5c06\u4e0d\u8d77\u4f5c\u7528<\/li>\n<li><code>overflowToDisk<\/code>: \u662f\u5426\u4fdd\u5b58\u5230\u78c1\u76d8\uff0c\u5f53\u7cfb\u7edf\u5b95\u673a\u65f6<\/li>\n<li><code>timeToIdleSeconds<\/code>: \u8bbe\u7f6e\u5bf9\u8c61\u5728\u5931\u6548\u524d\u7684\u5141\u8bb8\u95f2\u7f6e\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002\u4ec5\u5f53eternal=false\u5bf9\u8c61\u4e0d\u662f\u6c38\u4e45\u6709\u6548\u65f6\u4f7f\u7528\uff0c\u53ef\u9009\u5c5e\u6027\uff0c\u9ed8\u8ba4\u503c\u662f0\uff0c\u4e5f\u5c31\u662f\u53ef\u95f2\u7f6e\u65f6\u95f4\u65e0\u7a77\u5927<\/li>\n<li><code>timeToLiveSeconds<\/code>: \u8bbe\u7f6e\u5bf9\u8c61\u5728\u5931\u6548\u524d\u5141\u8bb8\u5b58\u6d3b\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002\u6700\u5927\u65f6\u95f4\u4ecb\u4e8e\u521b\u5efa\u65f6\u95f4\u548c\u5931\u6548\u65f6\u95f4\u4e4b\u95f4\u3002\u4ec5\u5f53eternal=false\u5bf9\u8c61\u4e0d\u662f\u6c38\u4e45\u6709\u6548\u65f6\u4f7f\u7528\uff0c\u9ed8\u8ba4\u662f0.\uff0c\u4e5f\u5c31\u662f\u5bf9\u8c61\u5b58\u6d3b\u65f6\u95f4\u65e0\u7a77\u5927<\/li>\n<li><code>diskPersistent<\/code>\uff1a\u662f\u5426\u7f13\u5b58\u865a\u62df\u673a\u91cd\u542f\u671f\u6570\u636e Whether the disk store persists between restarts of the Virtual Machine. The default value is false.<\/li>\n<li><code>diskSpoolBufferSizeMB<\/code>\uff1a\u8fd9\u4e2a\u53c2\u6570\u8bbe\u7f6eDiskStore\uff08\u78c1\u76d8\u7f13\u5b58\uff09\u7684\u7f13\u5b58\u533a\u5927\u5c0f\u3002\u9ed8\u8ba4\u662f30MB\u3002\u6bcf\u4e2aCache\u90fd\u5e94\u8be5\u6709\u81ea\u5df1\u7684\u4e00\u4e2a\u7f13\u51b2\u533a<\/li>\n<li><code>diskExpiryThreadIntervalSeconds<\/code>\uff1a\u78c1\u76d8\u5931\u6548\u7ebf\u7a0b\u8fd0\u884c\u65f6\u95f4\u95f4\u9694\uff0c\u9ed8\u8ba4\u662f120\u79d2<\/li>\n<li><code>memoryStoreEvictionPolicy<\/code>\uff1a\u5f53\u8fbe\u5230maxElementsInMemory\u9650\u5236\u65f6\uff0cEhcache\u5c06\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u7b56\u7565\u53bb\u6e05\u7406\u5185\u5b58\u3002\u9ed8\u8ba4\u7b56\u7565\u662fLRU\uff08\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff09\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4e3aFIFO\uff08\u5148\u8fdb\u5148\u51fa\uff09\u6216\u662fLFU\uff08\u8f83\u5c11\u4f7f\u7528\uff09<\/li>\n<li><code>clearOnFlush<\/code>\uff1a\u5185\u5b58\u6570\u91cf\u6700\u5927\u65f6\u662f\u5426\u6e05\u9664<\/li>\n<\/ul>\n<pre><code>FIFO\uff0cfirst in first out\uff0c\u5148\u8fdb\u5148\u51fa\nLFU\uff0cLess Frequently Used\uff0c\u4e00\u76f4\u4ee5\u6765\u6700\u5c11\u88ab\u4f7f\u7528\u7684\u3002\u5982\u4e0a\u9762\u6240\u8bb2\uff0c\u7f13\u5b58\u7684\u5143\u7d20\u6709\u4e00\u4e2ahit\u5c5e\u6027\uff0chit\u503c\u6700\u5c0f\u7684\u5c06\u4f1a\u88ab\u6e05\u51fa\u7f13\u5b58\nLRU\uff0cLeast Recently Used\uff0c\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\uff0c\u7f13\u5b58\u7684\u5143\u7d20\u6709\u4e00\u4e2a\u65f6\u95f4\u6233\uff0c\u5f53\u7f13\u5b58\u5bb9\u91cf\u6ee1\u4e86\uff0c\u800c\u53c8\u9700\u8981\u817e\u51fa\u5730\u65b9\u6765\u7f13\u5b58\u65b0\u7684\u5143\u7d20\u7684\u65f6\u5019\uff0c\u90a3\u4e48\u73b0\u6709\u7f13\u5b58\u5143\u7d20\u4e2d\u65f6\u95f4\u6233\u79bb\u5f53\u524d\u65f6\u95f4\u6700\u8fdc\u7684\u5143\u7d20\u5c06\u88ab\u6e05\u51fa\u7f13\u5b58<\/code><\/pre>\n<h3>\u5f00\u542f\u7f13\u5b58\u652f\u6301<\/h3>\n<pre><code class=\"language-java\">import org.springframework.cache.annotation.EnableCaching;\nimport org.springframework.context.annotation.Configuration;\n\n\/**\n * @ClassName: EhCacheConfig\n * @Description: \u914d\u7f6e\u7f13\u5b58\n *\/\n@Configuration\n@EnableCaching\npublic class EhCacheConfig {\n\n}<\/code><\/pre>\n<h3>\u9879\u76ee\u4e2d\u4f7f\u7528<\/h3>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5728Sercive\u5c42\u8fdb\u884c\u5bf9\u7f13\u5b58\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5148\u4ecb\u7ecd Ehcache \u5728 Spring \u4e2d\u7684\u6ce8\u89e3\uff1a\u5728\u652f\u6301 Spring Cache \u7684\u73af\u5883\u4e0b<\/p>\n<pre><code class=\"language-java\">@CacheConfig\n\nString[] cacheNames() default {};\n\u6307\u5b9a\u4f7f\u7528\u90a3\u4e2a\u7f13\u5b58,\u5982\u4e0b:\n@CacheConfig(cacheNames = &quot;GoodsType&quot;)\n@CacheConfig(cacheNames = {&quot;cache1&quot;, &quot;cache2&quot;})<\/code><\/pre>\n<p>\u914d\u7f6e\u5728\u7c7b\u4e0a\u9762\uff0c\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u7f13\u5b58<\/p>\n<pre><code class=\"language-java\">@Cacheable\n\n@AliasFor(&quot;cacheNames&quot;)\nString[] value() default {};\n\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5fc5\u987b\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2a\uff0c\u4f8b\u5982\uff1a\n@Cacheable(value = &quot;mycache&quot;) \u6216\u8005 \n@Cacheable(value = {&quot;cache1&quot;, &quot;cache2&quot;}\n\nString key() default &quot;&quot;;\n\u7f13\u5b58\u7684 key\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6307\u5b9a\u8981\u6309\u7167 SpEL \u8868\u8fbe\u5f0f\u7f16\u5199\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u7f3a\u7701\u6309\u7167\u65b9\u6cd5\u7684\u6240\u6709\u53c2\u6570\u8fdb\u884c\u7ec4\u5408  \u4f8b\u5982\uff1a\n@Cacheable(value = &quot;testcache&quot;, key = &quot;#userName&quot;)\n\nString condition() default &quot;&quot;;\n\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f7f\u7528 SpEL \u7f16\u5199\uff0c\u8fd4\u56de true \u6216\u8005 false\uff0c\u53ea\u6709\u4e3a true \u624d\u8fdb\u884c\u7f13\u5b58   \u4f8b\u5982\uff1a\n@Cacheable(value = &quot;testcache&quot;, condition = &quot;#userName.length()&gt;2&quot;)<\/code><\/pre>\n<p>Spring\u5728\u6bcf\u6b21\u6267\u884c\u524d\u90fd\u4f1a\u68c0\u67e5Cache\u4e2d\u662f\u5426\u5b58\u5728\u76f8\u540ckey\u7684\u7f13\u5b58\u5143\u7d20\uff0c\u5982\u679c\u5b58\u5728\u5c31\u4e0d\u518d\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u800c\u662f\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\u8fdb\u884c\u8fd4\u56de\uff0c\u5426\u5219\u624d\u4f1a\u6267\u884c\u5e76\u5c06\u8fd4\u56de\u7ed3\u679c\u5b58\u5165\u6307\u5b9a\u7684\u7f13\u5b58\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">@CachePut\n\n\u548c@Cacheable\u6ce8\u89e3\u76f8\u7c7b\u4f3c<\/code><\/pre>\n<p>\u4f7f\u7528<code>@CachePut<\/code>\u6807\u6ce8\u7684\u65b9\u6cd5\u5728\u6267\u884c\u524d\u4e0d\u4f1a\u53bb\u68c0\u67e5\u7f13\u5b58\u4e2d\u662f\u5426\u5b58\u5728\u4e4b\u524d\u6267\u884c\u8fc7\u7684\u7ed3\u679c\uff0c\u800c\u662f\u6bcf\u6b21\u90fd\u4f1a\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5e76\u5c06\u6267\u884c\u7ed3\u679c\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u5165\u6307\u5b9a\u7684\u7f13\u5b58\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">@CacheEvict\n\n@AliasFor(&quot;cacheNames&quot;)\nString[] value() default {};\n\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u5728 spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff0c\u5fc5\u987b\u6307\u5b9a\u81f3\u5c11\u4e00\u4e2a\uff0c\u4f8b\u5982\uff1a\n@CachEvict(value= &quot;mycach&quot;) \u6216\u8005 \n@CachEvict(value = {&quot;cache1&quot;, &quot;cache2&quot;}\n\nString key() default &quot;&quot;;\n\u7f13\u5b58\u7684 key\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6307\u5b9a\u8981\u6309\u7167 SpEL \u8868\u8fbe\u5f0f\u7f16\u5199\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u7f3a\u7701\u6309\u7167\u65b9\u6cd5\u7684\u6240\u6709\u53c2\u6570\u8fdb\u884c\u7ec4\u5408\uff0c\u4f8b\u5982\uff1a\n@CachEvict(value=&quot;testcache&quot;, key=&quot;#userName&quot;)\n\nString condition() default &quot;&quot;;\n\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f7f\u7528 SpEL \u7f16\u5199\uff0c\u8fd4\u56de true \u6216\u8005 false\uff0c\u53ea\u6709\u4e3a true \u624d\u6e05\u7a7a\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a\n@CachEvict(value=&quot;testcache&quot;, condition=&quot;#userName.length()&gt;2&quot;)\n\nboolean allEntries() default false;\n\u662f\u5426\u6e05\u7a7a\u6240\u6709\u7f13\u5b58\u5185\u5bb9\uff0c\u7f3a\u7701\u4e3a false\uff0c\u5982\u679c\u6307\u5b9a\u4e3a true\uff0c\u5219\u65b9\u6cd5\u8c03\u7528\u540e\u5c06\u7acb\u5373\u6e05\u7a7a\u6240\u6709\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a\n@CachEvict(value=&quot;testcache&quot;, allEntries=true)\n\nboolean beforeInvocation() default false;\n\u662f\u5426\u5728\u65b9\u6cd5\u6267\u884c\u524d\u5c31\u6e05\u7a7a\uff0c\u7f3a\u7701\u4e3a false\uff0c\u5982\u679c\u6307\u5b9a\u4e3a true\uff0c\u5219\u5728\u65b9\u6cd5\u8fd8\u6ca1\u6709\u6267\u884c\u7684\u65f6\u5019\u5c31\u6e05\u7a7a\u7f13\u5b58\uff0c\u7f3a\u7701\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u65b9\u6cd5\u6267\u884c\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u4e0d\u4f1a\u6e05\u7a7a\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a\n@CachEvict(value=&quot;testcache&quot;, beforeInvocation=true)<\/code><\/pre>\n<p>\u6e05\u9664\u7f13\u5b58<\/p>\n<pre><code class=\"language-java\">@Caching<\/code><\/pre>\n<p>\u5206\u7ec4\u6ce8\u89e3\uff0c\u80fd\u591f\u540c\u65f6\u5e94\u7528\u591a\u4e2a\u5176\u4ed6\u7684\u7f13\u5b58\u6ce8\u89e3<\/p>\n<p>\u6ce8\u89e3\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li><code>@Cacheable<\/code>\u8fd9\u4e2a\u76f8\u5f53\u4e8e<code>save()<\/code>\u64cd\u4f5c<\/li>\n<li><code>@CachePut<\/code>\u76f8\u5f53\u4e8e<code>update()<\/code>\u64cd\u4f5c\uff0c\u53ea\u8981\u4ed6\u6807\u793a\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\uff0c\u90a3\u4e48\u90fd\u4f1a\u7f13\u5b58\u8d77\u6765\uff0c\u800c@cache\u5219\u662f\u5148\u770b\u4e0b\u6709\u6ca1\u5df2\u7ecf\u7f13\u5b58\uff0c\u7136\u540e\u518d\u9009\u62e9\u662f\u5426\u6267\u884c\u65b9\u6cd5<\/li>\n<li><code>@CacheEvict<\/code>\u76f8\u5f53\u4e8e<code>delete()<\/code>\u64cd\u4f5c\uff0c\u7528\u6765\u6e05\u9664\u7f13\u5b58\u7528\u7684<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.cache.annotation.CacheConfig;\nimport org.springframework.cache.annotation.CacheEvict;\nimport org.springframework.cache.annotation.CachePut;\nimport org.springframework.cache.annotation.Cacheable;\nimport org.springframework.stereotype.Repository;\n\n\/**\n * \n * @ClassName: GoodsTypeDaoImpl\n * @Description: \u6a21\u62df\u6570\u636e\u8bbf\u95ee\u5b9e\u73b0\u7c7b\n *\/\n@Repository\n@CacheConfig(cacheNames = &quot;GoodsType&quot;)\npublic class GoodsTypeDaoImpl {\n\n    @Cacheable\n    public String save(String typeId) {\n        System.out.println(&quot;save()\u6267\u884c\u4e86=============&quot;);\n        return &quot;\u6a21\u62df\u6570\u636e\u5e93\u4fdd\u5b58&quot;;\n    }\n\n    @CachePut\n    public String update(String typeId) {\n        System.out.println(&quot;update()\u6267\u884c\u4e86=============&quot;);\n        return &quot;\u6a21\u62df\u6570\u636e\u5e93\u66f4\u65b0&quot;;\n    }\n\n    @CacheEvict\n    public String delete(String typeId) {\n        System.out.println(&quot;delete()\u6267\u884c\u4e86=============&quot;);\n        return &quot;\u6a21\u62df\u6570\u636e\u5e93\u5220\u9664&quot;;\n    }\n\n    @Cacheable\n    public String select(String typeId) {\n        System.out.println(&quot;select()\u6267\u884c\u4e86=============&quot;);\n        return &quot;\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2&quot;;\n    }\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-java\">import org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.context.junit4.SpringRunner;\n\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class GoodsTypeDaoImplTest {\n\n    @Autowired\n    private GoodsTypeDaoImpl typeDao;\n\n    @Test\n    public void testSave() {\n        String typeId = &quot;appblog.cn&quot;;\n        \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u4fdd\u5b58\n        String returnStr1 = typeDao.save(typeId);\n        System.out.println(returnStr1);\n        \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u4fdd\u5b58\n        String returnStr2 = typeDao.save(typeId);\n        System.out.println(returnStr2);\n    }\n\n    @Test\n    public void testUpdate() {\n        String typeId = &quot;appblog.cn&quot;;\n        \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n        String returnStr1 = typeDao.select(typeId);\n        System.out.println(returnStr1);\n        \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n        String returnStr2 = typeDao.select(typeId);\n        System.out.println(returnStr2);\n        \/\/ \u6a21\u62df\u66f4\u65b0\n        String returnStr3 = typeDao.update(typeId);\n        System.out.println(returnStr3);\n        \/\/ \u6a21\u62df\u67e5\u8be2\n        String returnStr4 = typeDao.select(typeId);\n        System.out.println(returnStr4);\n    }\n\n    @Test\n    public void testDelete() {\n        String typeId = &quot;appblog.cn&quot;;\n        \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n        String returnStr1 = typeDao.select(typeId);\n        System.out.println(returnStr1);\n        \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n        String returnStr2 = typeDao.select(typeId);\n        System.out.println(returnStr2);\n        \/\/ \u6a21\u62df\u5220\u9664\n        String returnStr3 = typeDao.delete(typeId);\n        System.out.println(returnStr3);\n        \/\/ \u6a21\u62df\u67e5\u8be2\n        String returnStr4 = typeDao.select(typeId);\n        System.out.println(returnStr4);\n    }\n\n    @Test\n    public void testSelect() {\n        String typeId = &quot;appblog.cn&quot;;\n        \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n        String returnStr1 = typeDao.select(typeId);\n        System.out.println(returnStr1);\n        \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n        String returnStr2 = typeDao.select(typeId);\n        System.out.println(returnStr2);\n    }\n}<\/code><\/pre>\n<p>\u6267\u884c<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testSave() {\n    String typeId = &quot;appblog.cn&quot;;\n    \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u4fdd\u5b58\n    String returnStr1 = typeDao.save(typeId);\n    System.out.println(returnStr1);\n    \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u4fdd\u5b58\n    String returnStr2 = typeDao.save(typeId);\n    System.out.println(returnStr2);\n}<\/code><\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre><code class=\"language-bash\">save()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u4fdd\u5b58\n\u6a21\u62df\u6570\u636e\u5e93\u4fdd\u5b58<\/code><\/pre>\n<p>\u6267\u884c<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testUpdate() {\n    String typeId = &quot;appblog.cn&quot;;\n    \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n    String returnStr1 = typeDao.select(typeId);\n    System.out.println(returnStr1);\n    \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n    String returnStr2 = typeDao.select(typeId);\n    System.out.println(returnStr2);\n    \/\/ \u6a21\u62df\u66f4\u65b0\n    String returnStr3 = typeDao.update(typeId);\n    System.out.println(returnStr3);\n    \/\/ \u6a21\u62df\u67e5\u8be2\n    String returnStr4 = typeDao.select(typeId);\n    System.out.println(returnStr4);\n}<\/code><\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre><code class=\"language-bash\">select()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\nupdate()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u66f4\u65b0\n\u6a21\u62df\u6570\u636e\u5e93\u66f4\u65b0<\/code><\/pre>\n<p>\u6267\u884c<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testDelete() {\n    String typeId = &quot;appblog.cn&quot;;\n    \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n    String returnStr1 = typeDao.select(typeId);\n    System.out.println(returnStr1);\n    \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n    String returnStr2 = typeDao.select(typeId);\n    System.out.println(returnStr2);\n    \/\/ \u6a21\u62df\u5220\u9664\n    String returnStr3 = typeDao.delete(typeId);\n    System.out.println(returnStr3);\n    \/\/ \u6a21\u62df\u67e5\u8be2\n    String returnStr4 = typeDao.select(typeId);\n    System.out.println(returnStr4);\n}<\/code><\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre><code class=\"language-bash\">select()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\ndelete()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u5220\u9664\nselect()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2<\/code><\/pre>\n<p>\u6267\u884c<\/p>\n<pre><code class=\"language-java\">@Test\npublic void testSelect() {\n    String typeId = &quot;appblog.cn&quot;;\n    \/\/ \u6a21\u62df\u7b2c\u4e00\u6b21\u67e5\u8be2\n    String returnStr1 = typeDao.select(typeId);\n    System.out.println(returnStr1);\n    \/\/ \u6a21\u62df\u7b2c\u4e8c\u6b21\u67e5\u8be2\n    String returnStr2 = typeDao.select(typeId);\n    System.out.println(returnStr2);\n}<\/code><\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre><code class=\"language-bash\">select()\u6267\u884c\u4e86=============\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\n\u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u7684\u538b\u529b\uff0c\u4f7f\u7528\u7f13\u5b58\u662f\u975e\u5e38\u597d\u7684\u624b\u6bb5\u4e4b\u4e00\u3002 \u58f0\u660e\u5f0f\u7f13\u5b58 Spring \u5b9a\u4e49 CacheMan [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[112],"class_list":["post-880","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-ehcache"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}