net.sf.ehcache.Ehcache Java Examples
The following examples show how to use
net.sf.ehcache.Ehcache.
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: EhCacheSupportTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testEhCacheFactoryBeanWithSelfPopulatingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(key -> key); assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof SelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); } finally { cacheManagerFb.destroy(); } }
Example #2
Source File: SakaiSecurity.java From sakai with Educational Community License v2.0 | 6 votes |
void logCacheState(String operator) { if (cacheDebug) { String name = m_callCache.getName(); net.sf.ehcache.Ehcache ehcache = m_callCache.unwrap(Ehcache.class); // DEBUGGING ONLY StringBuilder entriesSB = new StringBuilder(); List keys = ehcache.getKeysWithExpiryCheck(); // only current keys entriesSB.append(" * keys(").append(keys.size()).append("):").append(new ArrayList<Object>(keys)).append("\n"); Collection<Element> entries = ehcache.getAll(keys).values(); int countMaps = 0; for (Element element : entries) { if (element == null) continue; int count = 0; countMaps += count; if (cacheDebugDetailed) { entriesSB.append(" ").append(element.getObjectKey()).append(" => (").append(count).append(")").append(element.getObjectValue()).append("\n"); } } log.info("SScache:"+name+":: "+operator+" ::\n entries(Ehcache[key => payload],"+keys.size()+" + "+countMaps+" = "+(keys.size()+countMaps)+"):\n"+entriesSB); } }
Example #3
Source File: ElementFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
@SuppressWarnings("unchecked") public List<Route> listRouteWithParallel(String id) throws Exception { List<Route> list = new ArrayList<>(); Ehcache cache = ApplicationCache.instance().getCache(Parallel.class); String cacheKey = ApplicationCache.concreteCacheKey(id, Parallel.class.getCanonicalName()); Element element = cache.get(cacheKey); if (null != element) { list = (List<Route>) element.getObjectValue(); } else { EntityManager em = this.entityManagerContainer().get(Route.class); Parallel parallel = this.get(id, Parallel.class); if (null != parallel) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Route> cq = cb.createQuery(Route.class); Root<Route> root = cq.from(Route.class); Predicate p = root.get(Route_.id).in(parallel.getRouteList()); list = em.createQuery(cq.where(p).orderBy(cb.asc(root.get(Route_.orderNumber)))).getResultList(); if (!list.isEmpty()) { cache.put(new Element(cacheKey, list)); } } } return list; }
Example #4
Source File: ElementFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
@SuppressWarnings("unchecked") public List<Route> listRouteWithManual(String id) throws Exception { List<Route> list = new ArrayList<>(); Ehcache cache = ApplicationCache.instance().getCache(Manual.class); String cacheKey = ApplicationCache.concreteCacheKey(id, Manual.class.getCanonicalName()); Element element = cache.get(cacheKey); if (null != element) { list = (List<Route>) element.getObjectValue(); } else { EntityManager em = this.entityManagerContainer().get(Route.class); Manual manual = this.get(id, Manual.class); if (null != manual) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Route> cq = cb.createQuery(Route.class); Root<Route> root = cq.from(Route.class); Predicate p = root.get(Route_.id).in(manual.getRouteList()); list = em.createQuery(cq.where(p).orderBy(cb.asc(root.get(Route_.orderNumber)))).getResultList(); if (!list.isEmpty()) { cache.put(new Element(cacheKey, list)); } } } return list; }
Example #5
Source File: CacheInformations.java From javamelody with Apache License 2.0 | 6 votes |
private String buildConfiguration(Object cache) { final StringBuilder sb = new StringBuilder(); // getCacheConfiguration() et getMaxElementsOnDisk() n'existent pas en ehcache 1.2 final CacheConfiguration config = ((Ehcache) cache).getCacheConfiguration(); sb.append("ehcache [maxElementsInMemory = ").append(config.getMaxElementsInMemory()); final boolean overflowToDisk = config.isOverflowToDisk(); sb.append(", overflowToDisk = ").append(overflowToDisk); if (overflowToDisk) { sb.append(", maxElementsOnDisk = ").append(config.getMaxElementsOnDisk()); } final boolean eternal = config.isEternal(); sb.append(", eternal = ").append(eternal); if (!eternal) { sb.append(", timeToLiveSeconds = ").append(config.getTimeToLiveSeconds()); sb.append(", timeToIdleSeconds = ").append(config.getTimeToIdleSeconds()); sb.append(", memoryStoreEvictionPolicy = ") .append(config.getMemoryStoreEvictionPolicy()); } sb.append(", diskPersistent = ").append(config.isDiskPersistent()); sb.append(']'); return sb.toString(); }
Example #6
Source File: EhCacheSupportTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testEhCacheFactoryBeanWithBlockingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setBlocking(true); assertEquals(cacheFb.getObjectType(), BlockingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof BlockingCache); } finally { cacheManagerFb.destroy(); } }
Example #7
Source File: EhCacheSupportTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testEhCacheFactoryBeanWithBlockingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setBlocking(true); assertEquals(cacheFb.getObjectType(), BlockingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof BlockingCache); } finally { cacheManagerFb.destroy(); } }
Example #8
Source File: AbstractFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
@SuppressWarnings("unchecked") protected <T extends JpaObject> List<T> pick(List<String> flags, Class<T> clz) throws Exception { List<T> list = new ArrayList<>(); if (null == flags || flags.isEmpty()) { return list; } Ehcache cache = ApplicationCache.instance().getCache(clz); Map<Object, Element> map = cache.getAll(flags); if (map.size() == flags.size()) { map.values().stream().forEach(o -> { list.add((T) o.getObjectValue()); }); } else { List<T> os = this.entityManagerContainer().flag(flags, clz); EntityManager em = this.entityManagerContainer().get(clz); os.stream().forEach(o -> { em.detach(o); list.add(o); cache.put(new Element(o.getId(), o)); }); } return list; }
Example #9
Source File: EhCacheFactory.java From nextreports-server with Apache License 2.0 | 6 votes |
protected void createCache(String name, int expirationTime) { synchronized (this.getClass()) { cacheManager.addCache(name); Ehcache cache = cacheManager.getEhcache(name); CacheConfiguration config = cache.getCacheConfiguration(); config.setEternal(false); config.setTimeToLiveSeconds(expirationTime); // config.setTimeToIdleSeconds(60); // config.setMaxElementsInMemory(10000); // config.setMaxElementsOnDisk(1000000); BlockingCache blockingCache = new BlockingCache(cache); cacheManager.replaceCacheWithDecoratedCache(cache, blockingCache); } }
Example #10
Source File: ElementFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
@SuppressWarnings("unchecked") protected <T extends JpaObject> T pick( AppInfo appInfo, String flag, Class<T> clz) throws Exception { if (null == appInfo) { return null; } Ehcache cache = ApplicationCache.instance().getCache(clz); T t = null; String cacheKey = ApplicationCache.concreteCacheKey(appInfo.getId(), flag); Element element = cache.get(cacheKey); if (null != element) { if (null != element.getObjectValue()) { t = (T) element.getObjectValue(); } } else { t = this.entityManagerContainer().restrictFlag(flag, clz, CategoryInfo.appId_FIELDNAME, appInfo.getId()); if (t != null) { this.entityManagerContainer().get(clz).detach(t); } cache.put(new Element(cacheKey, t)); } return t; }
Example #11
Source File: EhCacheFactoryBean.java From lams with GNU General Public License v2.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 #12
Source File: AbstractAutoCreatingEhCacheCacheManagerTest.java From find with MIT License | 5 votes |
@Test public void getMissingCacheDefault() { final Ehcache ehcache = mock(Ehcache.class); when(ehcache.getStatus()).thenReturn(Status.STATUS_ALIVE); when(cacheManager.getEhcache(anyString())).thenReturn(ehcache); assertNotNull(autoCreatingEhCacheCacheManager.getMissingCache("SomeName")); }
Example #13
Source File: ElementFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public List<Mapping> listMappingEffectiveWithApplicationAndProcess(String application, String process) throws Exception { final List<Mapping> list = new ArrayList<>(); Ehcache cache = ApplicationCache.instance().getCache(Mapping.class); String cacheKey = ApplicationCache.concreteCacheKey(application, process, Application.class.getName(), Process.class.getName()); Element element = cache.get(cacheKey); if (null != element) { list.addAll((List<Mapping>) element.getObjectValue()); } else { EntityManager em = this.entityManagerContainer().get(Mapping.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Mapping> cq = cb.createQuery(Mapping.class); Root<Mapping> root = cq.from(Mapping.class); Predicate p = cb.equal(root.get(Mapping_.enable), true); p = cb.and(p, cb.equal(root.get(Mapping_.application), application)); p = cb.and(p, cb.or(cb.equal(root.get(Mapping_.process), process), cb.equal(root.get(Mapping_.process), ""), cb.isNull(root.get(Mapping_.process)))); List<Mapping> os = em.createQuery(cq.where(p)).getResultList(); os.stream().collect(Collectors.groupingBy(o -> { return o.getApplication() + o.getTableName(); })).forEach((k, v) -> { list.add(v.stream().filter(i -> StringUtils.isNotEmpty(i.getProcess())).findFirst().orElse(v.get(0))); }); if (!list.isEmpty()) { cache.put(new Element(cacheKey, list)); } } return list; }
Example #14
Source File: CacheManagerWrapper.java From cms with Apache License 2.0 | 5 votes |
@Override public int size() { if (springCache.getNativeCache() instanceof Ehcache) { Ehcache ehcache = (Ehcache) springCache.getNativeCache(); return ehcache.getSize(); } throw new UnsupportedOperationException("invoke spring cache abstract size method not supported"); }
Example #15
Source File: DeviceClassCacheImpl.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
@Autowired public DeviceClassCacheImpl(final ClusterCache clusterCache, @Qualifier("deviceClassEhcache") final Ehcache ehcache, @Qualifier("deviceClassEhcacheLoader") final CacheLoader cacheLoader, @Qualifier("deviceClassCacheLoader") final C2monCacheLoader c2monCacheLoader, @Qualifier("deviceClassDAO") final SimpleCacheLoaderDAO<DeviceClass> cacheLoaderDAO, final CacheProperties properties) { super(clusterCache, ehcache, cacheLoader, c2monCacheLoader, cacheLoaderDAO, properties); }
Example #16
Source File: ElementFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") public <T extends JpaObject> List<T> listWithProcess(Class<T> clz, Process process) throws Exception { List<T> list = new ArrayList<>(); Ehcache cache = ApplicationCache.instance().getCache(clz); String cacheKey = ApplicationCache.concreteCacheKey("listWithProcess", process.getId(), clz.getName()); Element element = cache.get(cacheKey); if (null != element) { Object obj = element.getObjectValue(); if (null != obj) { list = (List<T>) obj; } } else { EntityManager em = this.entityManagerContainer().get(clz); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(clz); Root<T> root = cq.from(clz); Predicate p = cb.equal(root.get(Agent.process_FIELDNAME), process.getId()); cq.select(root).where(p); List<T> os = em.createQuery(cq).getResultList(); for (T t : os) { em.detach(t); list.add(t); } /* 将object改为unmodifiable */ list = Collections.unmodifiableList(list); cache.put(new Element(cacheKey, list)); } return list; }
Example #17
Source File: AclConfig.java From Spring-Security-Third-Edition with MIT License | 5 votes |
@Bean public Ehcache ehcache(){ EhCacheFactoryBean cacheFactoryBean = new EhCacheFactoryBean(); cacheFactoryBean.setCacheManager(cacheManager()); cacheFactoryBean.setCacheName("aclCache"); cacheFactoryBean.setMaxBytesLocalHeap("1M"); cacheFactoryBean.setMaxEntriesLocalHeap(0L); cacheFactoryBean.afterPropertiesSet(); return cacheFactoryBean.getObject(); }
Example #18
Source File: QueryViewFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public QueryView pick(String flag, Application application, ExceptionWhen exceptionWhen) throws Exception { Ehcache cache = ApplicationCache.instance().getCache(QueryView.class); String cacheKey = flag + "#" + application.getId(); Element element = cache.get(cacheKey); QueryView o = null; if (null != element) { if (null != element.getObjectValue()) { o = (QueryView) element.getObjectValue(); } } else { EntityManager em = this.entityManagerContainer().get(QueryView.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<QueryView> cq = cb.createQuery(QueryView.class); Root<QueryView> root = cq.from(QueryView.class); Predicate p = cb.equal(root.get(QueryView_.application), application.getId()); p = cb.and(p, cb.or(cb.equal(root.get(QueryView_.id), flag), cb.equal(root.get(QueryView_.alias), flag), cb.equal(root.get(QueryView_.name), flag))); cq.select(root).where(p); List<QueryView> list = em.createQuery(cq).getResultList(); if (list.isEmpty()) { cache.put(new Element(cacheKey, o)); } else if (list.size() == 1) { o = list.get(0); em.detach(o); cache.put(new Element(cacheKey, o)); } else { throw new Exception("multiple queryView with flag:" + flag + "."); } } if (o == null && Objects.equals(ExceptionWhen.not_found, exceptionWhen)) { throw new Exception("can not find queryView with flag:" + flag + "."); } return o; }
Example #19
Source File: QueryStatFactory.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public QueryStat pick(String flag, Application application, ExceptionWhen exceptionWhen) throws Exception { Ehcache cache = ApplicationCache.instance().getCache(QueryStat.class); String cacheKey = flag + "#" + application.getId(); Element element = cache.get(cacheKey); QueryStat o = null; if (null != element) { if (null != element.getObjectValue()) { o = (QueryStat) element.getObjectValue(); } } else { EntityManager em = this.entityManagerContainer().get(QueryStat.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<QueryStat> cq = cb.createQuery(QueryStat.class); Root<QueryStat> root = cq.from(QueryStat.class); Predicate p = cb.equal(root.get(QueryStat_.application), application.getId()); p = cb.and(p, cb.or(cb.equal(root.get(QueryStat_.id), flag), cb.equal(root.get(QueryStat_.alias), flag), cb.equal(root.get(QueryStat_.name), flag))); cq.select(root).where(p); List<QueryStat> list = em.createQuery(cq).getResultList(); if (list.isEmpty()) { cache.put(new Element(cacheKey, o)); } else if (list.size() == 1) { o = list.get(0); em.detach(o); cache.put(new Element(cacheKey, o)); } else { throw new Exception("multiple queryStat with flag:" + flag + "."); } } if (o == null && Objects.equals(ExceptionWhen.not_found, exceptionWhen)) { throw new Exception("can not find queryStat with flag:" + flag + "."); } return o; }
Example #20
Source File: EhcacheDriver.java From rebuild with GNU General Public License v3.0 | 5 votes |
@Override public void putx(String key, V value, int seconds) { Element el = new Element(key, value); if (seconds > -1) { el.setTimeToLive(seconds); } ((Ehcache) cache().getNativeCache()).put(el); }
Example #21
Source File: SiteCacheImpl.java From sakai with Educational Community License v2.0 | 5 votes |
public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException { if (log.isDebugEnabled()) { log.debug("ehcache event: notifyElementUpdated: "+element.getKey()); } updateSiteCacheStatistics(); }
Example #22
Source File: JobInstanceSqlMapDao.java From gocd with Apache License 2.0 | 5 votes |
private static Ehcache createCacheIfRequired(String cacheName) { final CacheManager instance = CacheManager.newInstance(new Configuration().name(cacheName)); synchronized (instance) { if (!instance.cacheExists(cacheName)) { instance.addCache(new net.sf.ehcache.Cache(cacheConfiguration(cacheName))); } return instance.getCache(cacheName); } }
Example #23
Source File: AbstractTagCache.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
/** * Constructor. */ public AbstractTagCache(final ClusterCache clusterCache, final Ehcache ehcache, final CacheLoader cacheLoader, final C2monCacheLoader c2monCacheLoader, final SimpleCacheLoaderDAO<T> cacheLoaderDAO, final CacheProperties properties) { super(clusterCache, ehcache, cacheLoader, c2monCacheLoader, cacheLoaderDAO, properties); listenersWithSupervision = new ArrayList<>(); listenerLock = new ReentrantReadWriteLock(); }
Example #24
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 #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: 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 #27
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 #28
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 #29
Source File: CommandTagCacheImpl.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
@Autowired public CommandTagCacheImpl(final ClusterCache clusterCache, @Qualifier("commandTagEhcache") final Ehcache ehcache, @Qualifier("commandTagEhcacheLoader") final CacheLoader cacheLoader, @Qualifier("commandTagCacheLoader") final C2monCacheLoader c2monCacheLoader, @Qualifier("commandTagDAO") final SimpleCacheLoaderDAO<CommandTag> cacheLoaderDAO, final CacheProperties properties) { super(clusterCache, ehcache, cacheLoader, c2monCacheLoader, cacheLoaderDAO, properties); }
Example #30
Source File: EhCacheFactoryBean.java From spring-analysis-note with MIT License | 5 votes |
/** * Decorate the given Cache, if necessary. * @param cache the raw Cache object, based on the configuration of this FactoryBean * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Ehcache cache) { if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); } else { return new SelfPopulatingCache(cache, this.cacheEntryFactory); } } if (this.blocking) { return new BlockingCache(cache); } return cache; }