ehcache缓存
<p><strong>一、介绍</strong></p>
<p>EHCache是来自sourceforge(<a href="http://ehcache.sourceforge.net/">http://ehcache.sourceforge.net/</a>) 的开源项目,也是纯Java实现的简单、快速的Cache组件。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分 布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用 Gzip压缩提高响应速度。</p>
<p><strong>二、版本说明</strong></p>
<p>ehcache引入版本如下:</p>
<pre><code class="language-java"><dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependency></code></pre>
<p><strong>三、使用教程</strong></p>
<p>1、基于spring boot的项目</p>
<ul>
<li>1.1、在项目pom文件中增加包引用:</li>
</ul>
<pre><code class="language-java"> <dependency>
<groupId>com.nisbos</groupId>
<artifactId>nisbos-cache</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency></code></pre>
<ul>
<li>1.2、在启动类上增加相应注解即可. 如</li>
</ul>
<pre><code class="language-java"> @SpringBootApplication
@EnableEhcache
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}</code></pre>
<ul>
<li>1.3、需要调用的类中使用@Autowired注入IEhcacheService类,并在需要调用处直接使用IEhcacheService接口中的函数:</li>
</ul>
<pre><code class="language-java"> @Autowired
private IEhcacheService ehcacheService;
@GetMapping()
String Test() {
IEhcacheService ehcacheService = EhCacheUtil.service();
ehcacheService.set("yang", "77777");
System.out.println(ehcacheService.get("yang"));
}</code></pre>
<p>2、非spring web项目</p>
<ul>
<li>2.1、在项目pom文件中增加包引用:</li>
</ul>
<pre><code class="language-java"> <dependency>
<groupId>com.nisbos</groupId>
<artifactId>nisbos-cache</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency></code></pre>
<ul>
<li>2.2、在项目中增加一个单例类工具类, 如</li>
</ul>
<pre><code class="language-java"> public class EhCacheUtil {
// 使用提供的工厂类
private static IEhcacheService ehcacheService = FactoryProducer.getEhcacheService();
public static IEhcacheService service() {
return ehcacheService;
}
private EhCacheUtil() {
}
}</code></pre>
<ul>
<li>2.3、引用处调用:</li>
</ul>
<pre><code class="language-java"> ehcacheService.set("yang","ttttttt");
System.out.println("ehcache缓存:"+ehcacheService.get("yang"));</code></pre>
<p><strong>四、函数调用说明</strong></p>
<ul>
<li>
<p><a href="https://www.showdoc.cc/chlingm?page_id=4157830116544733" title="缓存获取">缓存获取</a></p>
</li>
<li>
<p><a href="https://www.showdoc.cc/chlingm?page_id=4157831017852985" title="缓存放入">缓存放入</a></p>
</li>
<li><a href="https://www.showdoc.cc/chlingm?page_id=4175641283320773" title="清空指定缓存的指定信息">清空指定缓存的指定信息</a></li>
</ul>