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 a general script for adding, deleting, modifying and querying Redis

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to achieve the general Redis add, delete, change and search script, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Think about what kind of repetitive code?

If    is just for simple additions, deletions, modifications and queries, then just tell me which class is to be serialized and deserialized, and tell me what the prefix of key is. As for the expiration time, we can ignore it this time, and if we need to add the expiration time, it is not a difficult thing, so we can think about it a little bit and use the generics in Java to get the following basic classes:

two。 The key prefix * / private String prefixString; private Class targetClass; @ Autowired private StringRedisTemplate redisTemplate; public BasicDataRedisService (String prefixString, Class targetClass) {this.prefixString = prefixString; this.targetClass = targetClass in the basic service class public class BasicDataRedisService {/ * Redis key prefixString * Redis } / /-for internal use-- protected String generateKey (String id) {return prefixString + "id:" + id;} protected T getByKey (String key) {T result = ValueRedisUtil.getRedisObject (redisTemplate, key, targetClass) Return result;} protected boolean setByKey (String key, Object object) {return ValueRedisUtil.setRedisObject (redisTemplate, key, object);} protected boolean deleteByKey (String key) {return ValueRedisUtil.delRedis (redisTemplate, key) } / /-the following is provided-- / * obtain * * @ param id * @ return * / public T getById (String id) {String key = generateKey (id) according to id Return getByKey (key);} / * * Refresh according to id * * @ param data * @ param id * @ return * / public boolean setByModel (Object data, String id) {String key = generateKey (id); return setByKey (key, data) Delete * * @ param id * @ return * / public boolean deleteById (String id) {String key = generateKey (id); return deleteByKey (key);}} 3 according to id. Customize a query service class

   assumes that we want to query the user, so we just need to write:

@ Servicepublic class UserRedisServiceImpl extends BasicDataRedisService {private static String PREFIX = "henbao:user:"; public UserRedisServiceImpl () {super (PREFIX, User.class);}} 4. Think about the disadvantages of writing in this way

Although    saves the amount of code by writing in this way, and the convenience it wants to achieve can also be achieved, it always feels that something is wrong and the operation seems to be a little more coquettish. Teenager, if you can have such an idea, then congratulations, you have a higher pursuit of code. We have now written the class twice, writing constants to show what the prefix in the name Redis is. This is not how we usually tell the underlying framework when we use Spring. Do we usually use annotations? So Redis Repositories made his debut.

5. Each has its advantages and disadvantages

Although we are happy to write    in this way, some developers want the content stored in redis to be exactly the same as the Model in the db layer, and the overwrite wave will be generated in reverse after modifying the database table structure. Then please redevelop it yourself and submit more powerful code for Spring! At present, it is recommended to have a separate redis layer model to use this method. Of course, it has all the problems that should be found in the annotation scan, so you can be patient when dealing with it.

The above is "how to achieve a general Redis add, delete, change and query script" all the content of this article, 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

Servers

Wechat

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

12
Report