Java 类net.sf.ehcache.config.PersistenceConfiguration 实例源码

项目:xsharing-services-router    文件:EhCacheWrapper.java   
/**
 * http://www.ehcache.org/documentation/2.8/code-samples.html#creating-caches-programmatically
 */
EhCacheWrapper(String cacheName) {
    AppConfiguration.Cache config = AppConfiguration.CONFIG.getCache();

    // Create a Cache specifying its configuration
    cache = new Cache(
            new CacheConfiguration(cacheName, config.getMaxSize())
                    .timeToLiveSeconds(TimeUnit.MINUTES.toSeconds(config.getLifeTime()))
                    .eternal(false)
                    .persistence(new PersistenceConfiguration().strategy(Strategy.NONE))
                    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO)
    );

    // The cache is not usable until it has been added
    manager.addCache(cache);
}
项目:stallion-core    文件:SmartQueryCache.java   
public static void initCache(String bucket) {
    if (manager == null) {
        load();
    }
    if (manager.getCache(bucket) != null) {
        return;
    }
    CacheConfiguration config = new CacheConfiguration(bucket, 10000);
    //config.setDiskPersistent(false);
    //config.setMaxElementsOnDisk(0);
    //config.setMaxBytesLocalDisk(0L);
    config.setOverflowToOffHeap(false);
    PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
    persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.NONE);
    config.persistence(persistenceConfiguration);
    manager.addCache(new Cache(config));
}
项目:ncwms    文件:NcwmsCatalogue.java   
public NcwmsCatalogue(NcwmsConfig config) throws IOException {
    super(config, new SimpleLayerNameMapper());
    this.styleCatalogue = SldTemplateStyleCatalogue.getStyleCatalogue();

    if (cacheManager.cacheExists(CACHE_NAME) == false) {
        /*
         * Configure cache
         */
        CacheConfiguration cacheConfig = new CacheConfiguration(CACHE_NAME, MAX_HEAP_ENTRIES)
                .eternal(true)
                .memoryStoreEvictionPolicy(EVICTION_POLICY)
                .persistence(new PersistenceConfiguration().strategy(PERSISTENCE_STRATEGY))
                .transactionalMode(TRANSACTIONAL_MODE);
        dynamicDatasetCache = new Cache(cacheConfig);
        cacheManager.addCache(dynamicDatasetCache);
    } else {
        dynamicDatasetCache = cacheManager.getCache(CACHE_NAME);
    }
}
项目:BJAF3.x    文件:ServletCache.java   
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);
}
项目:xslweb    文件:WebApp.java   
public void setCacheValue(String cacheName, String keyName, Collection<Attribute> attrs, int tti, int ttl) {
  CacheManager manager = Context.getInstance().getCacheManager();
  Cache cache = manager.getCache(cacheName);
  if (cache == null) {      
    cache = new Cache(
        new CacheConfiguration(cacheName, 1000)
          .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
          .eternal(false)
          .timeToLiveSeconds(60)
          .timeToIdleSeconds(60)
          .diskExpiryThreadIntervalSeconds(120)
          .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP)));
    manager.addCache(cache);
  }            
  cache.put(new net.sf.ehcache.Element(keyName, attrs, false, tti, ttl));
}
项目:rapidminer-onomastics-extension    文件:ExtractOriginOperator.java   
private Cache getOrCreateCache() {
    // Create a singleton CacheManager using defaults
    CacheManager manager = CacheManager.create();
    // Create a Cache specifying its configuration
    Cache c = manager.getCache(CACHE_name);
    if (c == null) {
        c = new Cache(new CacheConfiguration(CACHE_name,
                CACHE_maxEntriesLocalHeap)
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
                .eternal(true)
                .persistence(
                        new PersistenceConfiguration()
                                .strategy(Strategy.LOCALTEMPSWAP)));
        manager.addCache(c);
    }
    return c;
}
项目:rapidminer-onomastics-extension    文件:ParseNameOperator.java   
private Cache getOrCreateCache() {
    // Create a singleton CacheManager using defaults
    CacheManager manager = CacheManager.create();
    // Create a Cache specifying its configuration
    Cache c = manager.getCache(CACHE_name);
    if (c == null) {
        c = new Cache(new CacheConfiguration(CACHE_name,
                CACHE_maxEntriesLocalHeap)
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
                .eternal(true)
                .persistence(
                        new PersistenceConfiguration()
                                .strategy(Strategy.LOCALTEMPSWAP)));
        manager.addCache(c);
    }
    return c;
}
项目:rapidminer-onomastics-extension    文件:ExtractGenderOperator.java   
private Cache getOrCreateCache() {
    // Create a singleton CacheManager using defaults
    CacheManager manager = CacheManager.create();
    // Create a Cache specifying its configuration
    Cache c = manager.getCache(CACHE_name);
    if (c == null) {
        c = new Cache(new CacheConfiguration(CACHE_name,
                CACHE_maxEntriesLocalHeap)
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
                .eternal(true)
                .persistence(
                        new PersistenceConfiguration()
                                .strategy(Strategy.LOCALTEMPSWAP)));
        manager.addCache(c);
    }
    return c;
}
项目:CAM    文件:Caches.java   
public static Cache createMemCache(String name , long maxEntriesLocalHeap, long timeToLiveSeconds,
                                   long timeToIdleSeconds){
    CacheConfiguration config = new CacheConfiguration();
    config.setName(name);
    config.setEternal(false);
    config.setMaxEntriesLocalHeap(maxEntriesLocalHeap);
    config.setTimeToLiveSeconds(timeToLiveSeconds);
    config.setTimeToIdleSeconds(timeToIdleSeconds);
    config.setCopyOnRead(false);
    config.setCopyOnWrite(true);
    config.setMemoryStoreEvictionPolicy("LRU");

    CopyStrategyConfiguration copyConfig = new CopyStrategyConfiguration();
    copyConfig.setClass("org.cam.core.cache.CloneCopyStrategy");
    config.addCopyStrategy(copyConfig);

    PersistenceConfiguration persistConfig = new PersistenceConfiguration();
    persistConfig.setStrategy("NONE");
    config.addPersistence(persistConfig);

    return new Cache(config);
}
项目:arcus-java-client    文件:LocalCacheManager.java   
public LocalCacheManager(String name, int max, int exptime, boolean copyOnRead, boolean copyOnWrite) {
    this.cache = CacheManager.getInstance().getCache(name);
    if (cache == null) {
        CacheConfiguration config =
                        new CacheConfiguration(name, max)
                        .copyOnRead(copyOnRead)
                        .copyOnWrite(copyOnWrite)
                        .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
                        .eternal(false)
                        .timeToLiveSeconds(exptime)
                        .timeToIdleSeconds(exptime)
                        .diskExpiryThreadIntervalSeconds(60)
                        .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
        this.cache = new Cache(config, null, null);
        CacheManager.getInstance().addCache(cache);

        if (logger.isInfoEnabled()) {
            logger.info("Arcus k/v local cache is enabled : %s", cache.toString());
        }
    }
}
项目:cache2k-benchmark    文件:EhCache2Factory.java   
protected CacheConfiguration createCacheConfiguration(int _maxElements) {
  MemoryStoreEvictionPolicy _policy = MemoryStoreEvictionPolicy.LRU;
  switch (algorithm) {
    case CLOCK: _policy = MemoryStoreEvictionPolicy.CLOCK; break;
  }
  CacheConfiguration c = new CacheConfiguration(CACHE_NAME, _maxElements)
    .memoryStoreEvictionPolicy(_policy)
    .eternal(true)
    .diskExpiryThreadIntervalSeconds(0)
    .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
  if (withExpiry) {
    c.setEternal(false);
    c.setTimeToLiveSeconds(5 * 60);
  }
  return c;
}
项目:afp-api-client    文件:AFPDataGrabberCache.java   
public RealCache() {
    super();
    CacheManager manager = null;
    synchronized (RealCache.class) {
        manager = CacheManager.getInstance();
        if (!manager.cacheExists("AFPDataGrabberCache")) {
            Cache cache = new Cache(new CacheConfiguration("AFPDataGrabberCache", 50000).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
                    .eternal(true).timeToLiveSeconds(0).timeToIdleSeconds(0).persistence(new PersistenceConfiguration().strategy(Strategy.NONE)));
            manager.addCache(cache);
        }
    }
    ehcache = manager.getCache("AFPDataGrabberCache");
}
项目:cas-5.1.0    文件:EhcacheTicketRegistryConfiguration.java   
@Lazy
@Autowired
@Bean
public EhCacheFactoryBean ehcacheTicketsCache(@Qualifier("cacheManager") final CacheManager manager) {
    final EhcacheProperties ehcacheProperties = casProperties.getTicket().getRegistry().getEhcache();
    final EhCacheFactoryBean bean = new EhCacheFactoryBean();
    bean.setCacheName(ehcacheProperties.getCacheName());
    bean.setCacheEventListeners(Collections.singleton(ticketRMISynchronousCacheReplicator()));
    bean.setTimeToIdle(ehcacheProperties.getCacheTimeToIdle());
    bean.setTimeToLive(ehcacheProperties.getCacheTimeToLive());

    bean.setCacheManager(manager);
    bean.setBootstrapCacheLoader(ticketCacheBootstrapCacheLoader());
    bean.setDiskExpiryThreadIntervalSeconds(ehcacheProperties.getDiskExpiryThreadIntervalSeconds());

    bean.setEternal(ehcacheProperties.isEternal());
    bean.setMaxEntriesLocalHeap(ehcacheProperties.getMaxElementsInMemory());
    bean.setMaxEntriesInCache(ehcacheProperties.getMaxElementsInCache());
    bean.setMaxEntriesLocalDisk(ehcacheProperties.getMaxElementsOnDisk());
    bean.setMemoryStoreEvictionPolicy(ehcacheProperties.getMemoryStoreEvictionPolicy());

    final PersistenceConfiguration c = new PersistenceConfiguration();
    c.strategy(ehcacheProperties.getPersistence());
    c.setSynchronousWrites(ehcacheProperties.isSynchronousWrites());
    bean.persistence(c);

    return bean;
}
项目:stallion-core    文件:LocalMemoryCache.java   
public static void initCache(String bucket) {
    if (manager == null) {
        load();
    }
    if (manager.getCache(bucket) != null) {
        return;
    }
    synchronized(manager) {
        if (manager.getCache(bucket) != null) {
            return;
        }
        CacheConfiguration config = new CacheConfiguration();
        config.setName(bucket);
        // We have to do this way because there is no way to programmatically configure the
        // sizeOfEngine
        System.setProperty("net.sf.ehcache.sizeofengine.stallionLocalMemoryCache." + bucket, "io.stallion.dataAccess.filtering.EstimatedSizeOfEngine");

        // Max cache size is 100MB
        config.setMaxBytesLocalHeap(100 * 1000 * 1000L);
        //config.setDiskPersistent(false);
        //config.setMaxElementsOnDisk(0);
        //config.setMaxBytesLocalDisk(0L);
        config.setOverflowToOffHeap(false);
        PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
        persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.NONE);
        config.persistence(persistenceConfiguration);
        manager.addCache(new Cache(config));
    }
}
项目:stallion-core    文件:FilterCache.java   
public static void initCache(String bucket) {
    if (manager == null) {
        load();
    }
    if (manager.getCache(bucket) != null) {
        return;
    }
    // We have to do this way because there is no way to programmmatically configured the
    // sizeOfEngine
    System.setProperty("net.sf.ehcache.sizeofengine.stallionFilterCache." + bucket, "io.stallion.dataAccess.filtering.EstimatedSizeOfEngine");

    CacheConfiguration config = new CacheConfiguration();
    config.setName(bucket);
    // Max cache size is very approximately, 50MB

    config.setMaxBytesLocalHeap(Settings.instance().getFilterCacheSize());
    //config.setDiskPersistent(false);
    //config.setMaxElementsOnDisk(0);
    //config.setMaxBytesLocalDisk(0L);
    config.setOverflowToOffHeap(false);
    PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
    persistenceConfiguration.strategy(PersistenceConfiguration.Strategy.NONE);

    config.persistence(persistenceConfiguration);
    //SizeOfPolicyConfiguration sizeOfPolicyConfiguration = new SizeOfPolicyConfiguration();
    //sizeOfPolicyConfiguration.
    //config.addSizeOfPolicy();
    //config.set
    Cache cache = new Cache(config);

    manager.addCache(cache);

    //Cache cache = manager.getCache(bucket);
    //CacheConfiguration config = cache.getCacheConfiguration();


    //config.setMaxBytesLocalHeap(150000000L);
}
项目:Bananarama    文件:IndexedCollectionAdapter.java   
public IndexedCollectionAdapter(BananaRama parent){
    //This must be configurable in the future
    final Configuration conf = ConfigurationFactory
            .parseConfiguration();

    conf.getDefaultCacheConfiguration()
            .getPersistenceConfiguration()
            .strategy(PersistenceConfiguration.Strategy.NONE);

    cacheManager =  CacheManager.newInstance(conf);
    cache = cacheManager.addCacheIfAbsent(CACHE_NAME);
    this.parent = parent;
}
项目:BJAF3.x    文件:PersistQueue.java   
/**
 * 构造函数
 * 
 * @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());
    }
}
项目:BJAF3.x    文件:CacheQueue.java   
/**
 * 构造函数
 * 
 * @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);
}
项目:knowledge    文件:MemoryOnlyEhcache.java   
public MemoryOnlyEhcache(String size, String namePrefix)
{
    CacheConfiguration config = new CacheConfiguration();
    config.setMaxBytesLocalHeap(size);
    config.persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE));
    config.setMemoryStoreEvictionPolicy("LRU");
    config.eternal(true);
    config.setName(String.format("%s-%s", namePrefix, UUID.randomUUID().toString()));

    cache = new net.sf.ehcache.Cache(config);
    CacheManager.getInstance().addCache(cache);
}
项目:crigtt    文件:CrigttCacheFactoryBean.java   
@Override
public CrigttCache getObject() throws Exception {
    String name = this.config.getName();

    if (name == null) {
        this.config.setName((name = this.beanName));
    }

    Optional.ofNullable(this.maxBytesLocalDisk).ifPresent(this.config::setMaxBytesLocalDisk);
    Optional.ofNullable(this.maxBytesLocalHeap).ifPresent(this.config::setMaxBytesLocalHeap);
    Optional.ofNullable(this.maxBytesLocalOffHeap).ifPresent(this.config::setMaxBytesLocalOffHeap);
    Optional.ofNullable(this.persistenceStrategy).ifPresent(
        persistenceStrategy -> this.config.addPersistence(new PersistenceConfiguration().strategy(persistenceStrategy)));

    Cache cache = new Cache(this.config);
    cache.setName(name);
    cache.setCacheManager(this.manager.getCacheManager());

    if (!ArrayUtils.isEmpty(this.listeners)) {
        RegisteredEventListeners registeredListeners = cache.getCacheEventNotificationService();

        Stream.of(this.listeners).forEach(registeredListeners::registerListener);
    }

    cache.initialise();

    return new CrigttCache(cache);
}
项目:CAM    文件:InnerCacheTest.java   
@Test
public void testCreateCacheManual() throws Exception {

    CacheConfiguration config = new CacheConfiguration();
    config.setName("queryListCache");
    config.setEternal(false);
    config.setMaxEntriesLocalHeap(1000);
    config.setTimeToLiveSeconds(3600);
    config.setTimeToIdleSeconds(3600);
    config.setCopyOnRead(false);
    config.setCopyOnWrite(true);
    config.setMemoryStoreEvictionPolicy("LRU");

    CopyStrategyConfiguration copyConfig = new CopyStrategyConfiguration();
    copyConfig.setClass("org.cam.core.cache.CloneCopyStrategy");
    config.addCopyStrategy(copyConfig);

    PersistenceConfiguration persistConfig = new PersistenceConfiguration();
    persistConfig.setStrategy("NONE");
    config.addPersistence(persistConfig);

    Cache cache = new Cache(config);

    CacheManager manager = CacheManager.getInstance();
    manager.addCache(cache);

    StringSet mySet = new StringSet(Sets.newSet("1","2"));
    cache.put(new Element("mySet",mySet));

    Element e = cache.get("mySet");
    Assert.assertNotNull(e);
    Assert.assertNotNull(e.getObjectValue());

    StringSet stringSet = (StringSet) e.getObjectValue();
    Assert.assertEquals(2 , stringSet.getIdSet().size());

    System.out.println(stringSet);
}
项目:Brisk    文件:PortalServlet.java   
private Cache createImageCache(String name, Settings settings)
{
    PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE);

    CacheConfiguration imageCacheConfig = new CacheConfiguration().name(name).statistics(false).maxEntriesLocalHeap(settings.imageCacheMaxHeapEntries).maxEntriesLocalDisk(settings.imageCacheMaxDiskEntries).timeToIdleSeconds(settings.imageCacheTimeToIdle).timeToLiveSeconds(settings.imageCacheTimeToLive).persistence(persistenceConfiguration);

    return new Cache(imageCacheConfig);
}
项目:Brisk    文件:PortalServlet.java   
private Cache createPageCache(String name, Settings settings)
{
    PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE);

    CacheConfiguration imageCacheConfig = new CacheConfiguration().name(name).statistics(false).maxEntriesLocalHeap(settings.pageCacheMaxHeapEntries).maxEntriesLocalDisk(settings.pageCacheMaxDiskEntries).timeToIdleSeconds(settings.pageCacheTimeToIdle).timeToLiveSeconds(settings.pageCacheTimeToLive).persistence(persistenceConfiguration);

    return new Cache(imageCacheConfig);
}
项目:jerseyoauth2    文件:DefaultCacheManagerProvider.java   
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);
}
项目:sdcct    文件:CacheFactoryBean.java   
@Override
public EhCacheCache getObject() throws Exception {
    String name = this.config.getName();

    if (name == null) {
        this.config.setName((name = this.beanName));
    }

    if (this.searchable != null) {
        this.config.searchable(this.searchable);
    }

    if (this.hasMaxBytesLocalDisk()) {
        this.config.setMaxBytesLocalDisk(this.maxBytesLocalDisk);
    }

    if (this.hasMaxBytesLocalHeap()) {
        this.config.setMaxBytesLocalHeap(this.maxBytesLocalHeap);
    }

    if (this.hasMaxBytesLocalOffHeap()) {
        this.config.setMaxBytesLocalOffHeap(this.maxBytesLocalOffHeap);
    }

    if (this.hasPersistenceStrategy()) {
        this.config.addPersistence(new PersistenceConfiguration().strategy(persistenceStrategy));
    }

    if (this.hasSearchable()) {
        this.config.searchable(this.searchable);
    }

    Cache cache = new Cache(this.config);
    cache.setName(name);

    if (this.hasListeners()) {
        RegisteredEventListeners registeredListeners = cache.getCacheEventNotificationService();

        Stream.of(this.listeners).forEach(registeredListeners::registerListener);
    }

    cache.setCacheManager(this.cacheManager.getCacheManager());

    cache.initialise();

    return new EhCacheCache(cache);
}
项目:kylin    文件:EhcacheTest.java   
@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();
}