springboot 启动加载数据库数据到redis缓存

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: springboot 启动加载数据库数据到redis缓存


启动项目后, 加载数据库公共配置数据到redis中

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 *  启动项目后, 加载数据库公共配置数据到redis中
 * 
 */
@Component
public class PubConfigUtil {
    private static String pre = "pub_config"; // 存储到redis中的前缀
    private static String operation = ":"; // redis冒号分组
    @Resource
    private JdbcTemplate jdbcTemplate; // springboot集成mybatis-spring-boot-starter后本身封装的工具类
    @Resource
    private RedisTemplate redisTemplate;
    @PostConstruct // 是java注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
    public void reload() {
        String sql = "SELECT config_id,config_block,config_name,config_value,config_desc FROM ad_basic_config WHERE data_status = '1'"; // 数据库配置表,根据你自己的配置表定义查询语句
        List<Map<String, Object>> mapsList = jdbcTemplate.queryForList(sql);
        if (!CollectionUtils.isEmpty(mapsList)) { // 存在值
            StringBuilder sbl = new StringBuilder();
            for (Map<String, Object> map : mapsList) {
                sbl.setLength(0);
                String config_block = map.get("config_block").toString(); // 数据库配置表中的字段名称| 模块: aliyun
                String config_name = map.get("config_name").toString(); // 数据库配置表中的字段名称| 配置名: SMS_TEMPLATECODE_LOGGIN (阿里云下短信模板号 - 登录)
                String config_value = map.get("config_value").toString(); // 数据库配置表中的字段名称| 配置具体值: xxxxx (阿里云下短信模板号)
                String key = sbl.append(pre).append(operation).append(config_block).append(operation).append(config_name).toString();
                redisTemplate.opsForValue().set(key, config_value);
            }
        }
    }
    /*    // 获取启动后加载的配置数据 - 放到自己项目的redis工具类中
       public Integer getInt(String configBlock,String configName, Integer defaultVal) {
        String string = getString(configBlock,configName, null);
        Integer value = defaultVal;
        try {
            value = Integer.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Long getLong(String configBlock,String configName, Long defaultVal) {
        String string = getString(configBlock,configName, null);
        Long value = defaultVal;
        try {
            value = Long.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Double getDouble(String configBlock,String configName, Double defaultVal) {
        String string = getString(configBlock,configName, null);
        Double value = defaultVal;
        try {
            value = Double.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public Boolean getBoolean(String configBlock,String configName, Boolean defaultVal) {
        String string = getString(configBlock,configName, null);
        Boolean value = defaultVal;
        try {
            value = Boolean.valueOf(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    }   */

可以根据项目情况进行修改


相关文章
|
3月前
|
Web App开发 存储 缓存
如何精准清除特定类型或标签的缓存数据?
如何精准清除特定类型或标签的缓存数据?
307 57
|
5月前
|
缓存 NoSQL 关系型数据库
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
|
20天前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
217 4
|
2月前
|
存储 缓存 监控
一次缓存引发的文件系统数据不一致问题排查与深度解析
本文详述了一次由自研分布式文件系统客户端 EFC 的缓存架构更新所引发的严重数据不一致问题的完整排查过程。
一次缓存引发的文件系统数据不一致问题排查与深度解析
|
7月前
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult&lt;T&gt;`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码&quot;0&quot;和消息&quot;操作成功!&quot;,有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
547 0
|
7月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
331 0
|
3月前
|
JSON Java 数据格式
Spring Boot返回Json数据及数据封装
在Spring Boot中,接口间及前后端的数据传输通常使用JSON格式。通过@RestController注解,可轻松实现Controller返回JSON数据。该注解是Spring Boot新增的组合注解,结合了@Controller和@ResponseBody的功能,默认将返回值转换为JSON格式。Spring Boot底层默认采用Jackson作为JSON解析框架,并通过spring-boot-starter-json依赖集成了相关库,包括jackson-databind、jackson-datatype-jdk8等常用模块,简化了开发者对依赖的手动管理。
391 3
|
5月前
|
消息中间件 缓存 NoSQL
基于Spring Data Redis与RabbitMQ实现字符串缓存和计数功能(数据同步)
总的来说,借助Spring Data Redis和RabbitMQ,我们可以轻松实现字符串缓存和计数的功能。而关键的部分不过是一些"厨房的套路",一旦你掌握了这些套路,那么你就像厨师一样可以准备出一道道饕餮美食了。通过这种方式促进数据处理效率无疑将大大提高我们的生产力。
194 32
|
存储 缓存 负载均衡
学会这15点,让你分分钟拿下Redis数据库
介绍Redis技术入门的一些基础知识点
学会这15点,让你分分钟拿下Redis数据库
|
存储 NoSQL 数据库
学会这15点,让你分分钟拿下Redis数据库
原文:学会这15点,让你分分钟拿下Redis数据库 1、Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。
1247 0