zoukankan      html  css  js  c++  java
  • java缓存的使用

    缓存
    1,缓存的定义与作用
    2,缓存的使用范围(命中率高、高访问量)
    3,缓存策略(命中率,最大元素,清空策略);
    4,缓存介质(内存缓存,硬盘缓存,数据库缓存)(本地缓存(ehcache,oscache)与远程缓存(memcached));
    5,osCache缓存特点:
    <1>缓存任何对象,不受限制地缓存部分jsp页面或http请求
    <2>拥有全面的api
    <3>永久缓存,缓存能随意写入硬盘,因此允许昂贵的创建数据来保存缓存,甚至能让应用重启
    <4>支持集群,集群缓存数据能被单个地进行参数配置,不需要修改代码
    <5>缓存记录过期,可以最大限度的控制缓存对象的过期,包括可插入式刷新策略,在默认性能不需要的时候.
    6,oscache的使用,
    <1>下载occache.jar,放在web-inf/lib下,配置文件oscache.properties放入web-inf/classes目录下
    <2>使用jsp标签缓存部分页面<%@taglib uri="oscache" prefix="os"%>有5个标签,cache,usecached,flush,addgroup,addgroups.
    7,使用过滤器缓存整个页面
    <filter>
    <filter-name></filter-name>
    <filter-class></filter-class>
    <init-param>

    </init-param>
    </filter>
    8使用oscache api缓存java对象.
    主要通过GeneralCacheAdministrator来建立、刷新和管理缓存,可以通过加载cache.properties属性来创建一个缓存实例,最好使用单例模式来创建GeneralCacheAdministrator.
    9,在ibatis中使用oscache
    <cacheModel id="userCache" type="OSCACHE">
    <flushInterval hours="24"/>
    <flushOnExecute statement="updateUser"/>
    <property name="size" value="1000"/>
    </cacheMode>
    10ehcache的使用
    <1>下载ehcache.jar,并配置ehcache.xml文件,配置各种属性.使用<defaultCache>表示
    <2>属性解释:
    <!--
    配置自定义缓存:
    name: 缓存名称。通常为缓存对象的类名(非严格标准)。
    maxElementsInMemory: 设置基于内存的缓存可存放对象的最大数目。
    maxElementsOnDisk: 设置基于硬盘的缓存可存放对象的最大数目。
    eternal: 如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false;
    timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期。
    当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态。
    timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。
    当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。
    overflowToDisk: 如果为true,表示当基于内存的缓存中的对象数目达到了 maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
    memoryStoreEvictionPolicy: 缓存对象清除策略。有三种:FIFO、LFU、LRU
    -->
    <3>如果使用更多的缓存策略,可以添加类似的<cache>元素,其属性与<defaultCache>相同.
    11,使用过滤器缓存web页面,需要在web.xml中添加过滤器
    12,使用ehcache api缓存java对象
    CacheManager manager=CacheManager.create();//使用默认配置文件创建
    CacheManager manager=CacheManager.create("src/config/ehcache.xml")//使用指定配置文件创建
    Url url=getClass().getResource("/anothername.xml");
    CacheManager manager=CacheManager.create(url);//从classpath中寻找配置文件并创建.
    Inputstream fis=new FileInputStream(new File("src/config/ehcahce.xml").getAbsolutePath();
    CacheManager manager=CacheManager.create(fis);//使用输入流来创建.
    加载一个echache.xml配置的缓存策略
    Cache cache=manager.getCache("sampleCachel");
    然后往cache加入元素
    Element element=new Element("key1","value1");
    cache.put(new Element(element));
    从cache中取得元素
    Element element=cache.get("key1");
    结束卸载CacheManager:
    manager.shutdown();

  • 相关阅读:
    尚未笔记知识点
    jsonp的原理及其使用
    django中将views.py中的python方法传递给html模板文件
    UVA
    UVA
    UVA
    UVA
    UVA
    UVA 1600 Patrol Robot
    UVA
  • 原文地址:https://www.cnblogs.com/likeju/p/4717398.html
Copyright © 2011-2022 走看看