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 > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article uses the example code to show several application scenarios of Redis string type, the code is very detailed, which can be used as a reference for interested friends. I hope it will be helpful to you.
Current limit
Redis can be used to limit the current. Redis itself has excellent performance and can be used to fend off some requests.
Defense against heavy traffic
Now website attacks are increasingly rampant, static resources can use cdn, but dynamic programs how to defend. Next, show a redis solution to defend against a large number of requests.
The basic idea is to limit the number of requests in an ip within a certain period of time, for example, the number of requests per second in an ip cannot exceed 10.
The pseudo code is as follows:
Function ip_limit ($cnt = 10) {$ip = $SERVER ['SERVER_ADDR']; $key = "limit:$ip:cnt"; $isExisit = $redis- > set ($key, 1, [' nx', 'ex'= > 1]); if (! $isExisit | | $redis- > incr ($key) set ($key, 1, [' nx', 'ex'= > $time]); if (! $isExisit) {/ / through return true } else {/ / do not pass return false;}}
Store session
Using redis to store session is now the recommended way, its advantage is high performance, automatic expiration ecstasy. (PHP's own session garbage collection mechanism is not reliable, and there is a probability of expiration.)
This method is very simple, as long as you modify the php.ini file.
Find the session.save_handler line and change it to
Session.save_handler = redis
Modify session.save_path to
Session.save_path = "tcp://localhost:6379"
How to set the password for redis, just change it to
Session.save_path = "tcp://localhost:6379?auth=password"
If it is not a stand-alone application, but distributed, how to use redis to centrally manage session?
It's simple, too. Also, as long as you modify the php.ini file. For example, session is centrally managed on 192.168.1.220, and the php.ini files of other machines are modified as follows:
Session.save_handler = redissession.save_path = "tcp://192.168.1.220:6379?auth=password"
The above are several application scenarios of Redis string type. If you encounter this problem in your daily work, I hope you can solve the problem through this article. If you want to know more about it, 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.
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.