Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void testCacheWrite() {
- // create the cache manager from our configuration
- URL url = TestBed.class.getClass().getResource("/resource/ehcache.xml");
- CacheManager manager = CacheManager.create(url);
- // check to see if our cache exits, if it doesn't create it
- Cache testCache = null;
- if (!manager.cacheExists("test")) {
- System.out.println("No cache found. Creating cache...");
- int maxElements = 50000;
- testCache = new Cache("test", maxElements,
- MemoryStoreEvictionPolicy.LFU, true, null, true, 60, 30,
- true, Cache.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS, null);
- manager.addCache(testCache);
- // add an element to persist
- Element el = new Element("key", "value");
- testCache.put(el);
- testCache.flush();
- System.out.println("Cache to disk. Cache size on disk: " +
- testCache.getDiskStoreSize());
- } else {
- // cache exists so load it
- testCache = manager.getCache("test");
- Element el = testCache.get("key");
- if (null == el) {
- System.out.print("Value was null");
- return;
- }
- String value = (String) el.getObjectValue();
- System.out.println("Value is: " + value);
- }
- manager.shutdown();
- }
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
- <diskStore path="C:/mycache"/><!-- java.io.tmpdir -->
- <defaultCache
- maxElementsInMemory="10000"
- eternal="true"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="true"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU" />
- </ehcache>
- No cache found. Creating cache...
- Cache to disk. Cache size on disk: 2
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
- <diskStore path="C:/mycache" />
- <defaultCache
- maxElementsInMemory="10000"
- eternal="true"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="true"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU" />
- <cache
- name="test"
- maxElementsInMemory="500"
- eternal="true"
- overflowToDisk="true"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- diskPersistent="true"
- diskExpiryThreadIntervalSeconds="1"
- memoryStoreEvictionPolicy="LFU" />
- </ehcache>
- public DiskPersistentStorageFactory(Ehcache cache, String diskPath) {
- super(getDataFile(diskPath, cache), cache.getCacheConfiguration().getDiskExpiryThreadIntervalSeconds(),
- cache.getCacheConfiguration().getDiskSpoolBufferSizeMB(), cache.getCacheEventNotificationService(), false);
- indexFile = new File(getDataFile().getParentFile(), getIndexFileName(cache));
- flushTask = new IndexWriteTask(indexFile, cache.getCacheConfiguration().isClearOnFlush());
- if (!getDataFile().exists() || (getDataFile().length() == 0)) {
- LOG.debug("Matching data file missing (or empty) for index file. Deleting index file " + indexFile);
- indexFile.delete();
- } else if (getDataFile().exists() && indexFile.exists()) {
- if (getDataFile().lastModified() > (indexFile.lastModified() + TimeUnit.SECONDS.toMillis(1))) {
- LOG.warn("The index for data file {} is out of date, probably due to an unclean shutdown. "
- + "Deleting index file {}", getDataFile(), indexFile);
- indexFile.delete();
- }
- }
- diskCapacity = cache.getCacheConfiguration().getMaxElementsOnDisk();
- memoryCapacity = cache.getCacheConfiguration().getMaxElementsInMemory();
- memoryPolicy = determineEvictionPolicy(cache.getCacheConfiguration());
- }
- File index = new File( path, name + ".index" );
- File data = new File( path, name + ".data" );
- data.setLastModified( index.lastModified() + 1 );
- CacheManager.getInstance().shutdown();
- manager.shutdown();
- Cache cache = manager.getCache(name);
- if (cache == null) {
- throw new NullPointerException(String.format("no cache with name %s defined, please configure it in %s", name, url));
- }
- // ehcache.xml - shared between different invocations
- CacheManager defaultManager = CacheManager.getInstance();
- // others - avoid calling twice with same argument
- CacheManager manager = CacheManager.create(url);
- public static CacheManager create(URL configurationFileURL) throws CacheException {
- synchronized (CacheManager.class) {
- if (singleton == null) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Creating new CacheManager with config URL: " + configurationFileURL);
- }
- singleton = new CacheManager(configurationFileURL);
- }
- return singleton;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement