net.sf.ehcache.config.PersistenceConfiguration Java Examples

The following examples show how to use net.sf.ehcache.config.PersistenceConfiguration. 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: EhcacheApiTest.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 使用特定的配置添加缓存
 */
@Test
public void addAndRemove03() {
	CacheManager manager = CacheManager.create();

	// 添加缓存
	Cache testCache = new Cache(new CacheConfiguration("testCache3", 5000)
		.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU).eternal(false).timeToLiveSeconds(60)
		.timeToIdleSeconds(30).diskExpiryThreadIntervalSeconds(0)
		.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)));
	manager.addCache(testCache);

	// 打印配置信息和状态
	Cache test = manager.getCache("testCache3");
	System.out.println("cache name:" + test.getCacheConfiguration().getName());
	System.out.println("cache status:" + test.getStatus().toString());
	System.out.println("maxElementsInMemory:" + test.getCacheConfiguration().getMaxElementsInMemory());
	System.out.println("timeToIdleSeconds:" + test.getCacheConfiguration().getTimeToIdleSeconds());
	System.out.println("timeToLiveSeconds:" + test.getCacheConfiguration().getTimeToLiveSeconds());

	// 删除缓存
	manager.removeCache("testCache3");
	System.out.println("cache status:" + test.getStatus().toString());
}
 
Example #2
Source File: GoCacheFactory.java    From gocd with Apache License 2.0 5 votes vote down vote up
public GoCacheFactory(TransactionSynchronizationManager transactionSynchronizationManager,
                      @Value("${cruise.cache.elements.limit}") int maxElementsInMemory,
                      @Value("${cruise.cache.is.eternal}") boolean eternal) {
    this.transactionSynchronizationManager = transactionSynchronizationManager;
    cacheConfiguration = new CacheConfiguration("goCache", maxElementsInMemory)
            .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE))
            .eternal(eternal)
            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
}
 
Example #3
Source File: DefaultCacheManagerProvider.java    From jerseyoauth2 with MIT License 5 votes vote down vote up
public DefaultCacheManagerProvider()
{
	net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
	config.setUpdateCheck(false);
	CacheConfiguration tokenCacheConfiguration = new CacheConfiguration().
			maxEntriesLocalHeap(DEFAULT_MAX_CACHE_ENTRIES).
			name("tokenCache").
			persistence(new PersistenceConfiguration().strategy(Strategy.NONE));
	tokenCacheConfiguration.validateConfiguration();
	config.addCache(tokenCacheConfiguration );
	cacheManager = CacheManager.create(config);
}
 
Example #4
Source File: EhcacheTest.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void basicTest() throws InterruptedException {
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    Configuration conf = new Configuration();
    conf.setMaxBytesLocalHeap("100M");
    CacheManager cacheManager = CacheManager.create(conf);

    //Create a Cache specifying its configuration.
    Cache testCache = //Create a Cache specifying its configuration.
            new Cache(new CacheConfiguration("test", 0).//
                    memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).//
                    eternal(false).//
                    timeToIdleSeconds(86400).//
                    diskExpiryThreadIntervalSeconds(0).//
                    //maxBytesLocalHeap(1000, MemoryUnit.MEGABYTES).//
                    persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE)));

    cacheManager.addCache(testCache);

    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");
    byte[] blob = new byte[(1024 * 80 * 1024)];//400M
    Random random = new Random();
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }

    //        List<String> manyObjects = Lists.newArrayList();
    //        for (int i = 0; i < 10000; i++) {
    //            manyObjects.add(new String("" + i));
    //        }
    //        testCache.put(new Element("0", manyObjects));

    testCache.put(new Element("1", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    blob = new byte[(1024 * 80 * 1024)];//400M
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }

    testCache.put(new Element("2", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.get("2") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    blob = new byte[(1024 * 80 * 1024)];//400M
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }
    testCache.put(new Element("3", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.get("2") == null);
    System.out.println(testCache.get("3") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    cacheManager.shutdown();
}
 
Example #5
Source File: InternalMetricsCache.java    From ambari-metrics with Apache License 2.0 4 votes vote down vote up
private void initialize() {
  // Check in case of contention to avoid ObjectExistsException
  if (isCacheInitialized) {
    throw new RuntimeException("Cannot initialize internal cache twice");
  }

  System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
  System.setProperty("net.sf.ehcache.sizeofengine." + TIMELINE_METRIC_CACHE_MANAGER_NAME,
    "org.apache.ambari.metrics.core.timeline.source.cache.InternalMetricsCacheSizeOfEngine");

  net.sf.ehcache.config.Configuration managerConfig =
    new net.sf.ehcache.config.Configuration();
  managerConfig.setName(TIMELINE_METRIC_CACHE_MANAGER_NAME);

  // Set max heap available to the cache manager
  managerConfig.setMaxBytesLocalHeap(maxHeapPercent);

  //Create a singleton CacheManager using defaults
  CacheManager manager = CacheManager.create(managerConfig);

  LOG.info("Creating Metrics Cache with maxHeapPercent => " + maxHeapPercent);

  // Create a Cache specifying its configuration.
  CacheConfiguration cacheConfiguration = new CacheConfiguration()
    .name(instanceName)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
    .sizeOfPolicy(new SizeOfPolicyConfiguration() // Set sizeOf policy to continue on max depth reached - avoid OOM
      .maxDepth(10000)
      .maxDepthExceededBehavior(SizeOfPolicyConfiguration.MaxDepthExceededBehavior.CONTINUE))
    .eternal(true) // infinite time until eviction
    .persistence(new PersistenceConfiguration()
      .strategy(PersistenceConfiguration.Strategy.NONE.name()));

  cache = new Cache(cacheConfiguration);
  cache.getCacheEventNotificationService().registerListener(new InternalCacheEvictionListener());

  LOG.info("Registering internal metrics cache with provider: name = " +
    cache.getName() + ", guid: " + cache.getGuid());

  manager.addCache(cache);

  isCacheInitialized = true;
}
 
Example #6
Source File: EhcacheTest.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Test
public void basicTest() throws InterruptedException {
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    Configuration conf = new Configuration();
    conf.setMaxBytesLocalHeap("100M");
    CacheManager cacheManager = CacheManager.create(conf);

    //Create a Cache specifying its configuration.
    Cache testCache = //Create a Cache specifying its configuration.
            new Cache(new CacheConfiguration("test", 0).//
                    memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).//
                    eternal(false).//
                    timeToIdleSeconds(86400).//
                    diskExpiryThreadIntervalSeconds(0).//
                    //maxBytesLocalHeap(1000, MemoryUnit.MEGABYTES).//
                    persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE)));

    cacheManager.addCache(testCache);

    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");
    byte[] blob = new byte[(1024 * 80 * 1024)];//400M
    Random random = new Random();
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }

    //        List<String> manyObjects = Lists.newArrayList();
    //        for (int i = 0; i < 10000; i++) {
    //            manyObjects.add(new String("" + i));
    //        }
    //        testCache.put(new Element("0", manyObjects));

    testCache.put(new Element("1", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    blob = new byte[(1024 * 80 * 1024)];//400M
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }

    testCache.put(new Element("2", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.get("2") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    blob = new byte[(1024 * 80 * 1024)];//400M
    for (int i = 0; i < blob.length; i++) {
        blob[i] = (byte) random.nextInt();
    }
    testCache.put(new Element("3", blob));
    System.out.println(testCache.get("1") == null);
    System.out.println(testCache.get("2") == null);
    System.out.println(testCache.get("3") == null);
    System.out.println(testCache.getSize());
    System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes());
    System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M");

    cacheManager.shutdown();
}