In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how SpringBoot integrates RedisTemplate to monitor cache information. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The essence of SpringBoot integrating Redis database to realize data cache is to integrate Redis database. By storing the data that needs to be "cached" into Redis database, the next time it is used, it is first obtained from Redis, but not from the database in Redis. Thus, Redis is used as data cache.
According to convention, the following step-by-step implementation of Springboot integrates Redis to store and read data.
1. The first step in adding a project that depends on the home page is to add the environment of Redis in the project, Jedis.
Org.springframework.boot spring-boot-starter-data-redis redis.clients jedis
two。 Add parameters for redis
Spring: # Redis Configuration redis: pool: max-idle: 10 min-idle: 5 max-total: 20 hostName: 127.0.0.1 port: 6379
3. Write a RedisConfig to register with the Spring container
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;/** * description: Redis configuration class * / @ Configurationpublic class RedisConfig {/ * 1. Create a JedisPoolConfig object. Complete the configuration of some connection pools in this object * / @ Bean @ ConfigurationProperties (prefix= "spring.redis.pool") public JedisPoolConfig jedisPoolConfig () {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig (); return jedisPoolConfig;} / * 2. Create JedisConnectionFactory: configure redis connection information * / @ Bean @ ConfigurationProperties (prefix= "spring.redis") public JedisConnectionFactory jedisConnectionFactory (JedisPoolConfig jedisPoolConfig) {JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory (jedisPoolConfig); return jedisConnectionFactory;} / * 3. Create RedisTemplate: method for performing Redis operation * / @ Bean public RedisTemplate redisTemplate (JedisConnectionFactory jedisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); / / Associate redisTemplate.setConnectionFactory (jedisConnectionFactory) / / set serializer redisTemplate.setKeySerializer (new StringRedisSerializer ()) for key; / / set serializer redisTemplate.setValueSerializer (new StringRedisSerializer ()) for value; return redisTemplate;}}
4. Use 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 integrates the Redis test Controller* @ version V1.0*/@RestControllerpublic class RedisController {@ Autowired private RedisTemplate redisTemplate; @ RequestMapping ("/ redishandle") public String redishandle () {/ / add the string redisTemplate.opsForValue () .set ("author", "Ouyang") / / get the string String value = (String) redisTemplate.opsForValue () .get ("author"); System.out.println ("author =" + value); / / add objects / / reset serializer redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ()) RedisTemplate.opsForValue () .set ("users", new Users ("1", "Zhang San")); / / get the object / / reset the serializer redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ()); Users user = (Users) redisTemplate.opsForValue () .get ("users") System.out.println (user); / / Store objects in json format / / reset serializer redisTemplate.setValueSerializer (new Jackson2JsonRedisSerializer (Users.class)); redisTemplate.opsForValue () .set ("usersJson", new Users ("2", "Li Si")) / / get objects in json format / / reset serializer redisTemplate.setValueSerializer (new Jackson2JsonRedisSerializer (Users.class)); user = (Users) redisTemplate.opsForValue () .get ("usersJson"); System.out.println (user); return "home" }}
5. The use of redisTemplate in Project practice
/ * * / 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 where key is the string ValueOperations opsForValue = stringRedisTemplate.opsForValue () / get the data in redis through key String wellArea = opsForValue.get (RedisConstants.REDIS_KEY_WELL_AREA); / / if you don't check the database if (wellArea = = null) {List wellAreaList = wellAreaDao.getAll (); wellArea = new ObjectMapper () .writeValueAsString (wellAreaList) / / store the checked data in 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: expiration time Unit: unit of time / / use: redisTemplate.opsForValue (). Set ("name", "tom", 10, TimeUnit.SECONDS); / / redisTemplate.opsForValue () .get ("name") because it is set to expire in 10 seconds, the query has a result within 10 seconds, and returns null} return wellArea;} after 10 seconds.
Key used in 6.redis
Package com.shiwen.lujing.util;/** * redis related constant * * / public interface RedisConstants {/ * wellhead * / String REDIS_KEY_JING_SHOU_ZI = "JING-SHOU-ZI"; / * well block * / String REDIS_KEY_WELL_AREA = "WELL-AREA" / * / long REDIS_TIMEOUT_1 = 1L;}
Add: SpringBoot integrates RedisTemplate to realize cache information monitoring
1. CacheController interface code
@ RestController@RequestMapping ("/ monitor/cache") public class CacheController {@ Autowired private RedisTemplate redisTemplate; @ PreAuthorize ("@ ss.hasPermi ('monitor:cache:list')") / Custom permission Note @ GetMapping () public AjaxResult getInfo () throws Exception {/ / get full information about redis cache / / Properties info = redisTemplate.getRequiredConnectionFactory (). GetConnection (). Info () Properties info = (Properties) redisTemplate.execute ((RedisCallback) connection-> connection.info ()); / / get redis cache command statistics / / Properties commandStats = redisTemplate.getRequiredConnectionFactory (). GetConnection () .info ("commandstats"); Properties commandStats = (Properties) redisTemplate.execute ((RedisCallback) connection-> connection.info ("commandstats")) / / get the total number of keys Key available in the redis cache / / Long dbSize = redisTemplate.getRequiredConnectionFactory (). GetConnection (). DbSize (); Object dbSize = redisTemplate.execute ((RedisCallback) connection-> connection.dbSize ()); Map result = new HashMap (3); result.put ("info", info); result.put ("dbSize", dbSize); List pieList = new ArrayList () CommandStats.stringPropertyNames () .forEach (key-> {Map data = new HashMap (2); String property = commandStats.getProperty (key); data.put ("name", StringUtils.removeStart (key, "cmdstat_")); data.put ("value", StringUtils.substringBetween (property, "calls=", ", usec"); pieList.add (data);}) Result.put ("commandStats", pieList); return AjaxResult.success (result);}} Thank you for reading! This is the end of the article on "how SpringBoot integrates RedisTemplate to achieve cache information monitoring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un