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 counter function in Redis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In this issue, the editor will bring you about how to achieve a counter function in Redis. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Use string keys

The following code demonstrates how to use the string keys in Redis to implement the counter function. Where the incr () method is used to accumulate the count and the get_cnt () method is used to get the current count value.

From redis import Redis class Counter: def _ init__ (self, client: Redis, key: str): self.client = client self.key = key def incr (self, amount=1): "" self.client.incr (self.key, amount=amount) def decr (self, amount=1): "" count accumulation "self.client.decr (self.key) Amount=amount) def get_cnt (self): "get the value of the current count"return self.client.get (self.key) if _ _ name__ = ='_ main__': client = Redis (decode_responses=True) counter = Counter (client, 'page_view:12') counter.incr () counter.incr () print (counter.get_cnt ()) # 2

Suppose we want to count the number of views on a page with a page_id of 12, then we can set key to page_view:12, and every time the user browses, we call the incr () method of counter to count.

Use the hash key

In the above code, we need to set a separate string key for each statistic. So, let's take a look at how to manage the associated statistics uniformly through the hash key of Redis.

From redis import Redis class Counter: def _ _ init__ (self, client: Redis, key: str, counter: str): self.client = client self.key = key self.counter = counter def incr (self, amount=1): "" count accumulation "" self.client.hincrby (self.key, self.counter, amount=amount) def decr (self Amount=1): "" self.client.hincrby (self.key, self.counter, amount=-amount) def get_cnt (self): "get the value of the current count"return self.client.hget (self.key, self.counter) if _ _ name__ ='_ main__': client = Redis (decode_responses=True) counter = Counter (client, 'page_view')" '66') counter.incr () counter.incr () print (counter.get_cnt ()) # 2

If we use the hash key, we can use the same key to store the same type of count. For example, in the above example, we use page_view to count the number of page views. For a page with a page_id of 66, just add it to the corresponding field of page_view.

Use collection keys

In the above two examples, the program can call the incr () cumulative count method once when the action is executed. In some scenarios, we may need to count a specific action only once. What do you mean, "count only once"? That is, if the same user / IP visits a page multiple times, the counter will only increase the count value by 1. Take a look at the following code:

From redis import Redis class Counter: def _ _ init__ (self, client: Redis, key: str): self.client = client self.key = key def add (self, item: str)-> bool: "" count accumulation. If item already exists before counting, put it back to False Otherwise, return True "" return self.client.sadd (self.key, item) = = 1 def get_cnt (self): "get the value of the current count" return self.client.scard (self.key) if _ _ name__ ='_ _ main__': client = Redis (decode_responses=True) counter = Counter (client) " 'uv') counter.add (' user1') counter.add ('user2') counter.add (' user1') # repeatedly put into print (counter.get_cnt ()) # 2 this is how to implement a counter function in the Redis shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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

Database

Wechat

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

12
Report