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缓存处理机制的更多相关文章

  1. Redis 缓存失效机制

    Redis缓存失效的故事要从EXPIRE这个命令说起,EXPIRE允许用户为某个key指定超时时间,当超过这个时间之后key对应的值会被清除,这篇文章主要在分析Redis源码的基础上站在Redis设计 ...

  2. SpringBoot缓存管理(三) 自定义Redis缓存序列化机制

    前言 在上一篇文章中,我们完成了SpringBoot整合Redis进行数据缓存管理的工作,但缓存管理的实体类数据使用的是JDK序列化方式(如下图所示),不便于使用可视化管理工具进行查看和管理. 接下来 ...

  3. Redis 缓存失效和回收机制续

    二.Redis Key失效机制 Redis的Key失效机制,主要借助借助EXPIRE命令: EXPIRE key 30 上面的命令即为key设置30秒的过期时间,超过这个时间,我们应该就访问不到这个值 ...

  4. Redis 利用锁机制来防止缓存过期产生的惊群现象-转载自 http://my.oschina.net/u/1156660/blog/360552

    首先,所谓的缓存过期引起的“惊群”现象是指,在大并发情况下,我们通常会用缓存来给数据库分压,但是会有这么一种情况发生,那就是在一定时间 内生成大量的缓存,然后当缓存到期之后又有大量的缓存失效,导致后端 ...

  5. JAVA记录-redis缓存机制介绍(一)

    1.redis介绍 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Re ...

  6. 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存

    首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...

  7. [技术博客] 用户验证码验证机制---redis缓存数据库的使用

    目录 问题引入 初识redis 实际应用 作者:马振亚 问题引入 在这次的开发过程中,我们的需求中有一个是普通用户可以通过特定的机制申请成为社长.因为只有部分人才能验证成功,所以这个最开始想了两种思路 ...

  8. 缓存机制总结(JVM内置缓存机制,MyBatis和Hibernate缓存机制,Redis缓存)

    一.JVM内置缓存(值存放在JVM缓存中) 我们可以先了解一下Cookie,Session,和Cache Cookie:当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cooki ...

  9. Django缓存机制以及使用redis缓存数据库

    目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...

随机推荐

  1. 怎么解决Chrome浏览器“Failed to load resource: net::ERR_INSECURE_RESPONSE”错误?

    用科学方法安装的arcgisServer,需要修改系统时间,但是修改了系统时间,可能会出各种问题, office 不能正确验证,chrome 不能正常使用,访问网页, 如果直接输入本地地址进行访问的话 ...

  2. JavaSE学习笔记(8)---常用类

    JavaSE学习笔记(8)---常用类 1.Object类 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类 ...

  3. Linux指令(Terminal命令):

    刚开始学习Linux,整理了一下常用指令和快捷键,不足的日后补充. 指令:# cd:进入目录 pwd:查看当前所在路径 ls:查看当前所在目录下的所有子目录或者子文件 ls-l     |     l ...

  4. sql注入常见绕过技巧

    参考链接:https://blog.csdn.net/huanghelouzi/article/details/82995313 https://www.cnblogs.com/vincy99/p/9 ...

  5. bugku_web_变量1(CTF)

    这道题考察php全局变量GLOBALS的用法,同样是个php审计题. 看一下代码: flag In the variable ! <?php error_reporting(0); includ ...

  6. Easy_language

    http://www.guosen.com.cn/gxzq/tradestation/help/operate/operate06.html power language https://seekin ...

  7. mysql 获取当前时间加上一个月

    select DATE_ADD(NOW(), interval 1 MONTH) NOW()此处必须为时间格式 date_add() 增加 date_sub()减少 month 月份 minute 分 ...

  8. Android 基础知识 -- Intent

    Intent (意图) Android通信的桥梁,可以通过Intent启动Activity , Service , 发送指定广播到Receiver <1> 启动Activity : sta ...

  9. mac 安装Kafka

    1. 安装zookeeper brew install zookeeper 默认安装位置 启动文件: /usr/local/Cellar/zookeeper/3.4.10/bin/ 配置文件: /us ...

  10. asp.net core 配置文件动态更新

    IOptions<T> //站点启动后,获取到的值永远不变 IOptionsSnapshot<T> //站点启动后,每次获取到的值都是配置文件里的最新值 (reloadOnCh ...