首页 > 作文

SpringBoot整合RedisTemplate实现缓存信息监控的步骤

更新时间:2023-04-04 17:20:11 阅读: 评论:0

springboot 整合 redis 数据库实现数据缓存的本质是整合 redis 数据库,通过对需要“缓存”的数据存入 redis 数据库中,下次使用时先从 redis 中获取,redis 中没有再从数据库中获取,这样就实现了 redis 做数据缓存。   

按照惯例,下面一步一步的实现 springboot 整合 redis 来存储数据,读取数据。

1.项目添加依赖首页第一步还是在项目添加 redis 的环境, jedis。

<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-data-redis</artifactid></dependency><dependency><groupid>redis.clients</groupid><artifactid>jedis</artifactid></dependency>

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.rializer.stringredisrializer;import redis.clients.jedis.jedispoolconfig;/** * 描述:redis 配置类 */@configurationpublic class redis冬至简短祝福语八个字config {/** * 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 操作的方法 */@beanpublic redistemplate<string, object> redistemplate(jedisconnectionfactory jedisconnectionfactory) {redistemplate<string, object> redistemplate = new redistemplate<>();// 关联redistemplate.tconnectionfactory(jedisconnectionfactory);// 为 key 设置序列化器redistemplate.tkeyrializer(new stringredisrializer());// 为 value 设置序列化器redistemplate.tvaluerializer(new stringredisrializer());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.rializer.jackson2jsonredisrializer;import org.springframework.data.redis.rializer.jdkrializationredisrializer;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller;import com.bean.urs;/*** @description 整合 redis 测试controller* @version v1.0*/@restcontrollerpublic class rediscontroller {@autowiredprivate redistemplate<string, object> redistemplate;@requestmapping("/redishandle")public string redishandle() {//添加字符串redistemplate.opsforvalue().t("author", "欧阳");//获取字符串string value = (string)redistemplate.opsforvalue().get("author");system.out.println("author = " + value);//添加对象//重新设置序列化器redistemplate.tvaluerializer(new jdkrializationredisrializer());redistemplate.opsforvalue().t("urs", new urs("1" , "张三"));//获取对象//重新设置序列化器redistemplate.tvaluerializer(new jdkrializationredisrializer());urs ur = (urs)redistemplate九江学院官网首页.opsforvalue().get("urs");system.out.println(ur);//以json格式存储对象//重新设置序列化器redistemplate.tvaluerializer(new jackson2jsonredisrializer<>(urs.class));redistemplate.opsforvalue().t("ursjson", new urs("2" , "李四"));//以json格式获取对象//重新设置序猪肘子的做法列化器redistemplate.tvaluerializer(new jackson2jsonredisrializer<>(urs.class));ur = (urs)redistemplate.opsforvalue().get("ursjson");system.out.println(ur);return "home";}}

5.项目实战中redistemplate的使用

/** *  */package com.shiwen.lujing.rvice.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.rvice;import com.fasterxml.jackson.core.jsonprocessingexception;import com.fasterxml.jackson.databind.objectmapper;import com.shiwen.lujing霍去病的妻子.dao.mapper.wellareadao;import com.shiwen.lujing.rvice.wellarearvice;import com.shiwen.lujing.util.redisconstants;/** * @author zhangkai * */@rvicepublic class wellarearviceimpl implements wellarearvice {@autowiredprivate wellareadao wellareadao;@autowiredprivate redistemplate<string, string> stringredistemplate;/* * (non-javadoc) *  * @e com.shiwen.lujing.rvice.wellarearvice#getall() */@overridepublic string getall() throws jsonprocessingexception {//redis中key是字符串valueoperations<string, string> opsforvalue = stringredistemplate.opsforvalue();//通过key获取redis中的数据string wellarea = opsforvalue.get(redisconstants.redis_key_well_area);//如果没有去查数据库if (wellarea == null) {list<string> wellarealist = wellareadao.getall();wellarea = new objectmapper().writevalueasstring(wellarealist);//将查出来的数据存储在redis中opsforvalue.t(redisconstants.redis_key_well_area, wellarea, redisconstants.redis_timeout_1, timeunit.days);            // t(k key, v value, long timeout, timeunit unit)            //timeout:过期时间;  unit:时间单位              //使用:redistemplate.opsforvalue().t("name","tom",10, timeunit.conds);            //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<string, string> 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<object>) connection -> connection.info());    // 获取redis缓存命令统计信息    //properties commandstats = redistemplate.getrequiredconnectionfactory().getconnection().info("commandstats");    properties commandstats = (properties) redistemplate.execute((rediscallback<object>) connection -> connection.info("commandstats"));    // 获取redis缓存中可用键key的总数    //long dbsize = redistemplate.getrequiredconnectionfactory().getconnection().dbsize();    object dbsize = redistemplate.execute((rediscallback<object>) connection -> connection.dbsize());    map<string, object> result = new hashmap<>(3);    result.put("info", info);    result.put("dbsize", dbsize);    list<map<string, string>> pielist = new arraylist<>();    commandstats.stringpropertynames().foreach(key -> {      map<string, string> data = new hashmap<>(2);      string property = commandstats.getproperty(key);      data.put("name", stringutils.removestart(key, "cmdstat_"));      data.put("value", stringutils.substringbetween(property, "calls=", ",uc"));      pielist.add(data);    });    result.put("commandst四级及格分ats", pielist);    return ajaxresult.success(result);  }}

到此这篇关于springboot整合redistemplate实现缓存信息监控的文章就介绍到这了,更多相关springboot整合redistemplate内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 17:20:10,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8b64cb6b28ae97a7979351af2d36fb90.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:SpringBoot整合RedisTemplate实现缓存信息监控的步骤.doc

本文 PDF 下载地址:SpringBoot整合RedisTemplate实现缓存信息监控的步骤.pdf

标签:缓存   数据   对象   序列化
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图