缓存

<!-- 缓存注解声明,使用注解缓存 -->
<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);
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值