private ServletCache(String name, int type) { this.type = type; Configuration managerConfig = new Configuration(); CacheConfiguration mqCf = new CacheConfiguration(name, CacheConfig .getConfig().getMaxElementsInMemory()); mqCf.setEternal(true); DiskStoreConfiguration dsCf = new DiskStoreConfiguration(); dsCf.setPath(CacheConfig.getConfig().getDiskStorePath()); managerConfig.addDiskStore(dsCf); mqCf.setMaxElementsOnDisk(0); mqCf.setMaxEntriesLocalHeap(CacheConfig.getConfig() .getMaxElementsInMemory()); mqCf.persistence(new PersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)); mqCf.setTransactionalMode("OFF"); mqCf.setMemoryStoreEvictionPolicy(CacheConfig.getConfig() .getMemoryStoreEvictionPolicy()); managerConfig.addCache(mqCf); cacheManager = new CacheManager(managerConfig); cache = cacheManager.getCache(name); }
private CacheManager getCacheManager() { if (manager == null) { Configuration config = new Configuration(); CacheConfiguration cacheconfig = new CacheConfiguration(getName(), maxElementsInMemory); cacheconfig.setDiskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds); cacheconfig.setDiskPersistent(diskPersistent); cacheconfig.setEternal(eternal); cacheconfig.setMaxElementsOnDisk(maxElementsOnDisk); cacheconfig.setMemoryStoreEvictionPolicyFromObject(memoryStoreEvictionPolicy); cacheconfig.setOverflowToDisk(overflowToDisk); cacheconfig.setTimeToIdleSeconds(timeToIdleSeconds); cacheconfig.setTimeToLiveSeconds(timeToLiveSeconds); DiskStoreConfiguration diskStoreConfigurationParameter = new DiskStoreConfiguration(); diskStoreConfigurationParameter.setPath(getPath().getAbsolutePath()); config.addDiskStore(diskStoreConfigurationParameter); config.setDefaultCacheConfiguration(cacheconfig); manager = new CacheManager(config); } return manager; }
public void afterPropertiesSet() throws IOException, CacheException { log.info("Initializing EHCache CacheManager"); Configuration config = null; if (this.configLocation != null) { config = ConfigurationFactory .parseConfiguration(this.configLocation.getInputStream()); if (this.diskStoreLocation != null) { DiskStoreConfiguration dc = new DiskStoreConfiguration(); dc.setPath(this.diskStoreLocation.getFile().getAbsolutePath()); try { config.addDiskStore(dc); } catch (ObjectExistsException e) { log.warn("if you want to config distStore in spring," + " please remove diskStore in config file!", e); } } } if (config != null) { this.cacheManager = new CacheManager(config); } else { this.cacheManager = new CacheManager(); } if (this.cacheManagerName != null) { this.cacheManager.setName(this.cacheManagerName); } }
@SuppressWarnings("deprecation") @Override public void afterPropertiesSet() throws Exception { Cache.ID = key + "." + Dates.newDateStringOfFormatDateTimeSSSNoneSpace(); Cache.HOST = Utils.getLocalHostIP(); Cache.CACHE_STORE = key + spliter + "cache" + spliter + "store"; Cache.CACHE_STORE_SYNC = Cache.CACHE_STORE + spliter + "sync"; if (this.localEnabled) { Configuration configuration = new Configuration(); configuration.setName(Cache.ID); configuration.setMaxBytesLocalHeap(localMaxBytesLocalHeap); configuration.setMaxBytesLocalDisk(localMaxBytesLocalDisk); // DiskStore // 每次启动设置新的文件地址,以避免重启期间一级缓存未同步,以及单机多应用启动造成EhcacheManager重复的问题. DiskStoreConfiguration dsc = new DiskStoreConfiguration(); dsc.setPath(localStoreLocation + Cache.ID); configuration.diskStore(dsc); // DefaultCache CacheConfiguration defaultCacheConfiguration = new CacheConfiguration(); defaultCacheConfiguration.setEternal(false); defaultCacheConfiguration.setOverflowToDisk(true); defaultCacheConfiguration.setDiskPersistent(false); defaultCacheConfiguration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU); defaultCacheConfiguration.setDiskExpiryThreadIntervalSeconds(localDiskExpiryThreadIntervalSeconds); // 默认false,使用引用.设置为true,避免外部代码修改了缓存对象.造成EhCache的缓存对象也随之改变 // 但是设置为true后,将引起element的tti不自动刷新.如果直接新建element去覆盖原值.则本地ttl和远程ttl会产生一定的误差. // 因此,使用时放弃手动覆盖方式刷新本地tti,当本地tti过期后,自动从Redis中再获取即可. defaultCacheConfiguration.copyOnRead(true); defaultCacheConfiguration.copyOnWrite(true); defaultCacheConfiguration.setTimeToIdleSeconds(localTimeToIdleSeconds); defaultCacheConfiguration.setTimeToLiveSeconds(localTimeToLiveSeconds); configuration.setDefaultCacheConfiguration(defaultCacheConfiguration); configuration.setDynamicConfig(false); configuration.setUpdateCheck(false); this.cacheManager = new CacheManager(configuration); this.cacheSync = new RedisPubSubSync(this);// 使用Redis Topic发送订阅缓存变更消息 } }
/** * 构造函数 * * @param block * --是否为阻塞队列,true是阻塞队列,false是非阻塞队列 * @param cacheLength * --内存中队列长度,值>0; * @param persistDirPath * --持久数据落地目录(<b>注意:一个队列对应一个目录路径,多个队列共享一个目录路径,是不允许的,会出现数据不一致的情况! * < /b>) */ public PersistQueue(final boolean block, final int cacheLength, final String persistDirPath) { if (cacheLength < 0) { throw new AppRuntimeException("cacheLength must >0!"); } if (block) { this.tmpQueue = new BlockQueue(); } else { this.tmpQueue = new NoBlockConcurrentQueue(); } psKeys = new ConcurrentLinkedQueue<Long>(); hcName = "pq_" + persistDirPath.hashCode(); Configuration managerConfig = new Configuration(); CacheConfiguration mqCf = new CacheConfiguration(hcName, cacheLength); mqCf.setEternal(true); // mqCf.setDiskStorePath(persistDirPath); mqCf.setMaxElementsOnDisk(0); mqCf.setTransactionalMode("OFF"); mqCf.setMemoryStoreEvictionPolicy("LFU"); // mqCf.setDiskPersistent(true); // mqCf.setMaxElementsInMemory(cacheLength); mqCf.setMaxEntriesLocalHeap(cacheLength); // mqCf.setOverflowToDisk(true); mqCf.persistence(new PersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.LOCALRESTARTABLE)); managerConfig.addCache(mqCf); DiskStoreConfiguration dsCf = new DiskStoreConfiguration(); dsCf.setPath(persistDirPath); managerConfig.addDiskStore(dsCf); cacheManager = new CacheManager(managerConfig); cache = cacheManager.getCache(hcName); Element e = cache.get(hcName); if (null == e) { count = new AtomicLong(0); } else { Long cv = (Long) e.getObjectValue(); count = new AtomicLong(cv.longValue()); } }
/** * 构造函数 * * @param block * --是否为阻塞队列,true是阻塞队列,false是非阻塞队列 * @param cacheLength * --内存中队列长度,值>0; * @param persistDirPath * --数据落地目录(<b>注意:一个队列对应一个目录路径,多个队列共享一个目录路径,是不允许的,会出现数据不一致的情况! < * /b>) */ public CacheQueue(final boolean block, final int cacheLength, final String persistDirPath) { if (cacheLength < 0) { throw new AppRuntimeException("cacheLength must >0!"); } if (block) { this.tmpQueue = new BlockQueue(); } else { this.tmpQueue = new NoBlockConcurrentQueue(); } psKeys = new ConcurrentLinkedQueue<Long>(); hcName = "cq-" + persistDirPath.hashCode(); Configuration managerConfig = new Configuration(); CacheConfiguration mqCf = new CacheConfiguration(hcName, cacheLength); mqCf.setEternal(true); // mqCf.setDiskStorePath(persistDirPath); mqCf.setMaxElementsOnDisk(0); mqCf.setTransactionalMode("OFF"); mqCf.setMemoryStoreEvictionPolicy("LFU"); // mqCf.setDiskPersistent(true); // mqCf.setMaxElementsInMemory(cacheLength); mqCf.setMaxEntriesLocalHeap(cacheLength); // mqCf.setOverflowToDisk(true); mqCf.persistence(new PersistenceConfiguration() .strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)); managerConfig.addCache(mqCf); DiskStoreConfiguration dsCf = new DiskStoreConfiguration(); dsCf.setPath(persistDirPath); managerConfig.addDiskStore(dsCf); managerConfig.setName(hcName); // cacheManager = new CacheManager(managerConfig); cacheManager = CacheManager.newInstance(managerConfig); cache = cacheManager.getCache(hcName); count = new AtomicLong(0); }
/** * Eh cache manager. * * @return the net.sf.ehcache. cache manager */ @SuppressWarnings("deprecation") @Bean(destroyMethod="shutdown") public net.sf.ehcache.CacheManager ehCacheManager() { CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setMemoryStoreEvictionPolicy("LRU"); cacheConfiguration.setMaxEntriesLocalHeap(1000); cacheConfiguration.setEternal(false); cacheConfiguration.setOverflowToDisk(true); cacheConfiguration.setMaxElementsInMemory(1000); cacheConfiguration.setMaxElementsOnDisk(10000); // cacheConfiguration.setDiskPersistent(false); // cacheConfiguration.setStatistics(false); cacheConfiguration.setTimeToIdleSeconds(300); cacheConfiguration.setTimeToLiveSeconds(600); net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration(); DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath(System.getProperty("java.io.tmpdir")); config.diskStore(diskStoreConfiguration); config.setDynamicConfig(true); config.setMonitoring("autodetect"); config.setUpdateCheck(false); config.defaultCache(cacheConfiguration); CacheSettings.Items.list.parallelStream().forEach(it -> { config.addCache(loadCache(it)); }); return net.sf.ehcache.CacheManager.newInstance(config); }
private void initializeEhcacheDiskStore(Environment environment) { DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath(environment.getProperty(HmpProperties.EHCACHE_DATA_DIR)); Configuration configuration = ConfigurationFactory.parseConfiguration(); configuration.addDiskStore(diskStoreConfiguration); CacheManager.newInstance(configuration); }
public PortalServlet() throws IOException { logger.info("Brisk CMS started!"); homeDirectory = new File("/Users/dmi/Documents/Labs/TestCMSTemplates"); configDirectory = new File(homeDirectory, "conf"); if (!homeDirectory.exists()) throw new FileNotFoundException("Could not find brisk home directory."); cmsContext = new CMSContext(); cmsContext.settings = new Settings(new File(configDirectory, "cms.properties")); cmsContext.homeDirectory = homeDirectory; cmsContext.sitesDirectory = new File(homeDirectory, "sites"); cmsContext.imagesDirectory = new File(homeDirectory, "images"); // Cache setup cmsContext.vhostCache = new VhostCache(cmsContext.settings.vhostCacheLifeTime); DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration().path(new File(homeDirectory, "cache").getPath()); Configuration configuration = new Configuration().name("cmscache").diskStore(diskStoreConfiguration); cmsContext.cacheManager = CacheManager.create(configuration); cmsContext.cacheManager.addCache(createImageCache("imagecache", cmsContext.settings)); cmsContext.cacheManager.addCache(createPageCache("pagecache", cmsContext.settings)); cmsContext.client = ClientFactory.create(); }
public EhCacheMemory() { DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath("/tmp"); Configuration configuration = new Configuration(); configuration.addDiskStore(diskStoreConfiguration); CacheConfiguration cacheConf = new CacheConfiguration("cache_memory", Integer.MAX_VALUE).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).overflowToDisk(false).eternal(false).diskExpiryThreadIntervalSeconds(0); CacheManager cacheManager = new CacheManager(configuration); cacheManager.addCache(new Cache(cacheConf)); cache = cacheManager.getEhcache("cache_memory"); }
public EhCacheDisk() { DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath("/tmp"); Configuration configuration = new Configuration(); configuration.addDiskStore(diskStoreConfiguration); CacheConfiguration cacheConf = new CacheConfiguration("cache_disk", 0).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).overflowToDisk(false).eternal(false).diskExpiryThreadIntervalSeconds(0); CacheManager cacheManager = new CacheManager(configuration); cacheManager.addCache(new Cache(cacheConf)); cache = cacheManager.getEhcache("cache_disk"); }
private IbisCacheManager() { Configuration cacheManagerConfig = new Configuration(); String cacheDir = AppConstants.getInstance().getResolvedProperty(CACHE_DIR_KEY); if (StringUtils.isNotEmpty(cacheDir)) { log.debug("setting cache directory to ["+cacheDir+"]"); DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration(); diskStoreConfiguration.setPath(cacheDir); cacheManagerConfig.addDiskStore(diskStoreConfiguration); } CacheConfiguration defaultCacheConfig = new CacheConfiguration(); cacheManagerConfig.addDefaultCache(defaultCacheConfig); cacheManager= new CacheManager(cacheManagerConfig); }