Ehcache关键类

openkk 12年前
     <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:24.3pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:429.1pt;padding-right:5.4pt;height:24.3pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <span style="font-size:small;"><span style="font-size:x-small;"><strong>简介</strong></span></span>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>     </div>    <div>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">中的</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">是用来管理</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">的。而</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">中包含元素</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">,而</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">实质上就是一个键值对。</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">在物理方面的实现有内存实现和磁盘实现。<br /> <img title="Ehcache关键类" border="0" alt="Ehcache关键类" src="https://simg.open-open.com/show/149a124734ca9827a3527b4a8a2c83ed.jpg" width="291" height="595" /></span>    </div>    <div>     <img alt="" src="http://news.open-open.com/uploads/allimg/090430/1526490.jpg" />    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:571px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:22.65pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:428.5pt;padding-right:5.4pt;height:22.65pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="571">        <div>         <strong><span style="font-size:small;"><span><span style="font-size:x-small;">CacheManager</span></span></span></strong>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">包含</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">,而</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">反过来构成了</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">的要素。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="text-indent:-8.5pt;margin:0cm 0cm 0pt 8.5pt;">     <span style="color:#999999;"><span style="font-size:small;"><span style="font-size:x-small;">·</span><span style="font:7pt 'Times New Roman';">   </span></span></span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">创建模式:</span>     <span style="font-size:9pt;">CacheManger</span>     <span style="font-size:9pt;">创建模式包含</span>     <span style="font-size:9pt;">singleton</span>     <span style="font-size:9pt;">和</span>     <span style="font-size:9pt;">instance</span>     <span style="font-size:9pt;">俩种。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="color:#999999;font-size:9pt;">·<span style="font:7pt 'Times New Roman';">       </span></span>     <span style="font-size:9pt;">Simgleton Mode</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;">从字面可理解为单例模式,这种模式只允许创建一个</span>     <span style="font-size:9pt;">CacheManger</span>     <span style="font-size:9pt;">实例。</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="color:#999999;font-size:9pt;">·<span style="font:7pt 'Times New Roman';">       </span></span>     <span style="font-size:9pt;">Instance Mode</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;">那这种模式从字面上理解也应该是实例化模式了。从</span>     <span style="font-size:9pt;">Ehcache-1.2</span>     <span style="font-size:9pt;">以来,</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">有多种静态创建方法。这样我们就可以同时创建多个复杂的不同配置的</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">。</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;">如果只是用内存来存储</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">,那么没有什么需要你特别考虑的。如果是用磁盘来缓存</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">的话,你就必须为</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">指定的磁盘路径。当一个新的</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">被创建时,必须要确保磁盘路径没有被别的</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">使用。如果重复使用磁盘路径,这个时候就会抛出</span>     <span style="font-size:9pt;">CacheException</span>     <span style="font-size:9pt;">。如果</span>     <span style="font-size:9pt;">CacheManger</span>     <span style="font-size:9pt;">配置成集群模式的,还要注意端口的指定。</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="color:#999999;font-size:9pt;">·<span style="font:7pt 'Times New Roman';">       </span></span>     <span style="font-size:9pt;">Singletion</span>     <span style="font-size:9pt;">和</span>     <span style="font-size:9pt;">Instance</span>     <span style="font-size:9pt;">混用</span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;"> </span>    </div>    <div style="margin:0cm 0cm 0pt 1cm;">     <span style="font-size:9pt;">如果一个应用程序通过构造方法创建了一个</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">的实例,也调用了静态创建方法,这个时候会,没调用一次静态创建方法就会返回一个</span>     <span style="font-size:9pt;">CacheManager</span>     <span style="font-size:9pt;">的</span>     <span style="font-size:9pt;">Singleton</span>     <span style="font-size:9pt;">实例,所有的这些实例将会共存。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:572px;border-collapse:collapse;background:#6699ff;" class="ke-zeroborder" border="0" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:27.25pt;">       <td style="border-bottom-color:#ece9d8;padding-bottom:0cm;background-color:transparent;border-top-color:#ece9d8;padding-left:5.4pt;width:429pt;padding-right:5.4pt;height:27.25pt;border-right-color:#ece9d8;border-left-color:#ece9d8;padding-top:0cm;" width="572">        <div>         <strong><span style="font-size:small;"><span><span style="font-size:x-small;">Ehcache</span></span></span></strong>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">是一个接口,所有的</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">都实现了</span>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">。每个</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">都有自己的名字和特定的属性以及包含着</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">元素。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">Ehcache</span>     <span style="font-size:9pt;">中的</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">代表一块特定的缓存区域或是缓存系统。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">的</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">元素可以存储到</span>     <span style="font-size:9pt;">MemoryStore</span>     <span style="font-size:9pt;">(内存)中,也可以写到</span>     <span style="font-size:9pt;">DiskStore</span>     <span style="font-size:9pt;">(磁盘)中。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <table style="width:571px;border-collapse:collapse;background:#6699ff;" border="1" cellspacing="0" cellpadding="0">     <tbody>      <tr style="height:27.25pt;">       <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:428.5pt;padding-right:5.4pt;height:27.25pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" width="571">        <div>         <strong><span style="font-size:small;"><span><span style="font-size:x-small;">Element</span></span></span></strong>        </div> </td>      </tr>     </tbody>    </table>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">每一个</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">对应一个缓存的原子实体。它有</span>     <span style="font-size:9pt;">key</span>     <span style="font-size:9pt;">、</span>     <span style="font-size:9pt;">value</span>     <span style="font-size:9pt;">以及访问记录属性。</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">可以被</span>     <span style="font-size:9pt;">put</span>     <span style="font-size:9pt;">进</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">也可以</span>     <span style="font-size:9pt;">remove</span>     <span style="font-size:9pt;">出</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">。通过配置</span>     <span style="font-size:9pt;">Cache</span>     <span style="font-size:9pt;">可定制</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">的失效以及移除策略。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">在</span>     <span style="font-size:9pt;">Ehcache-1.2 API</span>     <span style="font-size:9pt;">中要求存储对象要可序列化。没有序列化的对象不可以存储到</span>     <span style="font-size:9pt;">DiskStore</span>     <span style="font-size:9pt;">(磁盘)中,也不能被拷贝。</span>    </div>    <div>     <span style="font-size:9pt;"> </span>    </div>    <div>     <span style="font-size:9pt;">在</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">类中注意到有俩个方法</span>     <span style="font-size:9pt;">getOjbectValue</span>     <span style="font-size:9pt;">和</span>     <span style="font-size:9pt;">getKeyValue</span>     <span style="font-size:9pt;">,这个方法都是从缓存中取得</span>     <span style="font-size:9pt;">Element</span>     <span style="font-size:9pt;">元素的方法,唯一区别就是一个是用来取得序列化对象,另外一个是不可序列化对象。</span>    </div>