net.sf.ehcache.Cache Java Examples
The following examples show how to use
net.sf.ehcache.Cache.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EHCacheProvider.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 7 votes |
public ResourceDataCache createDataCache() { try { final CacheManager manager = getCacheManager(); synchronized( manager ) { if ( manager.cacheExists( DATA_CACHE_NAME ) == false ) { final Cache libloaderCache = new Cache( DATA_CACHE_NAME, // cache name 500, // maxElementsInMemory false, // overflowToDisk false, // eternal 600, // timeToLiveSeconds 600, // timeToIdleSeconds false, // diskPersistent 120 ); // diskExpiryThreadIntervalSeconds manager.addCache( libloaderCache ); return new EHResourceDataCache( libloaderCache ); } else { return new EHResourceDataCache( manager.getCache( DATA_CACHE_NAME ) ); } } } catch ( CacheException e ) { logger.debug( "Failed to create EHCache libloader-data cache", e ); return null; } }
Example #2
Source File: EhCacheManagerFactoryBean.java From sakai with Educational Community License v2.0 | 6 votes |
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); //Now look for any custom configuration. CacheManager cm = (CacheManager) this.getObject(); if (cm != null) { String cacheNames[]= cm.getCacheNames(); //Check for old configuration properties. for (String cacheName:cacheNames) { if(serverConfigurationService.getString(cacheName) == null) { log.warn("Old cache configuration "+ cacheName+ " must be changed to memory."+ cacheName); } String config = serverConfigurationService.getString("memory."+ cacheName); if (config != null && config.length() > 0) { log.info("Found configuration override for cache: "+ cacheName+ " of: "+ config); Cache cache = cm.getCache(cacheName); if (cache != null) { new CacheInitializer().configure(config).initialize(cache.getCacheConfiguration()); } } } } }
Example #3
Source File: RegressServiceImpl.java From jvm-sandbox-repeater with Apache License 2.0 | 6 votes |
@Override public RepeaterResult<Regress> getRegressWithCache(String name) { Cache cache = cacheManager.getCache("regressCache"); // priority use of the cache data Element element = cache.get(name); Regress regress; if (element == null) { regress = getRegressInternal(name, 1); cache.put(new Element(name, regress)); } else { regress = (Regress) element.getObjectValue(); } return RepeaterResult.builder() .data(regress) .success(true) .message("operate success") .build(); }
Example #4
Source File: EhCacheSupportTests.java From spring-analysis-note with MIT License | 6 votes |
public void testCacheManagerFromConfigFile() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass())); cacheManagerFb.setCacheManagerName("myCacheManager"); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1); Cache myCache1 = cm.getCache("myCache1"); assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal()); assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300); } finally { cacheManagerFb.destroy(); } }
Example #5
Source File: EhCacheSupportTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testBlankCacheManager() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setCacheManagerName("myCacheManager"); assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); assertTrue("Singleton property", cacheManagerFb.isSingleton()); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); Cache myCache1 = cm.getCache("myCache1"); assertTrue("No myCache1 defined", myCache1 == null); } finally { cacheManagerFb.destroy(); } }
Example #6
Source File: JbootEhcacheImpl.java From jboot with Apache License 2.0 | 6 votes |
public Cache getOrAddCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { synchronized (locker) { cache = cacheManager.getCache(cacheName); if (cache == null) { cacheManager.addCacheIfAbsent(cacheName); cache = cacheManager.getCache(cacheName); if (cacheEventListener != null) { cache.getCacheEventNotificationService().registerListener(cacheEventListener); } } } } return cache; }
Example #7
Source File: ECacheProvider.java From anyline with Apache License 2.0 | 6 votes |
public Element getElement(String channel, String key){ Element result = null; long fr = System.currentTimeMillis(); Cache cache = getCache(channel); if(null != cache){ result = cache.get(key); if(null == result){ if(ConfigTable.isDebug() && log.isWarnEnabled()){ log.warn("[缓存不存在][cnannel:{}][key:{}][生存:-1/{}]",channel, key,cache.getCacheConfiguration().getTimeToLiveSeconds()); } return null; } if(result.isExpired()){ if(ConfigTable.isDebug() && log.isWarnEnabled()){ log.warn("[缓存数据提取成功但已过期][耗时:{}][cnannel:{}][key:{}][命中:{}][生存:{}/{}]",System.currentTimeMillis()-fr,channel,key,result.getHitCount(),(System.currentTimeMillis() - result.getCreationTime())/1000,result.getTimeToLive()); } result = null; }else{ if(ConfigTable.isDebug() && log.isWarnEnabled()){ log.warn("[缓存数据提取成功并有效][耗时:{}][cnannel:{}][key:{}][命中:{}][生存:{}/{}]",System.currentTimeMillis()-fr,channel,key,result.getHitCount(),(System.currentTimeMillis() - result.getCreationTime())/1000,result.getTimeToLive()); } } } return result; }
Example #8
Source File: CachedParentLdapGroupAuthorityRetrieverTest.java From hesperides with GNU General Public License v3.0 | 6 votes |
@Before public void setUp() { parentGroupsTree = new HashMap<>(); cache = new Cache(new CacheConfiguration(TEST_GROUPS_TREE_CACHE_NAME, 5000) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) .timeToLiveSeconds(2) .diskPersistent(false) .eternal(false) .overflowToDisk(false)); if (cacheManager.cacheExists(TEST_GROUPS_TREE_CACHE_NAME)) { cacheManager.removeCache(TEST_GROUPS_TREE_CACHE_NAME); } cacheManager.addCache(cache); cachedParentLdapGroupAuthorityRetriever = new CachedParentLdapGroupAuthorityRetriever(cache); cachedParentLdapGroupAuthorityRetriever.setParentGroupsDNRetriever(dn -> parentGroupsTree.getOrDefault(dn, new HashSet<>()) ); }
Example #9
Source File: GenericDaoBase.java From cosmic with Apache License 2.0 | 6 votes |
@DB() protected void createCache(final Map<String, ? extends Object> params) { final String value = (String) params.get("cache.size"); if (value != null) { final CacheManager cm = CacheManager.create(); final int maxElements = NumbersUtil.parseInt(value, 0); final int live = NumbersUtil.parseInt((String) params.get("cache.time.to.live"), 300); final int idle = NumbersUtil.parseInt((String) params.get("cache.time.to.idle"), 300); _cache = new Cache(getName(), maxElements, false, live == -1, live == -1 ? Integer.MAX_VALUE : live, idle); cm.addCache(_cache); s_logger.info("Cache created: " + _cache.toString()); } else { _cache = null; } }
Example #10
Source File: EhCache.java From iaf with Apache License 2.0 | 6 votes |
@Override public void open() { Cache configCache = new Cache( getName(), getMaxElementsInMemory(), MemoryStoreEvictionPolicy.fromString(getMemoryStoreEvictionPolicy()), isOverflowToDisk(), null, isEternal(), getTimeToLiveSeconds(), getTimeToIdleSeconds(), isDiskPersistent(), getDiskExpiryThreadIntervalSeconds(), null, null, getMaxElementsOnDisk() ); cacheManager=IbisCacheManager.getInstance(); cache = cacheManager.addCache(configCache); }
Example #11
Source File: DomainAccessControlStoreEhCache.java From joynr with Apache License 2.0 | 6 votes |
protected Cache getCache(CacheId cacheId) { Cache cache = cacheManager.getCache(cacheId.getIdAsString()); if (cache == null) { switch (cacheId) { case MASTER_ACL: case MEDIATOR_ACL: case OWNER_ACL: { cache = createAclCache(cacheId); break; } case DOMAIN_ROLES: { cache = createDrtCache(); break; } default: { break; } } } return cache; }
Example #12
Source File: EhCacheFactoryBean.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ @Override public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
Example #13
Source File: EhCacheSupportTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testBlankCacheManager() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setCacheManagerName("myCacheManager"); assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); assertTrue("Singleton property", cacheManagerFb.isSingleton()); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); Cache myCache1 = cm.getCache("myCache1"); assertTrue("No myCache1 defined", myCache1 == null); } finally { cacheManagerFb.destroy(); } }
Example #14
Source File: EhCacheSupportTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCacheManagerConflict() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setCacheManagerName("myCacheManager"); assertEquals(CacheManager.class, cacheManagerFb.getObjectType()); assertTrue("Singleton property", cacheManagerFb.isSingleton()); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0); Cache myCache1 = cm.getCache("myCache1"); assertTrue("No myCache1 defined", myCache1 == null); EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean(); cacheManagerFb2.setCacheManagerName("myCacheManager"); cacheManagerFb2.afterPropertiesSet(); fail("Should have thrown CacheException because of naming conflict"); } catch (CacheException ex) { // expected } finally { cacheManagerFb.destroy(); } }
Example #15
Source File: EhCacheDataCache.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
public TableModel get( final DataCacheKey key ) { final Cache cache = this.cache; synchronized ( this ) { if ( cache == null ) { return null; } if ( cache.getStatus() != Status.STATUS_ALIVE ) { this.cache = null; return null; } } final Element element = cache.get( key ); if ( element == null ) { return null; } return (TableModel) element.getObjectValue(); }
Example #16
Source File: EnchachePooFactory.java From dble with GNU General Public License v2.0 | 6 votes |
@Override public CachePool createCachePool(String poolName, int cacheSize, int expiredSeconds) { CacheManager cacheManager = CacheManager.create(); Cache enCache = cacheManager.getCache(poolName); if (enCache == null) { CacheConfiguration cacheConf = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone(); cacheConf.setName(poolName); if (cacheConf.getMaxEntriesLocalHeap() != 0) { cacheConf.setMaxEntriesLocalHeap(cacheSize); } else { cacheConf.setMaxBytesLocalHeap(String.valueOf(cacheSize)); } cacheConf.setTimeToIdleSeconds(expiredSeconds); Cache cache = new Cache(cacheConf); cacheManager.addCache(cache); return new EnchachePool(poolName, cache, cacheSize); } else { return new EnchachePool(poolName, enCache, cacheSize); } }
Example #17
Source File: CacheInformationProvider.java From gocd with Apache License 2.0 | 6 votes |
@Override public Map<String, Object> asJson() { LinkedHashMap<String, Object> json = new LinkedHashMap<>(); for (CacheManager cacheManager : CacheManager.ALL_CACHE_MANAGERS) { LinkedHashMap<String, Object> jsonForManager = new LinkedHashMap<>(); json.put(cacheManager.getName(), jsonForManager); for (String cacheName : cacheManager.getCacheNames()) { Cache cache = cacheManager.getCache(cacheName); LinkedHashMap<String, Object> cacheJson = new LinkedHashMap<>(); jsonForManager.put(cacheName, cacheJson); cacheJson.put("Cache configuration information", getCacheConfigurationInformationAsJson(cache)); cacheJson.put("Cache runtime information", getCacheRuntimeInformationAsJson(cache)); } } return json; }
Example #18
Source File: EhcacheFactory.java From springboot-admin with Apache License 2.0 | 6 votes |
static Cache getOrAddCache(String cacheName) { CacheManager cacheManager = getCacheManager(); Cache cache = cacheManager.getCache(cacheName); if (cache == null) { synchronized(locker) { cache = cacheManager.getCache(cacheName); if (cache == null) { log.warn("无法找到缓存 [" + cacheName + "]的配置, 使用默认配置."); cacheManager.addCacheIfAbsent(cacheName); cache = cacheManager.getCache(cacheName); log.debug("缓存 [" + cacheName + "] 启动."); } } } return cache; }
Example #19
Source File: AbstractIntegrationModule.java From freehealth-connector with GNU Affero General Public License v3.0 | 6 votes |
private void initCaching() { LOG.info("INIT CACHE MANAGER"); URL url = this.getClass().getResource("/cache/config/ehcache.xml"); this.cacheManager = CacheManager.newInstance(url); LOG.info("DOES KGSS CACHE EXIST?"); this.kgssCache = this.cacheManager.getCache("KGSS"); if (this.kgssCache == null) { LOG.info("NEW KGSS CACHE"); this.kgssCache = new Cache("KGSS", 0, false, false, 0L, 0L); this.cacheManager.addCache(this.kgssCache); } LOG.info("DOES ETK CACHE EXIST?"); this.etkCache = this.cacheManager.getCache("ETK"); if (this.etkCache == null) { LOG.info("NEW ETK CACHE"); this.etkCache = new Cache("ETK", 0, false, false, 0L, 0L); this.cacheManager.addCache(this.etkCache); } }
Example #20
Source File: EhCacheFactoryBean.java From java-technology-stack with MIT License | 6 votes |
/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ @Override public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
Example #21
Source File: EhcacheApiTest.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
/** * 使用特定的配置添加缓存 */ @Test public void addAndRemove03() { CacheManager manager = CacheManager.create(); // 添加缓存 Cache testCache = new Cache(new CacheConfiguration("testCache3", 5000) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU).eternal(false).timeToLiveSeconds(60) .timeToIdleSeconds(30).diskExpiryThreadIntervalSeconds(0) .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP))); manager.addCache(testCache); // 打印配置信息和状态 Cache test = manager.getCache("testCache3"); System.out.println("cache name:" + test.getCacheConfiguration().getName()); System.out.println("cache status:" + test.getStatus().toString()); System.out.println("maxElementsInMemory:" + test.getCacheConfiguration().getMaxElementsInMemory()); System.out.println("timeToIdleSeconds:" + test.getCacheConfiguration().getTimeToIdleSeconds()); System.out.println("timeToLiveSeconds:" + test.getCacheConfiguration().getTimeToLiveSeconds()); // 删除缓存 manager.removeCache("testCache3"); System.out.println("cache status:" + test.getStatus().toString()); }
Example #22
Source File: EhCacheSupportTests.java From java-technology-stack with MIT License | 6 votes |
public void testCacheManagerFromConfigFile() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass())); cacheManagerFb.setCacheManagerName("myCacheManager"); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1); Cache myCache1 = cm.getCache("myCache1"); assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal()); assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300); } finally { cacheManagerFb.destroy(); } }
Example #23
Source File: EHCacheReplayCache.java From steady with Apache License 2.0 | 5 votes |
public EHCacheReplayCache(String key, Bus b, URL configFileURL) { bus = b; if (bus != null) { bus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); } cacheManager = EHCacheManagerHolder.getCacheManager(bus, configFileURL); CacheConfiguration cc = EHCacheManagerHolder.getCacheConfiguration(key, cacheManager); Ehcache newCache = new Cache(cc); cache = cacheManager.addCacheIfAbsent(newCache); }
Example #24
Source File: ApiRateLimitServiceImpl.java From cosmic with Apache License 2.0 | 5 votes |
@Override public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException { super.configure(name, params); if (_store == null) { // get global configured duration and max values final String isEnabled = _configDao.getValue(Config.ApiLimitEnabled.key()); if (isEnabled != null) { enabled = Boolean.parseBoolean(isEnabled); } final String duration = _configDao.getValue(Config.ApiLimitInterval.key()); if (duration != null) { timeToLive = Integer.parseInt(duration); } final String maxReqs = _configDao.getValue(Config.ApiLimitMax.key()); if (maxReqs != null) { maxAllowed = Integer.parseInt(maxReqs); } // create limit store final EhcacheLimitStore cacheStore = new EhcacheLimitStore(); int maxElements = 10000; final String cachesize = _configDao.getValue(Config.ApiLimitCacheSize.key()); if (cachesize != null) { maxElements = Integer.parseInt(cachesize); } final CacheManager cm = CacheManager.create(); final Cache cache = new Cache("api-limit-cache", maxElements, false, false, timeToLive, timeToLive); cm.addCache(cache); s_logger.info("Limit Cache created with timeToLive=" + timeToLive + ", maxAllowed=" + maxAllowed + ", maxElements=" + maxElements); cacheStore.setCache(cache); _store = cacheStore; } return true; }
Example #25
Source File: EHCacheTokenStore.java From steady with Apache License 2.0 | 5 votes |
public EHCacheTokenStore(String key, Bus b, URL configFileURL) { bus = b; if (bus != null) { b.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); } cacheManager = EHCacheManagerHolder.getCacheManager(bus, configFileURL); // Cannot overflow to disk as SecurityToken Elements can't be serialized CacheConfiguration cc = EHCacheManagerHolder.getCacheConfiguration(key, cacheManager); cc.overflowToDisk(false); //tokens not writable Ehcache newCache = new Cache(cc); cache = cacheManager.addCacheIfAbsent(newCache); }
Example #26
Source File: EhCacheUtil.java From zheng with MIT License | 5 votes |
/** * 获取缓存记录 * @param cacheName * @param key * @return */ public static Object get(String cacheName, String key) { Cache cache = getCache(cacheName); if (null == cache) { return null; } Element cacheElement = cache.get(key); if (null == cacheElement) { return null; } return cacheElement.getObjectValue(); }
Example #27
Source File: BounceProxyEhcacheAdapter.java From joynr with Apache License 2.0 | 5 votes |
@Override public BounceProxyRecord getBounceProxy(String bpId) throws IllegalArgumentException { if (log.isTraceEnabled()) { log.trace("Retrieving bounce proxy {} from cache {}", bpId, cacheName); tracePeers(); } Cache cache = manager.getCache(cacheName); Element element = cache.get(bpId); if (element == null) { throw new IllegalArgumentException("No bounce proxy with ID '" + bpId + "' exists"); } return getBounceProxyRecordFromElement(element); }
Example #28
Source File: GoCacheFactory.java From gocd with Apache License 2.0 | 5 votes |
@Bean(name = "goCache") public GoCache createCache() { CacheManager cacheManager = CacheManager.newInstance(new Configuration().name(getClass().getName())); Cache cache = new Cache(cacheConfiguration); cacheManager.addCache(cache); return new GoCache(cache, transactionSynchronizationManager); }
Example #29
Source File: EHCacheTokenStore.java From steady with Apache License 2.0 | 5 votes |
public EHCacheTokenStore(String key, Bus b, URL configFileURL) { bus = b; if (bus != null) { b.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this); } cacheManager = EHCacheManagerHolder.getCacheManager(bus, configFileURL); // Cannot overflow to disk as SecurityToken Elements can't be serialized CacheConfiguration cc = EHCacheManagerHolder.getCacheConfiguration(key, cacheManager); cc.overflowToDisk(false); //tokens not writable Ehcache newCache = new Cache(cc); cache = cacheManager.addCacheIfAbsent(newCache); }
Example #30
Source File: ApiKeysCacheConfiguration.java From gravitee-gateway with Apache License 2.0 | 5 votes |
@Bean public Cache cache() { CacheManager cacheManager = cacheManager(); Cache apiKeyCache = cacheManager.getCache(API_KEY_CACHE_NAME); if (apiKeyCache == null) { LOGGER.warn("EHCache cache for apikey not found. Fallback to default EHCache configuration"); CacheConfiguration cacheConfiguration = new CacheConfiguration(API_KEY_CACHE_NAME, 1000); cacheManager.addCache(new Cache(cacheConfiguration)); } return cacheManager.getCache(API_KEY_CACHE_NAME); }