In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What are the ideas for Redis to achieve cache, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Scene 1: similar to Weibo, to achieve the function of following and being followed.
Train of thought:
Two set type keys are used for each user to store the users who follow others and the users who are followed by that user. When user A follows user B, perform two steps:
Sadd user:A B
Sadd user:B A
Question 1:
To complete a user attention operation, two steps of code need to be executed, and user A follows B for the first time, becoming a fan of B. In the second step, if the execution is not carried out or is successful for some reason, A does not know that B is paying attention to him.
Transaction:
The principle of a transaction is to send commands for a transaction to Redis, and then have Redis execute these commands in turn.
In a transaction, either all are executed successfully or none are executed
The multi command tells redis to prepare to introduce the transaction, save it temporarily, and don't execute it. Then send the commands that need to be executed together. Finally, exec ends the transaction, and redis executes the commands in turn.
If redis is broken for some reason before executing the exec command, Redis will empty the transaction queue.
Question 2:
Sometimes, we not only need to deal with some actions that must be successful together through transactions, such as bank transfer, transferring money from bank card 1 to bank card 2, must be successful together, it cannot be said that after deducting money from bank card 1, did not enter the bank card 2 account, for example, both actions should be completed. Sometimes, we not only have to do it together, but also need to proceed to the next step according to the result of one of the steps.
WATCH command
Watch, another command in the transaction. Monitor one or more keys, and once one of the keys is modified or deleted, subsequent transactions are not executed until the end of the exec.
Key1 is monitored through watch, and then key1 is modified, so the set command in the transaction is not executed
Scenario 2: time-limited activity, cache, CAPTCHA invalidation
In actual development, we often encounter scenarios such as time-limited activities, mailbox failure time, CAPTCHA expiration time, and so on. These data need to be valid within a certain period of time and deleted after expiration. If you save these data in a relational database, you need to query the data for each verification, compare the time, and then set the data to invalidate or delete. In Redis, you can set the expiration time through expire and delete it automatically.
Expire sets the failure time (in seconds). 1 is returned successfully, and 0 is returned if key does not exist
Returns-2 when key does not exist. Returns-1 when key exists but the remaining time to live is not set.
Note: prior to Redis 2.8When key does not exist or key does not set the remaining time to live, the command returns-1
Implement caching
In order to provide the load capacity of the website, it is necessary to cache the results of an operation with high access frequency and complex calculation or IO resource consumption, and set an expiration time. Every time the user visits, check whether the key exists. If it exists, get the element directly and return it. If not, the result is cached after a series of calculations, and the expiration time is set before returning to the user.
Question:
There are obviously two problems in the implementation of this kind of cache. the first is that the cache exists in the content, if a large number of caches are used, the Redis will be stained with memory, on the other hand, in order to prevent the Redis from being stained with memory, setting the expiration time key, if the setting time is too short, may lead to too low cache hit rate and a large amount of content wasted.
Mode of use:
In development, it is difficult to set the survival time of keys reasonably, so you can limit the maximum memory used by Redis and let Redis delete some unwanted keys according to certain rules.
To modify the maxmemory parameter of the configuration file,
For redis installed through Homebrew under OS X, the configuration file is at / usr/local/etc/redis.conf
After limiting the maximum memory of Redis, when this memory is exceeded, it will be based on the maxmemory-policy
The specified parameter removes unwanted keys. When this parameter is set to allkeys-lru, once the Redis memory exceeds the limit, Redis will continue to delete the least recently used keys in the database until the current memory limit is met.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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
© 2024 shulou.com SLNews company. All rights reserved.