Ehcache入门基础

Simple-Object 2018-02-28 原文

Ehcache入门基础

1.ehcache的简介

   EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

2.ehcache入门实例

  

  1.首先先导入ehcache相关的jar依赖,我这里有的是maven项目做得演示,所以要在pom.xml里面添加依赖。

  

  <dependency>  
      <groupId>net.sf.ehcache</groupId>  
      <artifactId>ehcache</artifactId>  
      <version>2.10.2</version>  
  </dependency> 

   2.创建配置文件 ehcache.xml,ehcache在启动的时候会默认去classpath根目录下找名为ehcache.xml的文件,也可以放在其他位置,使用时指明即可。为了方便,这次就在src/main/resources/创建一个配置文件 ehcache.xml。

  

<?xml version="1.0" encoding="UTF-8"?>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  
  <!-- 磁盘缓存位置 -->  
  <diskStore path="java.io.tmpdir/ehcache"/>  
  
  <!-- 默认缓存 -->  
  <defaultCache  
          maxEntriesLocalHeap="10000"  
          eternal="false"  
          timeToIdleSeconds="120"  
          timeToLiveSeconds="120"  
          maxEntriesLocalDisk="10000000"  
          diskExpiryThreadIntervalSeconds="120"  
          memoryStoreEvictionPolicy="LRU">  
    <persistence strategy="localTempSwap"/>  
  </defaultCache>  
  
  <!-- helloworld缓存 -->  
  <cache name="HelloWorldCache"  
         maxElementsInMemory="1000"  
         eternal="false"  
         timeToIdleSeconds="100"  
         timeToLiveSeconds="100"  
         overflowToDisk="false"  
         memoryStoreEvictionPolicy="LRU"/>  
</ehcache> 

   3.测试类

  

package com.hz.demo;  
  
import net.sf.ehcache.Cache;  
import net.sf.ehcache.CacheManager;  
import net.sf.ehcache.Element;  
  
public class EhcacheTest1 {  
  
    public static void main(String[] args) {  
        // 1. 创建缓存管理器  
            CacheManager cacheManager = CacheManager.create();  
            // 2. 获取缓存对象  
            Cache cache = cacheManager.getCache("HelloWorldCache");  
            // 3. 创建元素  
            Element element = new Element("key1", "HelleWorld");  
            // 4. 将元素添加到缓存  
            cache.put(element);  
            // 5. 获取缓存  
            Element value = cache.get("key1");  
            // 6.输出缓存中的值  
            System.out.println("key1="+value.getObjectValue());  
            // 7.删除缓存中的值  
            cache.remove("key1");  
            // 8. 刷新缓存  
            cache.flush();  
            // 9. 关闭缓存管理器  
            cacheManager.shutdown();  
    }  
}  

  4.输出结果

  

 

发表于 2018-02-28 13:56 Simple_Object 阅读() 评论() 编辑 收藏

 

版权声明:本文为Simple-Object原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Simple-Object/p/8483276.html

Ehcache入门基础的更多相关文章

随机推荐

  1. SQL Server到底需要使用哪些端口

    SQL Server在安装到服务器上后,由于出于服务器安全的需要,所以需要屏蔽掉所有不使用的端口,只开放必须使 […]...

  2. 数据结构与算法(七):迷宫回溯和八皇后问题

    一、迷宫回溯问题 1.问题 一个7*8的数组模拟迷宫,障碍用1表示,通路使用0表示,给定起点(1,1)和终点( […]...

  3. 预防SQL注入笔记

    SQL注入如何预防? 本文参考自owasp,重点是提供清晰,简单,可操作的指导,以防止应用程序中的SQL注入漏 […]...

  4. java后台开发代码规范

      总结了一些在开发java项目中的代码规范。   具体请参考阿里巴巴开发手册https://github.c […]...

  5. 微信公众号文章爬虫抓取实现原理!

    前言 无论是新方案还是旧方案, 获取公众号文章列表, 获取阅读点赞, 获取评论等接口可以通过抓包来获取 以上接 […]...

  6. 一切业务数据化,一切数据业务化

    阿里现在讲的一句话:“一切业务数据化,一切数据业务化”。 前半句的场景其实一直都在发生,只不过大家称之为信息化 […]...

  7. 百度OCR文字识别-身份证识别

      简介   一、介绍   身份证识别 API 接口文档地址:http://ai.baidu.com/docs […]...

  8. 三角函数常用公式总结

    万能公式 $\sin^2\alpha + \cos^2\alpha = 1$ 勾股定理   和角公式 $\si […]...

展开目录

目录导航