private void idleExample() throws InterruptedException { System.out.println("\nIdle example\n"); Cache testCache = EhcacheHelper.createIdleCache(manager, "idleCache"); testCache.put(new Element(0, "String: 0")); testCache.get(0); System.out.println("Hit count: " + testCache.getStatistics().cacheHitCount()); System.out.println("Miss count: " + testCache.getStatistics().cacheMissCount()); Thread.sleep(TimeUnit.SECONDS.toMillis(EhcacheHelper.IDLE_TIME_SEC) - 1); testCache.get(0); System.out.println("Hit count: " + testCache.getStatistics().cacheHitCount()); System.out.println("Miss count: " + testCache.getStatistics().cacheMissCount()); Thread.sleep(TimeUnit.SECONDS.toMillis(EhcacheHelper.IDLE_TIME_SEC) + 1); testCache.get(0); System.out.println("Hit count: " + testCache.getStatistics().cacheHitCount()); System.out.println("Miss count: " + testCache.getStatistics().cacheMissCount()); }
/** * http://www.ehcache.org/documentation/2.8/code-samples.html#creating-caches-programmatically */ EhCacheWrapper(String cacheName) { AppConfiguration.Cache config = AppConfiguration.CONFIG.getCache(); // Create a Cache specifying its configuration cache = new Cache( new CacheConfiguration(cacheName, config.getMaxSize()) .timeToLiveSeconds(TimeUnit.MINUTES.toSeconds(config.getLifeTime())) .eternal(false) .persistence(new PersistenceConfiguration().strategy(Strategy.NONE)) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO) ); // The cache is not usable until it has been added manager.addCache(cache); }
private BuildRights getCachedBuildRightSet(final int activeBuildID, final int userID) { BuildRights buildRights = null; try { final Cache cache = getBuildRightSetCache(activeBuildID); if (cache == null) { return null; } final Element element = cache.get(new Integer(userID)); if (element != null) { buildRights = (BuildRights) element.getValue(); } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("e: " + e); } } // if (log.isDebugEnabled()) log.debug("cached rightSet for build ID " + activeBuildID + ", user ID " + userID + " : " + rightSet); return buildRights; }
@Around("execution(* org.packt.aop.transaction.dao.impl.EmployeeDaoImpl.getEmployees(..))") public Object cacheMonitor(ProceedingJoinPoint joinPoint) throws Throwable { logger.info("executing " + joinPoint.getSignature().getName()); Cache cache = cacheManager.getCache("employeesCache"); logger.info("cache detected is " + cache.getName()); logger.info("begin caching....."); String key = joinPoint.getSignature().getName(); logger.info(key); if(cache.get(key) == null){ logger.info("caching new Object....."); Object result = joinPoint.proceed(); cache.put(new Element(key, result)); return result; }else{ logger.info("getting cached Object....."); return cache.get(key).getObjectValue(); } }
@Test public void verifyEhCacheTicketRegistryWithClearPass() { final Cache serviceTicketsCache = new Cache("serviceTicketsCache", 200, false, false, 100, 100); final Cache ticketGrantingTicketCache = new Cache("ticketGrantingTicketCache", 200, false, false, 100, 100); final CacheManager manager = new CacheManager(this.getClass().getClassLoader().getResourceAsStream("ehcacheClearPass.xml")); manager.addCache(serviceTicketsCache); manager.addCache(ticketGrantingTicketCache); final Map<String, String> map = new HashMap<>(); final TicketRegistry ticketRegistry = new EhCacheTicketRegistry(serviceTicketsCache, ticketGrantingTicketCache); final TicketRegistryDecorator decorator = new TicketRegistryDecorator(ticketRegistry, map); assertNotNull(decorator); assertEquals(decorator.serviceTicketCount(), 0); assertEquals(decorator.sessionCount(), 0); manager.removalAll(); manager.shutdown(); }
@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); } }
/** * Instantiates a new EhCache ticket registry. * * @param ticketCache the ticket cache * @param cipher the cipher */ public EhCacheTicketRegistry(final Cache ticketCache, final CipherExecutor cipher) { this.ehcacheTicketsCache = ticketCache; setCipherExecutor(cipher); LOGGER.info("Setting up Ehcache Ticket Registry..."); Assert.notNull(this.ehcacheTicketsCache, "Ehcache Tickets cache cannot nbe null"); if (LOGGER.isDebugEnabled()) { final CacheConfiguration config = this.ehcacheTicketsCache.getCacheConfiguration(); LOGGER.debug("TicketCache.maxEntriesLocalHeap=[{}]", config.getMaxEntriesLocalHeap()); LOGGER.debug("TicketCache.maxEntriesLocalDisk=[{}]", config.getMaxEntriesLocalDisk()); LOGGER.debug("TicketCache.maxEntriesInCache=[{}]", config.getMaxEntriesInCache()); LOGGER.debug("TicketCache.persistenceConfiguration=[{}]", config.getPersistenceConfiguration().getStrategy()); LOGGER.debug("TicketCache.synchronousWrites=[{}]", config.getPersistenceConfiguration().getSynchronousWrites()); LOGGER.debug("TicketCache.timeToLive=[{}]", config.getTimeToLiveSeconds()); LOGGER.debug("TicketCache.timeToIdle=[{}]", config.getTimeToIdleSeconds()); LOGGER.debug("TicketCache.cacheManager=[{}]", this.ehcacheTicketsCache.getCacheManager().getName()); } }
@Bean public RevocationChecker crlDistributionPointRevocationChecker() { final X509Properties x509 = casProperties.getAuthn().getX509(); final Cache cache = new Cache("CRL".concat(UUID.randomUUID().toString()), x509.getCacheMaxElementsInMemory(), x509.isCacheDiskOverflow(), x509.isCacheEternal(), x509.getCacheTimeToLiveSeconds(), x509.getCacheTimeToIdleSeconds()); return new CRLDistributionPointRevocationChecker( x509.isCheckAll(), getRevocationPolicy(x509.getCrlUnavailablePolicy()), getRevocationPolicy(x509.getCrlExpiredPolicy()), cache, crlFetcher(), x509.isThrowOnFetchFailure()); }
public Cache getOrAddCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { synchronized (locker) { cache = cacheManager.getCache(cacheName); if (cache == null) { log.warn("Could not find cache config [" + cacheName + "], using default."); cacheManager.addCacheIfAbsent(cacheName); cache = cacheManager.getCache(cacheName); if (cacheEventListener != null) { cache.getCacheEventNotificationService().registerListener(cacheEventListener); } } } } return cache; }
static Cache createLifeCache(CacheManager manager, String name) { CacheConfiguration configuration = new CacheConfiguration(name, MAX_ENTRIES); configuration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO) .timeToLiveSeconds(LIFE_TIME_SEC); Cache cache = new Cache(configuration); manager.addCache(cache); return cache; }
static Cache createEternalCache(CacheManager manager, String name) { return createCache( manager, name, configuration -> configuration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO) .eternal(true)); }
public int listCount(Cnd c) { Long num = 0L; Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(CACHE_COUNT_KEY) == null) { num = Long.valueOf(this.count(c)); cache.put(new Element(CACHE_COUNT_KEY, num)); }else{ num = (Long)cache.get(CACHE_COUNT_KEY).getObjectValue(); } return num.intValue(); }
@Test public void getCrlFromLdapWithNoCaching() throws Exception { for (int i = 0; i < 10; i++) { CacheManager.getInstance().removeAllCaches(); final Cache cache = new Cache("crlCache-1", 100, false, false, 20, 10); CacheManager.getInstance().addCache(cache); final CRLDistributionPointRevocationChecker checker = new CRLDistributionPointRevocationChecker(cache, fetcher); checker.setThrowOnFetchFailure(true); checker.setUnavailableCRLPolicy(new AllowRevocationPolicy()); final X509Certificate cert = CertUtils.readCertificate(new ClassPathResource("ldap-crl.crt")); checker.init(); checker.check(cert); } }
public Data get(String dataID) { Cache tmpCache = cacheManager.getCache("tmp"); if(tmpCache.get(dataID) != null) return (Data) tmpCache.get(dataID).getValue(); else return dataDao.get(dataID); }
public String getObjectID(String dataName,Weaver weaver){ Cache tmpNameCache = cacheManager.getCache("tmpName"); dataName = dataName.replace(" ", "_"); dataName = dataName.replace("?", "_"); dataName = dataName.replace("#", "_"); dataName = dataName.trim(); Element element = tmpNameCache.get(weaver.getId()+"/"+dataName); if(element == null) return ""; return (String)element.getValue(); }
/** * 删除缓存记录 * @param cacheName * @param key * @return */ public static boolean remove(String cacheName, String key) { Cache cache = getCache(cacheName); if (null == cache) { return false; } return cache.remove(key); }
public List<Productlevel> queryCache(Condition c,Page p) { List<Productlevel> list_productlevel = null; String cacheKey = "productlevel_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_productlevel = this.query(c, p); cache.put(new Element(cacheKey, list_productlevel)); }else{ list_productlevel = (List<Productlevel>)cache.get(cacheKey).getObjectValue(); } return list_productlevel; }
/** * 新增缓存记录 * @param cacheName * @param key * @param value */ public static void put(String cacheName, String key, Object value) { Cache cache = getCache(cacheName); if (null != cache) { Element element = new Element(key, value); cache.put(element); } }
public List<Contract> queryCache(Cnd c,Page p) { List<Contract> list_contract = null; String cacheKey = "contract_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_contract = this.query(c, p); cache.put(new Element(cacheKey, list_contract)); }else{ list_contract = (List<Contract>)cache.get(cacheKey).getObjectValue(); } return list_contract; }
public List<Target> queryCache(Condition c,Page p) { List<Target> list_target = null; String cacheKey = "target_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_target = this.query(c, p); cache.put(new Element(cacheKey, list_target)); }else{ list_target = (List<Target>)cache.get(cacheKey).getObjectValue(); } return list_target; }
public void monitor(Cache cache) { MetricRegistry registry = RegistryService.getMetricRegistry(); cache.registerCacheExtension(new CacheLifecycleListener(cache, registry) { @Override public void dispose() throws CacheException { getTimers.remove(cache.getName()); putTimers.remove(cache.getName()); super.dispose(); } }); }
public void onGetExit(Cache cache) { endTimedOperation(GET_OPERATION, () -> { return getTimers.computeIfAbsent(cache.getName(), s -> { String[] tags = new String[]{"op", "get", "cache", cache.getName()}; TagEncodedMetricName metricName = ROOT_NAME.submetric("ops").withTags(tags); return getTimer(metricName); }); }); }
static Cache createIdleCache(CacheManager manager, String name) { return createCache( manager, name, configuration -> configuration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO) .timeToIdleSeconds(IDLE_TIME_SEC)); }
public List<Linkcustomer> queryCache(Cnd c,Page p) { List<Linkcustomer> list_linkcustomer = null; String cacheKey = "linkcustomer_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_linkcustomer = this.query(c, p); cache.put(new Element(cacheKey, list_linkcustomer)); }else{ list_linkcustomer = (List<Linkcustomer>)cache.get(cacheKey).getObjectValue(); } return list_linkcustomer; }
public List<Technicalpoint> queryCache(Condition c,Page p) { List<Technicalpoint> list_technicalpoint = null; String cacheKey = "technicalpoint_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_technicalpoint = this.query(c, p); cache.put(new Element(cacheKey, list_technicalpoint)); }else{ list_technicalpoint = (List<Technicalpoint>)cache.get(cacheKey).getObjectValue(); } return list_technicalpoint; }
public List<Func> queryCache(Cnd c,Page p) { List<Func> list_func = null; String cacheKey = "func_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_func = this.query(c, p); cache.put(new Element(cacheKey, list_func)); }else{ list_func = (List<Func>)cache.get(cacheKey).getObjectValue(); } return list_func; }
public List<Customertype> queryCache(Cnd c,Page p) { List<Customertype> list_customertype = null; String cacheKey = "customertype_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_customertype = this.query(c, p); cache.put(new Element(cacheKey, list_customertype)); }else{ list_customertype = (List<Customertype>)cache.get(cacheKey).getObjectValue(); } return list_customertype; }
public List<Product> queryCache(Condition c,Page p) { List<Product> list_product = null; String cacheKey = "product_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_product = this.query(c, p); cache.put(new Element(cacheKey, list_product)); }else{ list_product = (List<Product>)cache.get(cacheKey).getObjectValue(); } return list_product; }
public List<Aporia> queryCache(Condition c,Page p) { List<Aporia> list_aporia = null; String cacheKey = "aporia_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_aporia = this.query(c, p); cache.put(new Element(cacheKey, list_aporia)); }else{ list_aporia = (List<Aporia>)cache.get(cacheKey).getObjectValue(); } return list_aporia; }
public List<Jindu> queryCache(Cnd c,Page p) { List<Jindu> list_jindu = null; String cacheKey = "jindu_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_jindu = this.query(c, p); cache.put(new Element(cacheKey, list_jindu)); }else{ list_jindu = (List<Jindu>)cache.get(cacheKey).getObjectValue(); } return list_jindu; }
public List<Publish> queryCache(Cnd c,Page p) { List<Publish> list_publish = null; String cacheKey = "publish_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_publish = this.query(c, p); cache.put(new Element(cacheKey, list_publish)); }else{ list_publish = (List<Publish>)cache.get(cacheKey).getObjectValue(); } return list_publish; }
public void onPutExit(Cache cache) { endTimedOperation(PUT_OPERATION, () -> { return putTimers.computeIfAbsent(cache.getName(), s -> { String[] tags = new String[]{"op", "put", "cache", cache.getName()}; TagEncodedMetricName metricName = ROOT_NAME.submetric("ops").withTags(tags); return getTimer(metricName); }); }); }
/** * 删除全部缓存记录 * @param cacheName * @return */ public static void removeAll(String cacheName) { Cache cache = getCache(cacheName); if (null != cache) { cache.removeAll(); } }
public List<Xinyong> queryCache(Cnd c,Page p) { List<Xinyong> list_xinyong = null; String cacheKey = "xinyong_list_" + p.getPageCurrent(); Cache cache = CacheManager.getInstance().getCache(CACHE_NAME); if(cache.get(cacheKey) == null) { list_xinyong = this.query(c, p); cache.put(new Element(cacheKey, list_xinyong)); }else{ list_xinyong = (List<Xinyong>)cache.get(cacheKey).getObjectValue(); } return list_xinyong; }