SpringBoot整合RedisTemplate如何实现缓存信息监控
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章给大家分享的是有关SpringBoot整合RedisTemplate如何实现缓存信息监控的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。SpringBoot 整合 R
千家信息网最后更新 2025年11月13日SpringBoot整合RedisTemplate如何实现缓存信息监控
这篇文章给大家分享的是有关SpringBoot整合RedisTemplate如何实现缓存信息监控的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
SpringBoot 整合 Redis 数据库实现数据缓存的本质是整合 Redis 数据库,通过对需要"缓存"的数据存入 Redis 数据库中,下次使用时先从 Redis 中获取,Redis 中没有再从数据库中获取,这样就实现了 Redis 做数据缓存。
按照惯例,下面一步一步的实现 Springboot 整合 Redis 来存储数据,读取数据。
1.项目添加依赖首页第一步还是在项目添加 Redis 的环境, Jedis。
org.springframework.boot spring-boot-starter-data-redis redis.clients jedis
2. 添加redis的参数
spring: ### Redis Configuration redis: pool: max-idle: 10 min-idle: 5 max-total: 20 hostName: 127.0.0.1 port: 6379
3.编写一个 RedisConfig 注册到 Spring 容器
package com.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import redis.clients.jedis.JedisPoolConfig;/** * 描述:Redis 配置类 */@Configurationpublic class RedisConfig { /** * 1.创建 JedisPoolConfig 对象。在该对象中完成一些连接池的配置 */ @Bean @ConfigurationProperties(prefix="spring.redis.pool") public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); return jedisPoolConfig; } /** * 2.创建 JedisConnectionFactory:配置 redis 连接信息 */ @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig); return jedisConnectionFactory; } /** * 3.创建 RedisTemplate:用于执行 Redis 操作的方法 */ @Bean public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); // 关联 redisTemplate.setConnectionFactory(jedisConnectionFactory); // 为 key 设置序列化器 redisTemplate.setKeySerializer(new StringRedisSerializer()); // 为 value 设置序列化器 redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; }} 4.使用redisTemplate
package com.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.bean.Users;/*** @Description 整合 Redis 测试Controller* @version V1.0*/@RestControllerpublic class RedisController { @Autowired private RedisTemplate redisTemplate; @RequestMapping("/redishandle") public String redishandle() { //添加字符串 redisTemplate.opsForValue().set("author", "欧阳"); //获取字符串 String value = (String)redisTemplate.opsForValue().get("author"); System.out.println("author = " + value); //添加对象 //重新设置序列化器 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.opsForValue().set("users", new Users("1" , "张三")); //获取对象 //重新设置序列化器 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); Users user = (Users)redisTemplate.opsForValue().get("users"); System.out.println(user); //以json格式存储对象 //重新设置序列化器 redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)); redisTemplate.opsForValue().set("usersJson", new Users("2" , "李四")); //以json格式获取对象 //重新设置序列化器 redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)); user = (Users)redisTemplate.opsForValue().get("usersJson"); System.out.println(user); return "home"; }} 5.项目实战中redisTemplate的使用
/** * */package com.shiwen.lujing.service.impl;import java.util.List;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.shiwen.lujing.dao.mapper.WellAreaDao;import com.shiwen.lujing.service.WellAreaService;import com.shiwen.lujing.util.RedisConstants;/** * @author zhangkai * */@Servicepublic class WellAreaServiceImpl implements WellAreaService { @Autowired private WellAreaDao wellAreaDao; @Autowired private RedisTemplate stringRedisTemplate; /* * (non-Javadoc) * * @see com.shiwen.lujing.service.WellAreaService#getAll() */ @Override public String getAll() throws JsonProcessingException { //redis中key是字符串 ValueOperations opsForValue = stringRedisTemplate.opsForValue(); //通过key获取redis中的数据 String wellArea = opsForValue.get(RedisConstants.REDIS_KEY_WELL_AREA); //如果没有去查数据库 if (wellArea == null) { List wellAreaList = wellAreaDao.getAll(); wellArea = new ObjectMapper().writeValueAsString(wellAreaList); //将查出来的数据存储在redis中 opsForValue.set(RedisConstants.REDIS_KEY_WELL_AREA, wellArea, RedisConstants.REDIS_TIMEOUT_1, TimeUnit.DAYS); // set(K key, V value, long timeout, TimeUnit unit) //timeout:过期时间; unit:时间单位 //使用:redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS); //redisTemplate.opsForValue().get("name")由于设置的是10秒失效,十秒之内查询有结 //果,十秒之后返回为null } return wellArea; }} 6.redis中使用的key
package com.shiwen.lujing.util;/** * redis 相关常量 * * */public interface RedisConstants { /** * 井首字 */ String REDIS_KEY_JING_SHOU_ZI = "JING-SHOU-ZI"; /** * 井区块 */ String REDIS_KEY_WELL_AREA = "WELL-AREA"; /** * */ long REDIS_TIMEOUT_1 = 1L;}补充:SpringBoot整合RedisTemplate实现缓存信息监控
1、CacheController接口代码
@RestController@RequestMapping("/monitor/cache")public class CacheController{ @Autowired private RedisTemplate redisTemplate; @PreAuthorize("@ss.hasPermi('monitor:cache:list')")// 自定义权限注解 @GetMapping() public AjaxResult getInfo() throws Exception { // 获取redis缓存完整信息 //Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info(); Properties info = (Properties) redisTemplate.execute((RedisCallback 感谢各位的阅读!关于"SpringBoot整合RedisTemplate如何实现缓存信息监控"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
数据
缓存
整合
信息
对象
序列
数据库
监控
字符
字符串
项目
存储
配置
内容
时间
更多
格式
篇文章
不错
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
店铺网络安全结论
永劫北美服务器会被盗号吗
首选dns的服务器地址怎么算
数据库运维实习生的面试技巧
地理信息平台服务器硬件
四川定制app软件开发公司
金华软件开发培训机构
咋开服务器
数据库应该录入原始分
今年中国网络安全市场
教育局网络安全课件
全国大学同上一堂网络安全课
身边的网络安全故事奖励
广州华向网络技术有限公司
华为itc网络技术工程师
ibm服务器cnfg
宁静致远网络技术有限公司
盈环网络技术有限公司基金
叛逆赛车无法连接服务器
企业上云是用云服务器么
mysql怎么连接一个新数据库
软件开发企业税收优惠税法
hymenoptera数据库
软件开发管理员工礼物
网络技术服务费发票怎么计算
网络安全数据库原理及试
方舟生存服务器卡等级了
游侠求生之路2连接服务器失败
百度软件开发文档模板
园租租网络技术有限公司