缓存查询列表并提供更新处理方案
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)); } }
相关文章推荐
- Python处理日志之取得文件列表(更新)
- 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理
- 处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】
- MySQL中SELECT+UPDATE处理并发更新问题解决方案
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天
- 数据库查询优化方案(处理上百万级记录如何提高处理查询速度)
- SQL透视查询(交叉查询)PIVOT in的值过多,或不确定时的处理方案
- POJ 3145Harmony Forever(线段树更新+分情况处理数据+区间极值查询+好题)
- MySQL中SELECT+UPDATE处理并发更新问题解决方案分享
- 数据库查询优化方案(处理上百万级记录如何提高处理查询速度)
- 数据更新操作,事物处理,伪列,分页查询
- Android进阶图片处理之DiskLruCache解析 硬盘缓存方案
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝
- 数据库查询优化方案(处理上百万级记录如何提高处理查询速度)(转载)
- VC++实现输入字符动态更新列表查询
- sqlDependency提供了这样一种能力:当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的.
- 安卓知识点及问题处理方案记录,持续更新~
- mybatis处理查询map列表属性为null的问题,而导致查询map无该key对象
- 如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案!(zz)
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝