net.sf.ehcache.store.MemoryStoreEvictionPolicy Java Examples

The following examples show how to use net.sf.ehcache.store.MemoryStoreEvictionPolicy. 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: CachedParentLdapGroupAuthorityRetrieverTest.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() {
    parentGroupsTree = new HashMap<>();
    cache = new Cache(new CacheConfiguration(TEST_GROUPS_TREE_CACHE_NAME, 5000)
            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
            .timeToLiveSeconds(2)
            .diskPersistent(false)
            .eternal(false)
            .overflowToDisk(false));
    if (cacheManager.cacheExists(TEST_GROUPS_TREE_CACHE_NAME)) {
        cacheManager.removeCache(TEST_GROUPS_TREE_CACHE_NAME);
    }
    cacheManager.addCache(cache);
    cachedParentLdapGroupAuthorityRetriever = new CachedParentLdapGroupAuthorityRetriever(cache);
    cachedParentLdapGroupAuthorityRetriever.setParentGroupsDNRetriever(dn ->
            parentGroupsTree.getOrDefault(dn, new HashSet<>())
    );
}
 
Example #3
Source File: EhCache.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public void open() {
	Cache configCache = new Cache(
			getName(),
			getMaxElementsInMemory(),
			MemoryStoreEvictionPolicy.fromString(getMemoryStoreEvictionPolicy()),
			isOverflowToDisk(),
			null,
			isEternal(),
			getTimeToLiveSeconds(),
			getTimeToIdleSeconds(),
			isDiskPersistent(),
			getDiskExpiryThreadIntervalSeconds(),
			null,
			null,
			getMaxElementsOnDisk()
			);
	cacheManager=IbisCacheManager.getInstance();
	cache = cacheManager.addCache(configCache);
}
 
Example #4
Source File: CacheTemplate.java    From smart-cache with Apache License 2.0 5 votes vote down vote up
@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发送订阅缓存变更消息
    }
}
 
Example #5
Source File: InMemoryConfiguration.java    From find with MIT License 5 votes vote down vote up
@Bean
public CacheConfiguration defaultCacheConfiguration() {
    return new CacheConfiguration()
            .eternal(false)
            .maxElementsInMemory(1000)
            .overflowToDisk(false)
            .diskPersistent(false)
            .timeToIdleSeconds(0)
            .timeToLiveSeconds(30 * 60)
            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
}
 
Example #6
Source File: EhCache.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(String ownerName) throws ConfigurationException {
	super.configure(ownerName);
	if (isDiskPersistent() && !isOverflowToDisk()) {
		log.info("setting overflowToDisk true, to support diskPersistent=true");
		setOverflowToDisk(true);
	}
	MemoryStoreEvictionPolicy.fromString(getMemoryStoreEvictionPolicy()); 
}
 
Example #7
Source File: EhcacheMap.java    From concurrentlinkedhashmap with Apache License 2.0 5 votes vote down vote up
public EhcacheMap(MemoryStoreEvictionPolicy evictionPolicy, CacheFactory builder) {
  CacheConfiguration config = new CacheConfiguration(DEFAULT_CACHE_NAME, builder.maximumCapacity);
  config.setMemoryStoreEvictionPolicyFromObject(evictionPolicy);
  CacheManager cacheManager = new CacheManager();
  cache = new Cache(config);
  cache.setCacheManager(cacheManager);
  cache.initialise();
}
 
Example #8
Source File: ApiEhcache.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void createCache(AppConstants ac) {
	if (isDiskPersistent() && !isOverflowToDisk()) {
		log.info("setting overflowToDisk true, to support diskPersistent=true");
		setOverflowToDisk(true);
	}

	String DiskStorePath = null;
	String cacheDir = ac.getResolvedProperty("etag.ehcache.dir");
	if (StringUtils.isNotEmpty(cacheDir)) {
		DiskStorePath = cacheDir;
	}

	Cache configCache = new Cache(
			KEY_CACHE_NAME,
			maxElementsInMemory,
			MemoryStoreEvictionPolicy.fromString(memoryStoreEvictionPolicy),
			isOverflowToDisk(),
			DiskStorePath,
			isEternal(),
			0,
			0,
			isDiskPersistent(),
			diskExpiryThreadIntervalSeconds,
			null,
			null,
			getMaxElementsOnDisk()
		);
	cache = cacheManager.addCache(configCache);
}
 
Example #9
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 #10
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 #11
Source File: SessionCache.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
@PostConstruct
@SuppressWarnings("unused")
private void init() throws Exception {

    // Init Cache
    Configuration c = new Configuration();
    c.setName("sessionManager");
    manager = CacheManager.create(c);
    CacheConfiguration config = new CacheConfiguration();
    config.eternal(false).name(CACHE_NAME).maxEntriesLocalHeap(maxEntries).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).timeToIdleSeconds(timeToIdle).timeToLiveSeconds(timeToLive);
    if (!manager.cacheExists(CACHE_NAME)) {
        manager.addCache(new Cache(config));
    }
    sessions = manager.getCache(CACHE_NAME);

    // Init JMS replication
    ConnectionFactory factory = new ActiveMQConnectionFactory(this.url);
    Connection conn = factory.createConnection();
    conn.start();
    jmsSession = (TopicSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Topic topic = jmsSession.createTopic(TOPIC_NAME);
    tp = jmsSession.createPublisher(topic);

    listener = new Thread() { // Thread created once upon container startup
        @Override
        public void run() {
            try {
                MessageConsumer consumer = jmsSession.createConsumer(topic);
                while (live) {

                    ObjectMessage msg = (ObjectMessage) consumer.receive();

                    LOG.debug("Received replication message: {}", msg);

                    if (PUT.equals(msg.getStringProperty(ACTION_KEY))) {
                        sessions.put(new Element(msg.getStringProperty(TOKEN_KEY), msg.getObject()));
                    } else if (REMOVE.equals(msg.getStringProperty(ACTION_KEY))) {
                        sessions.remove(msg.getStringProperty(TOKEN_KEY));
                    }

                }
            } catch (JMSException e) {
                LOG.error("Error reading replication message", e);
            }
        }
    };

    listener.start();
}
 
Example #12
Source File: JobInstanceSqlMapDao.java    From gocd with Apache License 2.0 4 votes vote down vote up
private static CacheConfiguration cacheConfiguration(String cacheName) {
    return new CacheConfiguration(cacheName, 1024)
            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
            .overflowToDisk(false)
            .diskPersistent(false);
}
 
Example #13
Source File: PipelineSqlMapDao.java    From gocd with Apache License 2.0 4 votes vote down vote up
private static CacheConfiguration cacheConfiguration(String cacheName) {
    return new CacheConfiguration(cacheName, 1024)
            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
            .overflowToDisk(false)
            .diskPersistent(false);
}
 
Example #14
Source File: GoCacheTest.java    From gocd with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Cache cache = new Cache(new CacheConfiguration(getClass().getName(), 100).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU));
    cacheManager.addCache(cache);
    this.goCache = new GoCache(cache, mock(TransactionSynchronizationManager.class));
}
 
Example #15
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();
}
 
Example #16
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 #17
Source File: EhCacheFactoryBean.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Set the memory style eviction policy for this cache.
 * <p>Supported values are "LRU", "LFU" and "FIFO", according to the
 * constants defined in EHCache's MemoryStoreEvictionPolicy class.
 * Default is "LRU".
 */
public void setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
	Assert.notNull(memoryStoreEvictionPolicy, "memoryStoreEvictionPolicy must not be null");
	this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
}
 
Example #18
Source File: CacheConfig.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * Set the memory style eviction policy for this cache. Supported values are "LRU", "LFU" and "FIFO", according to the constants defined in EHCache's
 * MemoryStoreEvictionPolicy class. Default is "LRU".
 */
public void setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
    this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
}
 
Example #19
Source File: CacheConfig.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * Set the memory style eviction policy for this cache. Supported values are "LRU", "LFU" and "FIFO", according to the constants defined in EHCache's
 * MemoryStoreEvictionPolicy class. Default is "LRU".
 */
public void setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
    this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
}
 
Example #20
Source File: EhCacheFactoryBean.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Set the memory style eviction policy for this cache.
 * <p>Supported values are "LRU", "LFU" and "FIFO", according to the
 * constants defined in EHCache's MemoryStoreEvictionPolicy class.
 * Default is "LRU".
 */
public void setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
	Assert.notNull(memoryStoreEvictionPolicy, "memoryStoreEvictionPolicy must not be null");
	this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
}