参考资料:

http://www.ehcache.org/documentation/3.2/getting-started.html#configuring-with-xml

http://www.ehcache.org/documentation/3.2/xml.html

示例代码:

https://github.com/gordonklg/study,cache module

A. 实例

gordon.study.cache.ehcache3.basic.XmlConfig.java

public class XmlConfig {

    public static void main(String[] args) {
final URL myUrl = XmlConfig.class.getResource("/ehcache3_basic.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
cacheManager.init();
Cache<String, UserModel> phoneUserCache = cacheManager.getCache("phoneUserCache", String.class, UserModel.class);
UserModel user = new UserModel("13316619988", "abc@123.com", "openid12345", "very very long user information ...");
phoneUserCache.put(user.getPhone(), user);
UserModel cached = phoneUserCache.get(user.getPhone());
System.out.println(cached.getInfo());
}
}

示例代码简单明了,没啥好说的。

ehcache3_basic.xml in src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3' xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.2.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.2.xsd"> <cache-template name="userTemplate">
<key-type>java.lang.String</key-type>
<value-type>gordon.study.cache.ehcache3.basic.UserModel</value-type>
<heap unit="entries">5</heap>
</cache-template> <cache alias="phoneUserCache" uses-template="userTemplate">
<heap unit="entries">20</heap>
</cache> <cache alias="emailUserCache" uses-template="userTemplate" />
</config>

配置文件同样简单明了。其中 cache-template 为缓存模板,可以简化配置。

最新文章

  1. npm源切换
  2. Create a new Windows service on windows server2012
  3. iOS开发 适配iOS10以及Xcode8[转]
  4. PYTHON isinstance语法
  5. LeetCode&mdash;&mdash;Single Number II(找出数组中只出现一次的数2)
  6. 读书笔记——body and html
  7. 教你安装CentOS 6.5如何选择安装包
  8. Nginx技巧:灵活的server_name,Nginx配置一个服务器多个站点 和 一个站点多个二级域名
  9. CCF 201403-3 命令行选项 (STL模拟)
  10. Oracle/PLSQL: Creating Functions
  11. 如何调整iMindMap打印设置
  12. C# 扩展方法克隆实体类
  13. javax.net.ssl.SSLException: java.lang.UnsupportedOperationException
  14. c# 正则表达式对网页进行内容抓取
  15. POJ 2250 Compromise (UVA 531)
  16. TensorFlow问题:The TensorFlow library wasn&#39;t compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
  17. MSIL实用指南-装箱拆箱
  18. poj 2253 Frogger (最小最大路段)【dijkstra】
  19. js 的空值判断程序
  20. vue render &amp; array of components &amp; vue for &amp; vue-jsx

热门文章

  1. git别名
  2. Java集合—Queue(转载)
  3. oracle RMAN复制数据库
  4. iOS学习之HelloWorld工程
  5. flask内置session原理
  6. 进程 、进程组、会话、控制终端之间的关系 (转载 http://blog.csdn.net/yh1548503342/article/details/41891047)
  7. android上传图片、视频、文件,服务端使用wcf接收
  8. JQuery EasyUI 扩展方法 日期控件 设置时间段函数
  9. 对 META标签 的一点点了解
  10. Eclipase .自动提示