缓存
2016-01-25 16:11
435 查看
<!-- 缓存注解声明,使用注解缓存 -->
<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);
}
}
<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);
}
}
相关文章推荐
- 横向滑动ViewPager和Fragment
- vs各类文件的作用
- 【箭扣观点】解剖ISIS的社交能力
- TabLayout和ViewPager实现Tabs切换
- Json格式字符串转换为数组
- Centos 7 PHP7环境配置 LNMP
- 设计模式--10、工厂方法和抽象工厂模式
- Protocol Buffer技术详解(语言规范)
- 使用pageheap调试程序,监控内存
- C++编程中指针的声明与基本使用讲解
- artemplate的自动保存问题
- hbase的thrift接口
- Hadoop 与 Spark 和 HBase 常用配置参数总结
- mybatis使用oracle序列
- iptables 基础
- 详解C++编程中向函数传递引用参数的用法
- Unity3D NGUI 制作Tabs
- python黑魔法之编码转换
- jquery遍历时长度为空
- apache如何配置多个虚拟目录