zoukankan      html  css  js  c++  java
  • Distributed Cache Guideline

    Note: Before this doc, I assume you have installed the terracotta with Ehcache already, if not, you can reference the guideline TERRACOTTA CONFIGURATION GUIDELINE doc.

    1.  Getting start from its sample

    We can find that there are two samples in the terracotta installation directory, colorcache and hibernate.  With these two samples, we will have a gross glance of how Ehcache works in terracotta.

    1.1   Switch to the /ehcache/samples/colorcache/bin.

    In this folder, we need run start-sample-server.bat , start-developer-console.bat, start- jetty.bat, start-sample.bat .

    Once the Jetty instances have started, you can view the sample application by visiting the following links:

    http://localhost:9081/colorcache ›
    http://localhost:9082/colorcache ›

    1.2   Switch to the terracotta Console

     

    1.3  Switch to the Statics tab panel 

    1.3.1    Retrieve Color many times.

    Launch to the page at  http://localhost:9081/colorcache/.

    When a color is first loaded, the application simulates the execution of a slow operation such as loading data from a database or executing a computationally intensive operation. It will then place the color object into Ehcache. Subsequent calls to load that color will be read from the cache, thereby executing much more quickly.

    1.3.2    View the Distributed Cached Data in Other JVMs

    Launch to the page at  http://localhost:9082/colorcache/.

    1.3.3    Back to the tab Panel to get more information.

    1.3.4    Change the attributes of the cache via UI

    Choose the Overview tab Panelà Cache Configuration, it will show the dialog box as below.

    2. Ehcache Configuration Guide

    Note: There are two methods to make the configuration: configure in xml directly, or create them programmatically and specify their parameters in the constructor. Usually, we recommend the first method.

    2.1Setting Cache Eviction

    One ehcache.xml file can be treated as a CacheManager, which class manages a set of defined caches. Cache eviction removes elements from the cache based on parameters with configurable values.

    An example is shown below. It allocates 1GB on heap and 4GB off heap at the CacheManager level. It also demonstrates some finer points which we will convert in the following sections.

    Sample:

    <ehcache maxBytesOnHeap="1g" maxBytesOffHeap="4g" maxBytesOnDisk="100g" >

           <cache   name="explicitlyAllocatedCache1"

                         maxBytesOnHeap="50m"

                         maxBytesOffHeap="200m"

                         timeToLiveSeconds="100"

           </cache>

           <cache   name="explicitlyAllocatedCache2"

                         maxBytesOnHeap="10%"

                         maxBytesOffHeap="200m"

                         timeToLiveSeconds="100"

           </cache>

           <cache   name="automaticallyAllocatedCache1"

                         timeToLiveSeconds="100"

                         overflowToDisk="true"

           </cache>

    </ehcache>

     

    3. Storage Options

    Ehcache has three stores:

    • a MemoryStore

    • an OffHeapStore (BigMemory, Enterprise Ehcache only) and

    • a DiskStore (two versions: open source and Ehcache Enterprise)

     

    3.1 Memory Store

    The MemoryStore is always enabled. It is not directly manipulated, but is a component of every

    cache.

     

    3.2 Off-Heap Store

    Terracotta BigMemory is an add-on to Enterprise Ehcache that permits caches to use an additional type of memory store outside the object heap.

    3.3. DiskStore

    The diskStore element in ehcache.xml is now optional (as of 1.5). If all caches use only MemoryStores, then there is no need to configure a diskStore. This simplifies configuration, and uses less threads. It is also good where multiple CacheManagers are being used, and multiple disk

    store paths would need to be configured.

    4. Enterprise Ehcache API Guide

    4.1Create CacheManager:

    There are two methods to create the CacheManager

    1. Singleton mode:

    CacheManager.getInstance()

    2. Instance mode:

    CacheManager manager = new CacheManager() ;

        While we need create the CacheManager from the specified file, so we can define as follow:

        CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");

    4.2 Using Caches and

    We can create a new cache and then add it into the cacheManager, and we also can get a cache according some key words in the configure file.eg.

    <cache name="sampleCache1" maxElementsInMemory="10000"

    maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"

    diskSpoolBufferSizeMB="20" timeToIdleSeconds="300"

    timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" />

    Cache cache = manager1.getCache(cacheName);

    4.3 CacheEventHandler

    Customized the events, such as add, delete and evict the cache. Then it need us do the three things.

    First, add the cacheEventListenerFactory node into the cache configuration file.

    <cache name="Test" maxElementsInMemory="1" eternal="false" overflowToDisk="true" timeToIdleSeconds="1" timeToLiveSeconds="2" diskPersistent="false" diskExpiryThreadIntervalSeconds="1" memoryStoreEvictionPolicy="LFU">

    <cacheEventListenerFactory class="co.ehcache.EventFactory" />

    </cache>

    Second, create the Class EventFactory, extend from the calss  CacheEventListenerFactory :

    public class EventFactory extends CacheEventListenerFactory {

    public CacheEventListener createCacheEventListener(Properties properties) {

    // TODO Auto-generated method stub

    return new CacheEvent(); }

    }

    Third,create the class CacheEvent, implement the CacheEventListener interface.

    Learn more Ehcache API

     

     

  • 相关阅读:
    【文言文】从高考到程序员
    lambda方法引用总结——烧脑吃透
    秒杀苹果carplay baidu车联网API冷艳北京车展
    东君误妾我怜卿(一)
    百度快照投诉技巧案例分析百度快照就是这样刷出来的
    新浪博客是否可以放谷歌广告?如何添加
    与葡萄酒的亲密接触-选购技巧篇
    车联网高速公路智能交通解决方案
    物联网细分领域-车联网(OBD)市场分析
    APP开发选择什么框架好? 请看这里!
  • 原文地址:https://www.cnblogs.com/michelleAnn2011/p/2873007.html
Copyright © 2011-2022 走看看