ehcache实例代码
来源:互联网 发布:电子商务数据分析前景 编辑:程序博客网 时间:2024/06/06 17:55
闲着无事,写了一个ehcache的实例代码,代码如下,很简单,见笑
EhcacheUtil,可以实例模式也可以单例模式,我都写好了,可以试下
package com.util;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheException;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;public class EhcacheUtil {private Cache cache;private void loadPrame() {/** * 单例模式 */try {CacheManager.create("src/com/x/ehcache.xml");cache = CacheManager.getInstance().getCache("a");PrameUtil pUtil = new PrameUtil();for (int i = 0; i < 6; i++) {cache.put(new Element(String.valueOf(i), pUtil.getPrame(String.valueOf(i))));}} catch (CacheException e1) {e1.printStackTrace();}///**// * 实例// *///try {//CacheManager cm = CacheManager.create("src/com/x/ehcache.xml");//// cache = new Cache("b", 5000, false, false, 5, 2);//// cm.addCache(cache);//// cache = cm.getCache("b");//cache = cm.getCache("a");//PrameUtil pUtil = new PrameUtil();//for (int i = 1; i < 6; i++) {//cache.put(new Element(String.valueOf(i), pUtil.getPrame(String//.valueOf(i))));//}//} catch (IllegalStateException e) {//e.printStackTrace();//} catch (CacheException e) {//e.printStackTrace();//}}public String getPrame(String key) {String value = "";try {if (cache == null) {loadPrame();}Element el = cache.get((String) key);if (el == null) {loadPrame();el = cache.get((String) key);}if (el == null) {System.out.println("没有");}value = el.getValue().toString();} catch (IllegalStateException e) {e.printStackTrace();} catch (CacheException e) {e.printStackTrace();}return value;}}
PrameUtil,这个类是读取properties文件的,很简单,用到InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/p/p.properties");
package com.util;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class PrameUtil {public String getPrame(String key){String value = "";InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/p/p.properties");System.out.println(this.getClass().getClassLoader());System.out.println(this.getClass().getClassLoader().getResourceAsStream(""));Properties p = new Properties();try {p.load(is);value = p.getProperty(key);} catch (IOException e) {e.printStackTrace();}return value;}}
测试类TestMain,更简单,都不好意思贴出来
public class TestMain {public static void main(String[] args) {EhcacheUtil eUtil = new EhcacheUtil();String one = eUtil.getPrame("1");System.out.println(one);}}
ehcache.xml文件内容如下,记得放好位置
<ehcache><defaultCache maxElementsInMemory="1000" eternal="true"timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" /><cache name="a" maxElementsInMemory="1000" eternal="true"timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false" /></ehcache>
很简单的一个小例子,欢迎批评指正。
实例代码,可以在我的资源中下载,免分,呵呵,地址如下:http://download.csdn.net/detail/liuxiaochen123/4471585
- ehcache实例代码
- ehcache实例
- EhCache实例
- EhCache实例
- Ehcache实例
- EhCache实例
- Ehcache 1.5.0 User Guide - Code Samples 代码实例 (1)
- Ehcache 1.5.0 User Guide - Code Samples 代码实例2
- Ehcache 1.5.0 User Guide - Code Samples 代码实例 3
- spring + ehcache 使用实例
- ehcache 详解 实例
- EHcache实例演示
- spring + ehcache 使用实例
- Ehcache整合Spring实例
- Spring-EhCache配置实例
- Spring+EhCache缓存实例
- spring + EhCache缓存实例
- Ehcache缓存实例
- erlang进程相关信息
- 系统资源详解
- line search and trust region----continue
- hdu 1498 50 years, 50 colors (二分匹配)
- js正则表达式 验证手机号,email地址和邮政编码
- ehcache实例代码
- 小菜的ArcObjects学习之路------Map Grids的使用
- python的时间使用一例——使用更少的字节存储时间
- 数据仓库之路 – ETL应用浅析
- Logger4j输出指定级别的Log
- linux设备模型--代码分析
- “小3”大战雷周之意在“移”不在“机”
- winform开始/停止/重启服务
- arcgisServer9.3 访问 IIS 元数据库失败