View Javadoc
1   package net.sumaris.core.dao.technical.ehcache;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core shared
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sf.ehcache.CacheManager;
26  import org.springframework.cache.ehcache.EhCacheFactoryBean;
27  
28  /**
29   * Helper class
30   */
31  public class Caches {
32  
33      protected static final int ETERNAL_TIME_TO_LIVE = 24 * 60* 60; // 1 days
34      protected static final int ETERNAL_TIME_TO_IDLE = 24 * 60* 60; // 1 days
35  
36  
37      private Caches() {
38          // Helper class
39      }
40  
41      public static EhCacheFactoryBean createHeapCache(CacheManager cacheManager, String cacheName, int timeToLive, int timeToIdle, int entriesLocalHeap) {
42          EhCacheFactoryBean factory = new EhCacheFactoryBean();
43          // Default properties
44          factory.setCacheName(cacheName);
45          factory.setCacheManager(cacheManager);
46  
47          factory.setMaxEntriesLocalHeap(entriesLocalHeap);
48  
49          factory.setEternal(false);
50          factory.setOverflowToOffHeap(false);
51          factory.setDiskSpoolBufferSize(0);
52          factory.setDiskExpiryThreadIntervalSeconds(0);
53          factory.setTimeToLive(timeToLive);
54          factory.setTimeToIdle(timeToIdle);
55          return factory;
56      }
57  
58      public static EhCacheFactoryBean createEternalHeapCache(CacheManager cacheManager, String cacheName, int entriesLocalHeap) {
59         return createHeapCache(cacheManager, cacheName, ETERNAL_TIME_TO_LIVE, ETERNAL_TIME_TO_IDLE, entriesLocalHeap);
60      }
61  
62      public static EhCacheFactoryBean createEternalDiskCache(CacheManager cacheManager, String cacheName, int timeToLive, int timeToIdle, int entriesLocalHeap, int diskSpoolBufferSize) {
63          EhCacheFactoryBean factory = new EhCacheFactoryBean();
64          // Default properties
65          factory.setCacheName(cacheName);
66          factory.setCacheManager(cacheManager);
67          factory.setMaxEntriesLocalHeap(entriesLocalHeap);
68          factory.setEternal(true);
69          factory.setOverflowToOffHeap(true);
70          factory.setDiskSpoolBufferSize(diskSpoolBufferSize);
71          factory.setDiskExpiryThreadIntervalSeconds(timeToLive);
72          factory.setTimeToLive(timeToLive);
73          factory.setTimeToIdle(timeToIdle);
74          return factory;
75      }
76  }