开源缓存包 EHCache

系统 1876 0
<!--==S 模块标题-->

<!--==S 投票--><!--==E 投票--><!--S 文章(这部分加style只是为了看效果,基本上表现都受编辑器设置控制)-->

EHCache 是一个纯java的,在Hibernate2.1充当可插入的的在进程中的缓存,它具有以下缓存,最小的依赖性,全面的文特性:快速,简单,丰富的文档和测试用例。

    官方网站 http://ehcache.sourceforge.net/

--------------------------
----------使用简介------------
--------------------------
    ehcache-1.2 cacheNames 列表的取得;

    方法一:
        CacheManager.create();
        String[] cacheNames = CacheManager.getInstance().getCacheNames();

    方法二:
        CacheManager manager = new CacheManager();
        String[] cacheNames = manager.getCacheNames();

    方法三:
        CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");
        CacheManager manager2 = new CacheManager("src/config/ehcache2.xml");
        String[] cacheNamesForManager1 = manager1.getCacheNames();
        String[] cacheNamesForManager2 = manager2.getCacheNames();


    ehcache-1.2 管理器各种建立的方法:

    方法一:
         CacheManager manager = new CacheManager();

    方法二:
        CacheManager manager = new CacheManager("src/config/ehcache.xml");

    方法三:
        URL url = getClass().getResource("/anotherconfigurationname.xml");
        CacheManager manager = new CacheManager(url);

    方法四:
        InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
        try {
            CacheManager manager = new CacheManager(fis);
        } finally {
            fis.close();
        }


    添加和删除缓存元素

        设置一个名为test 的新cache,test属性为默认
        CacheManager singletonManager = CacheManager.create();
        singletonManager.addCache("testCache");
        Cache test = singletonManager.getCache("testCache");

        设置一个名为test 的新cache,并定义其属性
        CacheManager singletonManager = CacheManager.create();
        Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
        manager.addCache(memoryOnlyCache);
        Cache test = singletonManager.getCache("testCache");
        
        Cache 属性说明:

            构造函数:
            public Cache(java.lang.String name,
                         int maxElementsInMemory,
                         boolean overflowToDisk,
                         boolean eternal,
                         long timeToLiveSeconds,
                         long timeToIdleSeconds)

            参数说明:
            name                          - 元素名字。
                maxElementsInMemory           - 设定内存中创建对象的最大值。
                overflowToDisk                - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘
                                                       上。
                eternal                       - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
                                                      时限制且元素永不消亡。
                timeToIdleSeconds             - 设置某个元素消亡前的停顿时间。
                                                      也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
                                                      这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
                                                      设置该属性也无用)。
                                                      如果该值是 0 就意味着元素可以停顿无穷长的时间。
                timeToLiveSeconds             - 为元素设置消亡前的生存时间。
                                                       也就是一个元素从构建到消亡的最大时间间隔值。
                                                       这只能在元素不是永久驻留时有效。

        删除缓存元素:
        CacheManager singletonManager = CacheManager.create();
        singletonManager.removeCache("test");


    关闭缓存管理器 CacheManager
        
        CacheManager.getInstance().shutdown();


    对于缓存对象的操作:
        放入一个简单的对象到缓存元素;
        Cache cache = manager.getCache("sampleCache1");
        Element element = new Element("key1", "value1");
        cache.put(element);

        得到一个序列化后的对象属性值;
        Cache cache = manager.getCache("sampleCache1");
        Element element = cache.get("key1");
        Serializable value = element.getValue();

        得到一个没有序列化后的对象属性值;
        Cache cache = manager.getCache("sampleCache1");
        Element element = cache.get("key1");
        Object value = element.getObjectValue();
        
        删除一个对象从元素;
        Cache cache = manager.getCache("sampleCache1");
        Element element = new Element("key1", "value1"
        cache.remove("key1");

    对于永固性磁盘存储,立即存储到磁盘:

        Cache cache = manager.getCache("sampleCache1");
        cache.flush();


    获得缓存大小:
        得到缓存的对象数量;
        Cache cache = manager.getCache("sampleCache1");
        int elementsInMemory = cache.getSize();

        得到缓存对象占用内存的数量
        Cache cache = manager.getCache("sampleCache1");
        long elementsInMemory = cache.getMemoryStoreSize();

        得到缓存对对象占用磁盘的数量
        Cache cache = manager.getCache("sampleCache1");
        long elementsInMemory = cache.getDiskStoreSize();

    关于缓存的读取和丢失的记录
        得到缓存读取的命中次数;
        Cache cache = manager.getCache("sampleCache1");
        int hits = cache.getHitCount();
        
        得到内存中缓存读取的命中次数;
        Cache cache = manager.getCache("sampleCache1");
        int hits = cache.getMemoryStoreHitCount();

        得到磁盘中缓存读取的命中次数;
        Cache cache = manager.getCache("sampleCache1");
        int hits = cache.getDiskStoreCount();
        
        得到缓存读取的丢失次数;
        Cache cache = manager.getCache("sampleCache1");
        int hits = cache.getMissCountNotFound();

        得到缓存读取的已经被销毁的对象丢失次数;
        Cache cache = manager.getCache("sampleCache1");
        int hits = cache.getMissCountExpired();

--------------------------
----------简单例子------------
--------------------------

    实战:
        XML文件格式:

            <ehcache>

                <diskStore path="java.io.tmpdir"/>

                <defaultCache
                        maxElementsInMemory="10000"
                        eternal="false"
                        timeToIdleSeconds="120"
                        timeToLiveSeconds="120"
                        overflowToDisk="true"
                        diskPersistent="false"
                        diskExpiryThreadIntervalSeconds="120"
                        memoryStoreEvictionPolicy="LRU"
                        />

                <cache name="cache1"
                       maxElementsInMemory="10000"
                       eternal="false"
                       overflowToDisk="true"
                       timeToIdleSeconds="2"
                       timeToLiveSeconds="3"
                       memoryStoreEvictionPolicy="LFU"
                        />
                        
            </ehcache>

    源码:

        import java.io.Serializable;

        import net.sf.ehcache.Cache;
        import net.sf.ehcache.CacheManager;
        import net.sf.ehcache.Element;

        /**
         #############################################################################
         # DESCRIBE ehcache 缓存操作DEMO
         # AUTHOR   悠~游
         # DATE     2006-7-10
         # COMPANY  FLX
         # PORJECT  ehcache-demo
         #############################################################################
         */

        public class Demo {
            
            static CacheManager manager= new CacheManager();

            /**
             *##############################################################################
             *
             * @DESCRIBE    
             * @param args
             * @throws InterruptedException
             *                        
             *##############################################################################
             */
            public static void main(String[] args) throws InterruptedException {
                
                String[] cacheNames = manager.getCacheNames();
                System.out.println("读取的缓存列表为:");
                for(int i=0;i<cacheNames.length;i++){
                    System.out.println("-- "+(i+1)+" "+cacheNames );
                }
                
                Cache cache = manager.getCache("cache1");
                Element element = new Element("key1", "value1");
                cache.put(element);
                
                element = cache.get("key1");
                Serializable value = element.getValue();
                System.out.println("序列化后的值为:"+value.toString());

                element = cache.get("key1");
                Object value1 = element.getObjectValue();
                System.out.println("未序列化的值为:"+value1.toString());
                
                int elementsInMemory = cache.getSize();
                System.out.println("得到缓存的对象数量:"+elementsInMemory);
                
                long elementsInMemory1 = cache.getMemoryStoreSize();
                System.out.println("得到缓存对象占用内存的数量:"+elementsInMemory1);
                
                long elementsInMemory2 = cache.getDiskStoreSize();
                System.out.println("得到缓存对对象占用磁盘的数量:"+elementsInMemory2);        
                
                int hits = cache.getHitCount();
                System.out.println("得到缓存读取的命中次数:"+hits);        
                
                int hits1 = cache.getMemoryStoreHitCount();
                System.out.println("得到内存中缓存读取的命中次数:"+hits1);        
                
                int hits2 =cache.getDiskStoreHitCount();
                System.out.println("得到磁盘中缓存读取的命中次数:"+hits2);        
                
                int hits3 = cache.getMissCountNotFound();
                System.out.println("得到缓存读取的丢失次数:"+hits3);        
                
                int hits4 = cache.getMissCountExpired();
                System.out.println("得到缓存读取的已经被销毁的对象丢失次数:"+hits4);    
            }

        }
<!--v:3.2-->

<script type="text/javascript"></script><script src="/qzone/newblog/v5/script/newubb.js" type="text/javascript"></script><script src="/qzone/newblog/v5/script/content.js" type="text/javascript"></script><script type="text/javascript"></script>

loading......

开源缓存包 EHCache


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论