Current section

Files

Jump to
eeb html ehcache调用.html
Raw

html/ehcache调用.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="eeb v0.1.3">
<title>ehcache调用 – eeb v0.1.3</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="http://oss.maxcdn.com/semantic-ui/2.1.6/semantic.min.css">
<link rel="stylesheet" type="text/css" href="http://oss.maxcdn.com/highlight.js/9.0.0/styles/github.min.css">
<link rel="stylesheet" type="text/css" href="assets/base.css">
</head>
<body>
<div class="ui text container" style="margin-top: 25px;">
<div class="author-info">
<div class="ui stackable two column grid container">
<div class="column">
<a href="index.html">
<img class="ui avatar image" src="http://upload.jianshu.io/users/upload_avatars/297930/02d576b98e6a.png?imageMogr/thumbnail/90x90/quality/100">
</a>
</div>
<div class="column" style="margin-left: -300px">
<div class="content">
<div class="header" style="margin-bottom: 5px">
<span class="mini ui red basic label">作者</span>
<a class="author-name blue-link" href="index.html">
<span>aborn</span>
</a>
<span class="popup" data-content="最后编辑于 2016-02-02 20:13" data-variation="inverted">2016-01-19 10:59*</span>
</div>
<span>写了667字</span><span>被7人访问</span><span>获得了0个喜欢</span>
</div>
</div>
</div>
</div>
<h1 class="ui header">ehcache调用</h1>
<p>@(技术笔记)</p>
<p><a href="http://www.ehcache.org/">ehcache</a>是非常优秀的、轻量级的、本地缓存方案,它可以解决大并发情况下静态资源的快速保存与访问。</p>
<h2>引入ehcache jar包</h2>
<p>这里引入最新版本的ehcache jar包:</p>
<pre><code class="elixir">&lt;dependency&gt;
&lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
&lt;artifactId&gt;ehcache&lt;/artifactId&gt;
&lt;version&gt;2.10.0&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;</code></pre>
<h2>基本概念</h2>
<p><strong>1. CacheManager</strong>
CacheManager是缓存的管理器,使用ehcache必需创建一个CacheManager,它的创建方式有两种:
a. 单例模式:</p>
<pre><code class="elixir">private static volatile CacheManager cacheManager = CacheManager.create()</code></pre>
<p>b. ehcache 2.x以后允许多个cacheManager存在,可以通过.xml配置新的cacheManager,如下demo-ehcache.xml</p>
<pre><code class="elixir">&lt;ehcache name=&quot;DemoCacheManager&quot;&gt;
&lt;/ehcache&gt;</code></pre>
<p>通过.xml文件初始化非单例模式的cacheManager,如下:</p>
<pre><code class="elixir">private static volatile CacheManager cacheManager = CacheManager.newInstance(this.class.getClassLoader().getResourceAsStream(&quot;demo-ehcache.xml&quot;));</code></pre>
<p><strong>2. cache(缓存)</strong>
一个CacheManager可以管理多个缓存,每个缓存在内存中都有其自己的属性,例如缓存元素(Element)的最大个数、缓存在内存中的最长存活时间、缓存的淘汰算法等。一个cache可以存储多个缓存元素(Element)。缓存在使用前都要进行注册,注册有两种方式:</p>
<ul>
<li>通过xml配置文件注册。配置文件可配置缓存的名字,缓存在堆中的最大个数,缓存的淘汰算法等,参数非常之多,下面是一个最简单的例子:
</li>
</ul>
<pre><code class="elixir">&lt;ehcache&gt;
&lt;cache name=&quot;demo&quot;
maxEntriesLocalHeap=&quot;10000&quot;
eternal=&quot;false&quot;
timeToIdleSeconds=&quot;300&quot;
timeToLiveSeconds=&quot;600&quot;
memoryStoreEvictionPolicy=&quot;LFU&quot;
&gt;
&lt;/cache&gt;
&lt;/ehcache&gt;</code></pre>
<p>上面配置了一个缓存名为demo的缓存,它在内存中的最大个数为10000个,最长存活时间timeToLiveSeconds为10分钟(600s),最长可访问时间timeToIdleSeconds为5分钟(300s)。当个数超过最大个数限制时采用LFU算法进行淘汰。eternal表示缓存是否永久不失效,当这个值为true时,上面的最大存活时间和最长可访问时间无效。</p>
<ul>
<li>通过代码动态注册
下面是一个例子,与通过xml文件注册效果一样:
</li>
</ul>
<pre><code class="elixir">Cache newMemCache = new Cache(&quot;demo&quot;,
1000, // 内存中最大元素个数
MemoryStoreEvictionPolicy.LFU, // 淘汰策略
false, // 是否存到disk
&quot;/data/appdatas&quot;, // 存在disk的位置
fale, // 是否永久有效
600, // timeToLiveSeconds
300, // timeToIdleSeconds
false, // 是否在JVM重启的时候,是否保存到disk
60 * 60, // 多长时间跑一次disk超时的线程(这个参数没用)
null // 注册事件
);
cacheManager.addCache(newMemCache);</code></pre>
<p><strong>3. Element缓存元素</strong>
缓存元素是存储数据的最小单位,由其ElementKey和对象组成,如下定义:</p>
<pre><code class="elixir">Element element = new Element(elementkey, object);</code></pre>
<p>注意:这里的object可以为任意对象!!</p>
<h2>调用说明</h2>
<p><strong>1. add操作</strong>
往缓存中添加Element,如下:</p>
<pre><code class="elixir">Cache cache = cacheManager.getCache(&quot;demo&quot;);
if (cache != null) {
Element element = new Element(&quot;demoElementKey&quot;, &quot;demo object&quot;);
cache.put(element);
}</code></pre>
<p><strong>2. get操作</strong>
从缓存中取元素</p>
<pre><code class="elixir">Cache cache = cacheManager.getCache(&quot;demo&quot;);
if (null != cache) {
Element element = cache.get(&quot;demoElementKey&quot;);
}</code></pre>
<h2>具体例子</h2>
<p>具体例子请参考我写的<a href="https://github.com/aborn/ehcache-client/blob/master/src/main/java/org/popkit/mobile/ehcache/EhCacheService.java">ehcahce-client</a></p>
<footer class="footer">
<p>
<span class="line">
Powered by
<a href="https://github.com/aborn/eeb" title="eeb" rel="help" target="_blank">eeb</a> (v0.1.3).
</span>
</p>
</footer>
</div>
<script src="http://oss.maxcdn.com/jquery/2.1.4/jquery.min.js"></script>
<script src="http://oss.maxcdn.com/semantic-ui/2.1.6/semantic.min.js"></script>
<script src="http://oss.maxcdn.com/highlight.js/9.0.0/highlight.min.js"></script>
<script src="assets/app.js"></script>
</body>
</html>