Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement redis cache menu list by SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "SpringBoot how to achieve redis cache menu list", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "SpringBoot how to achieve redis cache menu list" this article.

Because the menu list of the system does not change easily, it does not need to query the database every time it is requested, so when the menu list is requested by the user id for the first time, the data of the menu list can be cached in redis, and when the menu list is requested for the second time, the data can be obtained directly in the redis cache, thus reducing the operation on the database and improving performance! First of all, we need to download redis locally, then open the src directory of redis in the cmd terminal, and then run redis-server to enable the redis local service (mac). After enabling the redis service, we need to configure the relevant redis code in the project. First, import the package we need in pom.xml:

Org.springframework.boot spring-boot-starter-data-redis 2.5.1 org.apache.commons commons-pool2 2.9.0

Then create the ReidsConfig configuration class file under the config file:

/ * redis configuration class * / @ Configurationpublic class RedisConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate=new RedisTemplate (); / / String type key serializer redisTemplate.setKeySerializer (new StringRedisSerializer ()); / / String type value serializer redisTemplate.setValueSerializer (new GenericJackson2JsonRedisSerializer ()); / / Hash type key serializer redisTemplate.setHashKeySerializer (new StringRedisSerializer ()) / / Hash type value serializer redisTemplate.setHashValueSerializer (new GenericJackson2JsonRedisSerializer ()); redisTemplate.setConnectionFactory (redisConnectionFactory); return redisTemplate;}}

This configuration file is mainly used to serialize key and value of String type and hash type.

The next step is to use redis. In the implementation class of the API for obtaining menu list based on user id that needs to be introduced into redis, it is judged that if the menu is already cached in redis, get the data back from redis, otherwise query the database to obtain data:

/ *

* menu list service implementation class *

* * @ author hhk * @ since 2022-01-04 * / @ Servicepublic class MenuServiceImpl extends ServiceImpl implements IMenuService {@ Autowired private MenuMapper menuMapper; @ Autowired private RedisTemplate redisTemplate / * * query menu list according to user id * @ return * / @ Override public List getMenuByAdminId () {/ / getMenuByAdminId needs to pass user id. In this case, to get the user id,SecurityContextHolder.getContext (). GetAuthentication (). GetPrincipal () from the global context of security, get the current user object Admin principal = (Admin) SecurityContextHolder.getContext (). GetAuthentication (). GetPrincipal () / / get user id Integer id = principal.getId (); ValueOperations valueOperations = redisTemplate.opsForValue (); List menus= ((List) valueOperations.get ("menu_" + id)); / / get menu list if (CollectionUtils.isEmpty (menus)) in redis cache {/ / if empty, get menus= menuMapper.getMenuByAdminId (id) from database / / set the data to redis valueOperations.set ("menu_" + id,menus);} return menus;}} these are all the contents of the article "how SpringBoot implements the redis cache menu list". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report