Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. private static void testCacheWrite() {
  2.  
  3. // create the cache manager from our configuration
  4. URL url = TestBed.class.getClass().getResource("/resource/ehcache.xml");
  5. CacheManager manager = CacheManager.create(url);
  6. // check to see if our cache exits, if it doesn't create it
  7. Cache testCache = null;
  8. if (!manager.cacheExists("test")) {
  9. System.out.println("No cache found. Creating cache...");
  10. int maxElements = 50000;
  11. testCache = new Cache("test", maxElements,
  12. MemoryStoreEvictionPolicy.LFU, true, null, true, 60, 30,
  13. true, Cache.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS, null);
  14. manager.addCache(testCache);
  15. // add an element to persist
  16. Element el = new Element("key", "value");
  17. testCache.put(el);
  18. testCache.flush();
  19. System.out.println("Cache to disk. Cache size on disk: " +
  20. testCache.getDiskStoreSize());
  21. } else {
  22. // cache exists so load it
  23. testCache = manager.getCache("test");
  24. Element el = testCache.get("key");
  25. if (null == el) {
  26. System.out.print("Value was null");
  27. return;
  28. }
  29. String value = (String) el.getObjectValue();
  30. System.out.println("Value is: " + value);
  31. }
  32. manager.shutdown();
  33. }
  34.  
  35. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36. xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  37. <diskStore path="C:/mycache"/><!-- java.io.tmpdir -->
  38. <defaultCache
  39. maxElementsInMemory="10000"
  40. eternal="true"
  41. timeToIdleSeconds="120"
  42. timeToLiveSeconds="120"
  43. overflowToDisk="true"
  44. maxElementsOnDisk="10000000"
  45. diskPersistent="true"
  46. diskExpiryThreadIntervalSeconds="120"
  47. memoryStoreEvictionPolicy="LRU" />
  48. </ehcache>
  49.  
  50. No cache found. Creating cache...
  51. Cache to disk. Cache size on disk: 2
  52.  
  53. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  54.  
  55. <diskStore path="C:/mycache" />
  56.  
  57. <defaultCache
  58. maxElementsInMemory="10000"
  59. eternal="true"
  60. timeToIdleSeconds="120"
  61. timeToLiveSeconds="120"
  62. overflowToDisk="true"
  63. maxElementsOnDisk="10000000"
  64. diskPersistent="true"
  65. diskExpiryThreadIntervalSeconds="120"
  66. memoryStoreEvictionPolicy="LRU" />
  67.  
  68. <cache
  69. name="test"
  70. maxElementsInMemory="500"
  71. eternal="true"
  72. overflowToDisk="true"
  73. timeToIdleSeconds="300"
  74. timeToLiveSeconds="600"
  75. diskPersistent="true"
  76. diskExpiryThreadIntervalSeconds="1"
  77. memoryStoreEvictionPolicy="LFU" />
  78.  
  79. </ehcache>
  80.  
  81. public DiskPersistentStorageFactory(Ehcache cache, String diskPath) {
  82. super(getDataFile(diskPath, cache), cache.getCacheConfiguration().getDiskExpiryThreadIntervalSeconds(),
  83. cache.getCacheConfiguration().getDiskSpoolBufferSizeMB(), cache.getCacheEventNotificationService(), false);
  84.  
  85. indexFile = new File(getDataFile().getParentFile(), getIndexFileName(cache));
  86. flushTask = new IndexWriteTask(indexFile, cache.getCacheConfiguration().isClearOnFlush());
  87.  
  88. if (!getDataFile().exists() || (getDataFile().length() == 0)) {
  89. LOG.debug("Matching data file missing (or empty) for index file. Deleting index file " + indexFile);
  90. indexFile.delete();
  91. } else if (getDataFile().exists() && indexFile.exists()) {
  92. if (getDataFile().lastModified() > (indexFile.lastModified() + TimeUnit.SECONDS.toMillis(1))) {
  93. LOG.warn("The index for data file {} is out of date, probably due to an unclean shutdown. "
  94. + "Deleting index file {}", getDataFile(), indexFile);
  95. indexFile.delete();
  96. }
  97. }
  98.  
  99. diskCapacity = cache.getCacheConfiguration().getMaxElementsOnDisk();
  100. memoryCapacity = cache.getCacheConfiguration().getMaxElementsInMemory();
  101. memoryPolicy = determineEvictionPolicy(cache.getCacheConfiguration());
  102. }
  103.  
  104. File index = new File( path, name + ".index" );
  105. File data = new File( path, name + ".data" );
  106.  
  107. data.setLastModified( index.lastModified() + 1 );
  108.  
  109. CacheManager.getInstance().shutdown();
  110.  
  111. manager.shutdown();
  112.  
  113. Cache cache = manager.getCache(name);
  114. if (cache == null) {
  115. throw new NullPointerException(String.format("no cache with name %s defined, please configure it in %s", name, url));
  116. }
  117.  
  118. // ehcache.xml - shared between different invocations
  119. CacheManager defaultManager = CacheManager.getInstance();
  120. // others - avoid calling twice with same argument
  121. CacheManager manager = CacheManager.create(url);
  122.  
  123. public static CacheManager create(URL configurationFileURL) throws CacheException {
  124. synchronized (CacheManager.class) {
  125. if (singleton == null) {
  126. if (LOG.isDebugEnabled()) {
  127. LOG.debug("Creating new CacheManager with config URL: " + configurationFileURL);
  128. }
  129. singleton = new CacheManager(configurationFileURL);
  130.  
  131. }
  132. return singleton;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement