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来缓存: ...
随机推荐
- Linux centos7 安装 phpMyAdmin
yum install httpd php mariadb-server –y搭建lamp运行环境之后安装phpMyAdmin遇到的一些问题记录一下 1.官网下载phpMyAdmin压缩包 wget ...
- element使用
官方网址: 点击 全局使用 1.创建项目 vue init webpack vue-ele 配置 2.安装依赖 npm install 3.安装loader模块(开发) npm install sty ...
- JAVA输出流与输入流
输出流 编程入门的第一个程序,输出一串字符串 public class C { public static void main(String[] args) { System.out.println( ...
- 基于AccessToken方式设计API
目录 数据库设计 实现方案 应用场景:公司A有一平台需要对外提供接口给其他商户使用,考虑到安全性问题,此时可考虑采用AccessToken方案.商户在公司A平台注册一app,平台分配appId.app ...
- Wannafly挑战赛13 zzf的好矩阵 题解 答案解释
Wannafly挑战赛13 zzf的好矩阵 题解 文章目录 Wannafly挑战赛13 zzf的好矩阵 题解 分析 结论1 结论2 结论3 C数组对应带子说明 空白长度论述 后续黑色长度论述 能&qu ...
- 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...
- python全栈学习 day03
换行符: \n 制表符: \t 字符串截取:顾头不顾尾 s[首:尾:步长] 首--->尾走向 和 步长方向一致 s[0:4:2] s[4:0:-2] a = "qwertyui&quo ...
- vue动画&过渡整理
- 网站后门shell-----eval
我们先来看看网站被攻击的代码: <?php error_reporting(E_ERROR); unlink('user.php'); unlink('../member/login.php') ...
- the simmon effect(in psychology) :build the function of subject_information(modify the experiment programme),before we begin the experiment
#the real experiment for simon effect #load the library which is our need import pygame import sys i ...