ehcache用法

配置

<?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" />

    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="1200" overflowToDisk="true"
        maxElementsOnDisk="10000000" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

    <!-- timeToIdleSeconds一直不访问这个对象的前提下,这个对象可以在cache中的存活时间 -->
    <!-- timeToLiveSeconds无论对象访问或是不访问(闲置),这个对象在cache中的存活时间 -->
    <!-- see 配置: http://haohaoxuexi.iteye.com/blog/2116749 -->
    <cache name="session" maxEntriesLocalHeap="20000"
        maxEntriesLocalDisk="20000" eternal="false" timeToIdleSeconds="3600"
        timeToLiveSeconds="3600" overflowToDisk="false" statistics="true">
    </cache>
    <cache name="moblie" maxEntriesLocalHeap="20000"
        maxEntriesLocalDisk="20000" eternal="false" timeToIdleSeconds="70"
        timeToLiveSeconds="70" overflowToDisk="false" statistics="true">
    </cache>
</ehcache>

Java代码

package com.uaf.cache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.uaf.model.Session;

public class SessionCache {
    
    private static Cache    SESSION    = CacheManage.getCache("session");
    
    /**
     * 通过openId获取一个自定义Session
     *         不存在时会创建
     * @param openId
     * @return
     */
    public static Session get(String openId) {
        Element element = SESSION.get(openId);
        Session session = null;
        if (element == null || element.getObjectKey() == null) {
            session = new Session();
            SESSION.put(new Element(openId, session));
        } else {
            session = (Session) element.getObjectValue();
        }
        return session;
    }
    
    /**
     * 放置一个
     * @param key
     * @param object
     */
    public static void setAttribute(String key, Object object) {
        SESSION.put(new Element(key, object));
    }
    
    /**
     * 获取一个
     * @param key
     *
     * @return
     */
    public static Object getAttribute(String key) {
        Element element = SESSION.get(key);
        if (element == null || element.getObjectKey() == null) {
            return null;
        } else {
            return element.getObjectValue();
        }
    }
    
    /**
     * 移除
     * @param key
     */
    public static void remove(String key) {
        SESSION.remove(key);
    }
    
}


package com.uaf.cache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

/**                    
 * @Filename CacheManage.java
 *
 * @Description 缓存管理
 *
 * @author frank.tan 2015-9-7
 *
 */
public class CacheManage {
    
    /**
     * 获取一个cache
     * @param name
     * @return
     */
    public static Cache getCache(String name) {
        return CacheManager.create().getCache(name);
    }
    
}


使用

public CustomerInfo getBySession(String openId) {
        JobLog.writeLog(MODULE, "getBySession传入的openId是:"+openId);
        Assert.notNull(openId, "openId不能为null");
        CustomerInfo customerInfo = null;
        Session session = SessionCache.get(openId);
        JobLog.writeLog(MODULE, "通过openId校验当前用户是否绑定城市传入的openId是:"+openId+session);
        JobLog.writeLog(MODULE, "判断SessionCache.get(openId)是否为空:session:"+session);
        Object object = session.getAttribute("customerInfo");
        JobLog.writeLog(MODULE, "session.getAttribute(customerInfo)object是否为空:"+(object==null) + object);
        if (object == null) {
            synchronized (openId.intern()) {//TODO 防止相同openId的多次并发请求
                session = SessionCache.get(openId);
                JobLog.writeLog(MODULE, "SessionCache.get(openId)session是否为空:"+(session==null) + session);
                object = session.getAttribute("customerInfo");
                if (object == null) {
                    customerInfo = getByOpenId(openId);
                    JobLog.writeLog(MODULE, "getByOpenId(openId)customerInfo是否为空:"+(customerInfo==null) + customerInfo);
                    session.setAttribute("customerInfo", customerInfo);//入缓存
                    return customerInfo;
                }
                return (CustomerInfo) object;
            }
        }
        customerInfo = (CustomerInfo) object;
        JobLog.writeLog(MODULE, "(CustomerInfo) objectcustomerInfo是否为空:"+(customerInfo==null) + customerInfo);
        return customerInfo;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值