您的位置:首页 > 其它

缓存查询列表并提供更新处理方案

2014-03-21 14:18 465 查看
package cn.com.iboyaa.ifservice.client.cache.util;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;

import cn.com.iboyaa.ifservice.client.cache.ListCache;
import cn.com.iboyaa.ifservice.client.entity.IdEntity;
import cn.com.iboyaa.ifservice.client.util.ListUtil;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* 实体类的list。用于缓存list型的查询结果
*
* @author 盼庚
* @Package cn.com.iboyaa.ifservice.client.memcached.util.maps.EntityList.java
* @version 1.0
* @since Service 1.0
* @date 2014年1月12日 下午6:25:04
* @control
*/
/**
*
* @author 盼庚
* @Package cn.com.iboyaa.ifservice.client.cache.util.EntityList.java
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:36:22
* @control
*/
@Component
public class EntityList<T extends IdEntity> implements Serializable {
private static final long serialVersionUID = 7790662020703118917L;
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(EntityList.class);
/** spel表达式解析器 */
private static ExpressionParser expParser = new SpelExpressionParser();
/** 唯一entitylist缓存,用来调用listcache */
private static EntityList<IdEntity> cached;
/** 记录id对应的list中未知 */
private Map<Long, T> map = Maps.newHashMap();
/** 对象列表 */
private List<T> list = Lists.newArrayList();
/** 缓存处理对象 */
@Autowired
private ListCache listCache;

/**
* 处理spel表达式
*
* @param key
* @return spel表达式处理结果字符串
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:33:07
*/
private String parseSpel(String key) {
return expParser.parseExpression(key).getValue(String.class);
}

/**
* 启动参数,启动时注入静态cached。实现listcache的缓存注入
*
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:33:32
*/
@SuppressWarnings("unchecked")
@PostConstruct
public void init() {
cached = (EntityList<IdEntity>) this;
cached.listCache = this.listCache;
}

/**
* 创建EntityList对象。并将列表对象缓存
*
* @param list
* @param key
*            key+id生成缓存id
*/
public EntityList(List<T> list, String key) {
this.list = ListUtil.NotNullList(list);
for (T t : list) {
cached.listCache.cacheEntity(t, parseSpel(key + t.getId()));
this.map.put(t.getId(), t);
}
}

/**
* 创建entitylist,不缓存类表对象
*
* @param list
*/
public EntityList(List<T> list) {
this.list = ListUtil.NotNullList(list);
for (T t : list) {
this.map.put(t.getId(), t);
}
}

/**
* 创建空的entitylist
*/
public EntityList() {
}

/**
* 通过id获取list中的对象
*
* @param id
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:36:43
*/
public T getEntity(Long id) {
return map.get(id);
}

/**
* 检查id是否存在
*
* @param id
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:37:02
*/
public Boolean check(Long id) {
if (map.get(id) == null) {
logger.warn("此id" + id + "在列表中不存在");
return false;
}
return true;
}

/**
* 注入对象
*
* @param id
* @param entity
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:37:25
*/
public Boolean setEntity(T entity) {
if (!check(entity.getId())) {
list.add(entity);
}
map.put(entity.getId(), entity);
return true;
}

/**
* 获取map,
*
* @return map中记录id与在list中的未知
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:38:13
*/
public Map<Long, T> getMap() {
return map;
}

/**
* 设置map
*
* @param map
*            map中记录id与在list中的未知
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:39:04
*/
public void setMap(Map<Long, T> map) {
this.map = map;
}

/**
* 获取对象列表
*
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:39:22
*/
public List<T> getList() {
return list;
}

/**
* 注入对象列表
*
* @param list
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:39:33
*/
public void setList(List<T> list) {
this.list = list;
}

/**
* 添加或更新对象。并根据key更新entitylist缓存
*
* @param entity
* @param key
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:40:01
*/
public EntityList<T> addEntity(T entity, String key) {
addEntity(entity);
return cached.listCache.upDate(this, parseSpel(key));
}

/**
* 添加或更新对象。
*
* @param entity
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:42:06
*/
public EntityList<T> addEntity(T entity) {
setEntity(entity);
return this;
}

/**
* 初始化entitylist,并更新缓存
*
* @param key
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:42:28
*/
public EntityList<T> removeAll(String key) {
this.list = Lists.newArrayList();
this.map = Maps.newHashMap();
return cached.listCache.upDate(this, parseSpel(key));
}

/**
* 从entitylist中去除对象,并更具key更新entitylist缓存
*
* @param t
* @param key
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:42:49
*/
public EntityList<T> remove(T t, String key) {
list.remove(map.get(t.getId()));
map.remove(t.getId());
return cached.listCache.upDate(this, parseSpel(key));
}

/**
* 根据key更新entitylist
*
* @param key
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年3月19日 上午9:43:25
*/
public EntityList<T> updateCache(String key) {
return cached.listCache.upDate(this, parseSpel(key));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐