问题背景

为什么要使用缓存?本地缓存/Redis缓存/数据库查询优先级?

一、为什么要使用缓存

原因:CPU的速度远远高于磁盘IO的速度
问题:很多信息存在数据库当中的,每次查询数据库就是一次IO操作
所以,提高响应速度,必须减少磁盘IO的操作,缓存就是为了提升系统性能而存在的

二、查询的优先级

1、本地缓存

2、Redis缓存

3、数据库查询

 public static List<Content> getContentList(int positionId, String provCode, String areaCode) throws SQLException {
String key_p_p_a = positionId + "_" + provCode + "_" + areaCode;
List<Content> contentList = null;
String strTemp = "|";
StringBuilder cacheLogInfoSb = new StringBuilder("get cache [");
/*判断本地缓存是否存在*/
if (ContentService.MAP_CONTENT_P_P_A.containsKey(key_p_p_a)) {
contentList = ContentService.MAP_CONTENT_P_P_A.get(key_p_p_a);
cacheLogInfoSb.append(strTemp).append(" by cache MAP_CONTENT_P_P_A get List, key:").append(key_p_p_a)
.append(",List.size:").append(contentList.size());
/*判断Reids缓存是否存在*/
}else if (RedisCached.has(key_p_p_a)) {
contentList = (List<Content>) RedisCached.get(key_p_p_a.getBytes());
cacheLogInfoSb.append(strTemp).append(" by xmemcache MAP_CONTENT_P_P_A get List, key:").append(key_p_p_a)
.append(",List.size:").append(contentList.size());
/*数据库查询操作*/
} else {
try {
contentList = dao.getContentList(positionId, provCode, areaCode);
cacheLogInfoSb = new StringBuilder(" cache is not exists,get data by database");
} catch (SQLException e) {
cacheLogInfoSb.append(strTemp).append("SQLException:").append(e.toString());
e.printStackTrace();
throw e;
}
log.info(cacheLogInfoSb.toString() + "]");
log.info("[ContentService]加入缓存key_p_p_a, key:{},contentList.size:{}", key_p_p_a, contentList.size());
/*数据库查询后,将结果写入redis,方便下次查询*/
RedisCached.set(key_p_p_a, contentList, ContentService.Cache_ExPireTime_Day);
} return contentList;
}

三、缓存常用的集合框架以及操作(实际使用得比较多的方法)

使用的MAP集合ConcurrentHashMap

  public static Map<String, List<Content>> MAP_CONTENT_P_P_A = new ConcurrentHashMap<String, List<Content>>();

读取本地缓存get()

 List<Content> list = ContentService.MAP_CONTENT_P_P_A.get(key);

设置本地缓存put()

 ContentService.MAP_CONTENT_P_P_A.put(key_p_p_a, contentList);

定时更新本地/Redis缓存(代码演示)

 /** 查询数据库,获取最新数据 **/
Map<String, List<Content>> position_content_map_p_p_a = dao.queryContentListRelateProvCodeAreaCode(positionId);
/** 清除本地缓存 **/
Iterator<String> contentskeysIterPPA = MAP_CONTENT_P_P_A.keySet().iterator();
while (contentskeysIterPPA.hasNext()) {
String key = contentskeysIterPPA.next();
if (key.startsWith(String.valueOf(positionId))) {
removeKeys.add(key);
}
}
/** 清除Redis缓存 **/
for (int i = 0; i < removeKeys.size(); i++) {
String key = removeKeys.get(i);
contentList.addAll(MAP_CONTENT_P_P_A.remove(key));
RedisCached.delete(key);
}
removeKeys.clear();
/** 把最新内容加入MAP_CONTENT_P_P_A缓存 **/
Iterator<String> keysIterppa = position_content_map_p_p_a.keySet().iterator();
while (keysIterppa.hasNext()) {
String key = keysIterppa.next();
reFreshContenList.addAll(position_content_map_p_p_a.get(key));
MAP_CONTENT_P_P_A.putAll(position_content_map_p_p_a);
}

最新文章

  1. golang bytes.Buffer Reset
  2. nyoj 106背包问题(贪心专题)
  3. Objective-C 随机数
  4. python爬虫技术的选择
  5. CentOS 命令【备忘】
  6. GUID全局唯一标识符相关知识了解
  7. Codeforces Round #327 (Div. 2) B. Rebranding 水题
  8. bzoj2281
  9. 关于ionic传值
  10. Cocos2d-x 3.0final 终结者系列教程14-L新abel-Cocos2d-x公文
  11. apache和nginx支持SSI配置
  12. hdu_4717: The Moving Points 【三分】
  13. Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method &#39;POST&#39; not supported
  14. mysql基础优化-explain的使用-mysql死锁
  15. RN开发中的报错以及告警
  16. python的内置模块xml模块方法 xml解析 详解以及使用
  17. linux目录详细列表
  18. python day07笔记总结
  19. yolo
  20. python中class的序列化和反序列化

热门文章

  1. python-map的用法
  2. WPF 自定义ItemsControl/ListBox/ListView控件样式
  3. C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字
  4. mosquitto安装和测试
  5. SpringCloud的注解:EnableEurekaClient vs EnableDiscoveryClient
  6. 角落的开发工具集之Vs(Visual Studio)2017插件推荐
  7. flask 视图函数的使用
  8. windows10无法启动承载网络
  9. 一 Unicode和UTF-8的异同
  10. js 常用数组和字符串方法