redis缓存处理机制
1.redis缓存处理机制:先从缓存里面取,取不到去数据库里面取,然后丢入缓存中
例如:系统参数处理工具类
package com.ztesoft.iotcmp.utils; import com.esotericsoftware.minlog.Log;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.iotcmp.service.systemparammgr.service.DcSystemParamService;
import com.ztesoft.iotcmp.util.PrimaryKeyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate; import java.util.HashMap;
import java.util.List;
import java.util.Map; public class SystemParamUtils { public static String getValue(String paramCode,String pkey) throws Exception
{
// 先从缓存里面获取,缓存里面没有再查询
StringRedisTemplate redisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
String paramValue = redisTemplate.opsForValue().get("Value_"+paramCode+"_"+pkey);
if (StringUtils.isBlank(paramValue)) {
// 使用springUtil获取Service,去数据库里取值
DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
paramValue = dcSystemParamService.queryValueByCodeAndPKey(paramCode,pkey); if (StringUtils.isBlank(paramValue))
{
Log.error("数据库参数Value获取失败,paramCode="+paramCode+"pkey="+pkey);
return null;
} // 再丢缓存里面去
redisTemplate.opsForValue().set("Value_"+paramCode+"_"+pkey,paramValue);
}
return paramValue;
} public static List<String> getValueList(String paramCode) throws Exception
{
// 先从缓存里面获取,缓存里面没有再查询
StringRedisTemplate redisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
List<String> paramValueList = redisTemplate.opsForList().range("List_"+paramCode,0,-1);
if (paramValueList == null || paramValueList.isEmpty()) {
// 使用springUtil获取Service,去数据库里取值
DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
paramValueList = dcSystemParamService.queryValueListByCode(paramCode); if (paramValueList == null)
{
Log.error("数据库参数List获取失败,paramCode="+paramCode);
return null;
} // 再丢缓存里面去
redisTemplate.opsForList().leftPushAll("List_"+paramCode,paramValueList);
}
return paramValueList;
} public static Map<String,String> getMap(String paramCode) throws Exception
{
// 先从缓存里面获取,缓存里面没有再查询
StringRedisTemplate stringRedisTemplate = PrimaryKeyUtils.getStringRedisTemplate();
Map resultValueMap = stringRedisTemplate.opsForHash().entries("Map_"+paramCode);
if (resultValueMap == null || resultValueMap.isEmpty()) {
// 使用springUtil获取Service,去数据库里取值
DcSystemParamService dcSystemParamService = SpringUtil.getBean("dcSystemParamService");
List<Map> paramValueMap= dcSystemParamService.queryValueAndPKeyByCode(paramCode); if (paramValueMap == null || paramValueMap.isEmpty())
{
Log.error("数据库参数Map获取失败,paramCode="+paramCode);
return null;
} resultValueMap = new HashMap<String, String>();
for ( Map<String,String> map : paramValueMap) {
String key = map.get("pkey");
String value = map.get("param_value");
resultValueMap.put(key, value);
}
// 再丢缓存里面去
stringRedisTemplate.opsForHash().putAll("Map_"+paramCode,resultValueMap);
}
return resultValueMap;
}
}
public class PrimaryKeyUtils {
public static StringRedisTemplate getStringRedisTemplate() {
RedisCache redisCache = (RedisCache) CacheFactory.getCacheClient(DataDictCache.getCacheNamespace());
StringRedisTemplate stringRedisTemplate = redisCache.getRedisTemplate();
return stringRedisTemplate;
}
}
2.系统参数表设计结构

redis缓存处理机制的更多相关文章
- Redis 缓存失效机制
Redis缓存失效的故事要从EXPIRE这个命令说起,EXPIRE允许用户为某个key指定超时时间,当超过这个时间之后key对应的值会被清除,这篇文章主要在分析Redis源码的基础上站在Redis设计 ...
- SpringBoot缓存管理(三) 自定义Redis缓存序列化机制
前言 在上一篇文章中,我们完成了SpringBoot整合Redis进行数据缓存管理的工作,但缓存管理的实体类数据使用的是JDK序列化方式(如下图所示),不便于使用可视化管理工具进行查看和管理. 接下来 ...
- Redis 缓存失效和回收机制续
二.Redis Key失效机制 Redis的Key失效机制,主要借助借助EXPIRE命令: EXPIRE key 30 上面的命令即为key设置30秒的过期时间,超过这个时间,我们应该就访问不到这个值 ...
- Redis 利用锁机制来防止缓存过期产生的惊群现象-转载自 http://my.oschina.net/u/1156660/blog/360552
首先,所谓的缓存过期引起的“惊群”现象是指,在大并发情况下,我们通常会用缓存来给数据库分压,但是会有这么一种情况发生,那就是在一定时间 内生成大量的缓存,然后当缓存到期之后又有大量的缓存失效,导致后端 ...
- JAVA记录-redis缓存机制介绍(一)
1.redis介绍 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Re ...
- 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存
首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...
- [技术博客] 用户验证码验证机制---redis缓存数据库的使用
目录 问题引入 初识redis 实际应用 作者:马振亚 问题引入 在这次的开发过程中,我们的需求中有一个是普通用户可以通过特定的机制申请成为社长.因为只有部分人才能验证成功,所以这个最开始想了两种思路 ...
- 缓存机制总结(JVM内置缓存机制,MyBatis和Hibernate缓存机制,Redis缓存)
一.JVM内置缓存(值存放在JVM缓存中) 我们可以先了解一下Cookie,Session,和Cache Cookie:当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cooki ...
- Django缓存机制以及使用redis缓存数据库
目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...
随机推荐
- UVA-1588
只用C来写 题目:https://vjudge.net/problem/UVA-1588 #include<stdio.h> #include<string.h> #defin ...
- B - Draw!
You still have partial information about the score during the historic football match. You are given ...
- 一起学Vue之表单输入绑定
在Vue进行前端开发中,表单的输入是基础且常见的功能,本文以一个简单的小例子,简述v-model数据绑定的用法,仅供学习分享使用,如有不足,还请指正. 基础用法 你可以用 v-model 指令在表单 ...
- 树莓派3b 换国内源 更新源
在国内要更新源的时候,因为是国外的源,总会出现网速太慢的问题, 以下是对于安装了,2017-11-29-raspbian-stretch 系统源 更换最好用root登陆操作 sudo passwd r ...
- C++中的IO类(iostream, fstream, stringstream)小结(转)
原文地址:https://blog.csdn.net/stpeace/article/details/44763009
- jQuery---音乐导航
音乐导航 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 批量获取mysql数据库实例指定参数的值
需求:需要对比所有mysql数据库实例上面的指定参数配置情况,同时需要需要能看到如ip,端口,master or slave,毕竟主和从参数不一样还是有可能的. 说明:必须要有个数据库存储所有是数据库 ...
- Leetcode Week2 Two Sum
Question Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- CVE-2019-0708 远程桌面代码执行漏洞复现
0x01 首先是靶机,这里的靶机是使用清水表哥提供的win7sp1的系统 漏洞环境使用VM安装Windows7 SP1模拟受害机Windows7 SP1下载链接:ed2k://|file|cn_win ...
- Nginx 配置Websocket
Nginx反向代理配置websocket nginx.org 官网推荐如下的配置,也可以直接看官网:http://nginx.org/en/docs/http/websocket.html http ...