Java Code Examples for net.sf.ehcache.Cache#get()
The following examples show how to use
net.sf.ehcache.Cache#get() .
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: 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 2
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 3
Source File: CacheListenerAspect.java From Spring-5.0-Cookbook with MIT License | 6 votes |
@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(); } }
Example 4
Source File: EhcacheImpl.java From framework with Apache License 2.0 | 6 votes |
/** * Description: <br> * * @author 王伟<br> * @taskId <br> * @param nodeName * @param type * @return <br> */ @SuppressWarnings("unchecked") @Override public <T> Map<String, T> getNode(final String nodeName, final Class<T> type) { Cache cache = getCache(nodeName); List<String> keys = cache.getKeysWithExpiryCheck(); Map<String, T> cacheMap = new HashMap<String, T>(); if (CollectionUtils.isNotEmpty(keys)) { for (String key : keys) { Element element = cache.get(key); Object value = (element != null ? element.getObjectValue() : null); if (value != null && type != null && !type.isInstance(value)) { throw new IllegalStateException( "Cached value is not of required type [" + type.getName() + "]: " + value); } cacheMap.put(key, (T) value); } } return cacheMap; }
Example 5
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 6
Source File: BounceProxyEhcacheAdapter.java From joynr with Apache License 2.0 | 6 votes |
@Override public void updateChannelAssignment(String ccid, BounceProxyInformation bpInfo) throws IllegalArgumentException { if (log.isTraceEnabled()) { log.trace("Update channel assignment for bounce proxy {} in cache {}", bpInfo.getId(), cacheName); tracePeers(); } Cache cache = manager.getCache(cacheName); Element element = cache.get(bpInfo.getId()); if (element == null) { throw new IllegalArgumentException("No bounce proxy with ID '" + bpInfo.getId() + "' exists"); } BounceProxyRecord bpRecord = getBounceProxyRecordFromElement(element); bpRecord.addAssignedChannel(ccid); Element updatedElement = new Element(bpInfo.getId(), bpRecord); cache.put(updatedElement); }
Example 7
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 8
Source File: TestHtmlReport.java From javamelody with Apache License 2.0 | 5 votes |
/** Test. * @throws IOException e */ @Test public void testCache() throws IOException { final String cacheName = "test 1"; final CacheManager cacheManager = CacheManager.getInstance(); cacheManager.addCache(cacheName); // test empty cache name in the cache keys link: // cacheManager.addCache("") does nothing, but cacheManager.addCache(new Cache("", ...)) works cacheManager.addCache(new Cache("", 1000, true, false, 10000, 10000, false, 10000)); final String cacheName2 = "test 2"; try { final Cache cache = cacheManager.getCache(cacheName); cache.put(new Element(1, Math.random())); cache.get(1); cache.get(0); cacheManager.addCache(cacheName2); final Cache cache2 = cacheManager.getCache(cacheName2); cache2.getCacheConfiguration().setOverflowToDisk(false); cache2.getCacheConfiguration().setEternal(true); cache2.getCacheConfiguration().setMaxElementsInMemory(0); // JavaInformations doit être réinstancié pour récupérer les caches final List<JavaInformations> javaInformationsList2 = Collections .singletonList(new JavaInformations(null, true)); final HtmlReport htmlReport = new HtmlReport(collector, null, javaInformationsList2, Period.TOUT, writer); htmlReport.toHtml(null, null); assertNotEmptyAndClear(writer); setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, "false"); htmlReport.toHtml(null, null); assertNotEmptyAndClear(writer); } finally { setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, null); cacheManager.removeCache(cacheName); cacheManager.removeCache(cacheName2); } }
Example 9
Source File: CaseServlet.java From skywalking with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cache cache = cacheManager.getCache("testCache"); String objectKey = "dataKey"; Element el = new Element(objectKey, "2"); // EhcacheOperateElementInterceptor cache.put(el); // EhcacheOperateObjectInterceptor cache.get(objectKey); // EhcacheOperateAllInterceptor cache.putAll(Arrays.asList(new Element[] {el})); // EhcacheLockInterceptor try { boolean success = cache.tryReadLockOnKey(objectKey, 300); } catch (InterruptedException e) { } finally { cache.releaseReadLockOnKey(objectKey); } // EhcacheCacheNameInterceptor cacheManager.addCacheIfAbsent("testCache2"); Cache cloneCache = cacheManager.getCache("testCache2"); // EhcacheOperateElementInterceptor cloneCache.put(el); PrintWriter printWriter = resp.getWriter(); printWriter.write("success"); printWriter.flush(); printWriter.close(); }
Example 10
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 11
Source File: BounceProxyEhcacheAdapter.java From joynr with Apache License 2.0 | 5 votes |
@Override public boolean containsBounceProxy(String bpId) { if (log.isTraceEnabled()) { log.trace("containsBounceProxy {} in cache {}", bpId, cacheName); tracePeers(); } Cache cache = manager.getCache(cacheName); return cache.get(bpId) != null; }
Example 12
Source File: EHCacheManager.java From javalite with Apache License 2.0 | 5 votes |
@Override public Object getCache(String group, String key) { try { createIfMissing(group); Cache c = cacheManager.getCache(group); return c.get(key) == null ? null : c.get(key).getObjectValue(); } catch (Exception e) { LogFilter.log(LOGGER, LogLevel.WARNING, "{}", e, e); return null; } }
Example 13
Source File: EhcacheService.java From jeecg with Apache License 2.0 | 5 votes |
@Override public Object get(String cacheName, Object key) { log.debug(" EhcacheService get cacheName: [{}] , key: [{}]",cacheName,key); Cache cache = manager.getCache(cacheName); if (cache != null) { Element element = cache.get(key); if (element != null) { return element.getObjectValue(); } } return null; }
Example 14
Source File: EhcacheTest.java From blog with BSD 2-Clause "Simplified" License | 4 votes |
public static void main(String[] args) { try { // 创建一个CacheManager实例 CacheManager manager = new CacheManager(); // 增加一个cache manager.addCache("cardCache"); // 获取所有cache名称 String[] cacheNamesForManager = manager.getCacheNames(); int iLen = cacheNamesForManager.length; System.out.println("缓存名称列表:----------------------------"); for (int i = 0; i < iLen; i++) { System.out.println(cacheNamesForManager[i].toString()); } // 获取cache对象 Cache cache = manager.getCache("cardCache"); // create Element element = new Element("username", "howsky"); cache.put(element); // get Element element_get = cache.get("username"); Object value = element_get.getObjectValue(); System.out.println(value.toString()); Element element_get2 = cache.get("username"); System.out.println(element_get2==element_get); // update cache.put(new Element("username", "howsky.net")); // get Element element_new = cache.get("username"); System.out.println(element_new==element_get); Object value_new = element_new.getObjectValue(); System.out.println(value_new.toString()); cache.remove("username"); // 移除cache manager.removeCache("cardCache"); // 关闭CacheManager manager.shutdown(); } catch (Exception e) { System.out.println(e.getMessage()); } }
Example 15
Source File: EhcacheApiTest.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
/** * 使用Ehcache默认配置(classpath下的ehcache.xml)获取单例的CacheManager实例 */ @Test public void operation() { CacheManager manager = CacheManager.newInstance("src/test/resources/ehcache/ehcache.xml"); // 获得Cache的引用 Cache cache = manager.getCache("users"); // 将一个Element添加到Cache cache.put(new Element("key1", "value1")); // 获取Element,Element类支持序列化,所以下面两种方法都可以用 Element element1 = cache.get("key1"); // 获取非序列化的值 System.out.println("key=" + element1.getObjectKey() + ", value=" + element1.getObjectValue()); // 获取序列化的值 System.out.println("key=" + element1.getKey() + ", value=" + element1.getValue()); // 更新Cache中的Element cache.put(new Element("key1", "value2")); Element element2 = cache.get("key1"); System.out.println("key=" + element2.getObjectKey() + ", value=" + element2.getObjectValue()); // 获取Cache的元素数 System.out.println("cache size:" + cache.getSize()); // 获取MemoryStore的元素数 System.out.println("MemoryStoreSize:" + cache.getMemoryStoreSize()); // 获取DiskStore的元素数 System.out.println("DiskStoreSize:" + cache.getDiskStoreSize()); // 移除Element cache.remove("key1"); System.out.println("cache size:" + cache.getSize()); // 关闭当前CacheManager对象 manager.shutdown(); // 关闭CacheManager单例实例 CacheManager.getInstance().shutdown(); }