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来缓存: ...
随机推荐
- 创建react项目,用typescript语法
create-react-app 文档 typescript 单元测试 官网测试相关文档
- 转载 selenium_对浏览器操作、鼠标操作等总结
https://www.jianshu.com/p/7a4414082ce2 查看环境conda info --env 激活环境conda activate machine 路径改成H:cd H:\p ...
- defender 书荐
2018全年度推荐: 1.[法]大仲马——基督山伯爵 2.[美]加西亚·马尔克斯——百年孤独 3.[法]大仲马——三个火枪手 2019第一季度推荐: 1.霍达——穆斯林的葬礼 2.[苏联]尼·奥斯特洛 ...
- 初识Mybatis和一些配置和练习
什么是Mybatis: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyBat ...
- Java8新特性一览表
总览 forEach() method in Iterable interface(Iterable接口中的forEach()方法) default and static methods in Int ...
- java集合框架备忘
List,Set,Map三者的区别? List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set(注重独一无二的性质): 不允许重复的集合.不会有多 ...
- 为什么重写equals方法,还必须要重写hashcode方法
一.equals方法和hashcode的关系 根据Object.hashCode的通用约定: 如果两个对象相同(equals方法返回true),那么hashcode也相等.(图1) 如果两个对象的ha ...
- JavaScript仿计算器案例源代码
效果图 index.html <!DOCTYPE html> <html> <head> <title></title> <link ...
- nodepad++ | 变成 _
点击右下角切换
- 【巨杉数据库SequoiaDB】巨杉Tech | 巨杉数据库数据高性能数据导入迁移实践
SequoiaDB 一款自研金融级分布式数据库产品,支持标准SQL和分布式事务功能.支持复杂索引查询,兼容 MySQL.PGSQL.SparkSQL等SQL访问方式.SequoiaDB 在分布式存储功 ...