<cache:annotation-driven cache-manager="cacheManager" />
<!-- 指定ehcache.xml的位置 -->
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:com/xml/ehcache.xml">
<property name="shared" value="true" />
</bean>
<!-- 声明缓存Manager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="setProperty" />
<property name="arguments">
<list>
<value>system.project_name</value>
<value>${system.project_name}</value>
</list>
</property>
</bean>
package com.other;
import java.util.ArrayList;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.map.LRUMap;
/**
* @see <a href="http://crunchify.com/">We help customers create an effective
* online presence...</a>
* @since 2015-11-10
* @version 1.0.0
*/
public class MemoryCache<K, T> {
private long timeToLive;
private LRUMap cacheMap;
protected class CacheObject {
public long lastAccessed = System.currentTimeMillis();
public T value;
protected CacheObject(T value) {
this.value = value;
}
}
public MemoryCache(long crunchifyTimeToLive, final long crunchifyTimerInterval, int maxItems) {
this.timeToLive = crunchifyTimeToLive * 1000;
cacheMap = new LRUMap(maxItems);
if (timeToLive > 0 && crunchifyTimerInterval > 0) {
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(crunchifyTimerInterval * 1000);
} catch (InterruptedException ex) {
}
cleanup();
}
}
});
t.setDaemon(true);
t.start();
}
}
public void put(K key, T value) {
synchronized (cacheMap) {
cacheMap.put(key, new CacheObject(value));
}
}
@SuppressWarnings("unchecked")
public T get(K key) {
synchronized (cacheMap) {
CacheObject c = (CacheObject) cacheMap.get(key);
if (c == null)
return null;
else {
c.lastAccessed = System.currentTimeMillis();
return c.value;
}
}
}
public void remove(K key) {
synchronized (cacheMap) {
cacheMap.remove(key);
}
}
public int size() {
synchronized (cacheMap) {
return cacheMap.size();
}
}
@SuppressWarnings("unchecked")
public void cleanup() {
long now = System.currentTimeMillis();
ArrayList<K> deleteKey = null;
synchronized (cacheMap) {
MapIterator itr = cacheMap.mapIterator();
deleteKey = new ArrayList<K>((cacheMap.size() / 2) + 1);
K key = null;
CacheObject c = null;
while (itr.hasNext()) {
key = (K) itr.next();
c = (CacheObject) itr.getValue();
if (c != null && (now > (timeToLive + c.lastAccessed))) {
deleteKey.add(key);
}
}
}
for (K key : deleteKey) {
synchronized (cacheMap) {
cacheMap.remove(key);
}
Thread.yield();
}
}
}
package com.other.helper;
import com.other.MemoryCache;
/**
* 2、会话暂存区域
* @author Jackie
* @since 2015-11-12
* @version 1.0.0
*/
public class APPCacheHelp {
private static final Integer SESSION_LIVE_TIME = 1 * 60 * 60 * 24 * 7; //7天
private static final Integer SESSION_INTERVAL_TIME = 1 * 60 * 60; //1小时
private static final Integer SESSION_MAX_ITEMS = 100000; //100,000个
private static MemoryCache<String, String> appLoginSessionCache = new MemoryCache<String, String>(SESSION_LIVE_TIME, SESSION_INTERVAL_TIME, SESSION_MAX_ITEMS);
private APPCacheHelp() {
super();
}
/**
* 登录会话存储
* @param token 登录令牌
* @param userId 用户ID
*/
public static void put(String token,String userId){
appLoginSessionCache.put(token, userId);
}
/**
* 获取会话
* @param token 登录令牌
* @return 用户ID
*/
public static String get(String token){
return appLoginSessionCache.get(token);
}
/**
* 移除会话
* @param token 登录令牌
*/
public static void remove(String token){
appLoginSessionCache.remove(token);
}
}