@@ -67,17 +67,15 @@ def decrease(self, proxy: Proxy) -> int:
6767 :param proxy: proxy
6868 :return: new score
6969 """
70- score = self .db .zscore (REDIS_KEY , proxy .string ())
71- # current score is larger than PROXY_SCORE_MIN
72- if score and score > PROXY_SCORE_MIN :
73- logger .info (f'{ proxy .string ()} current score { score } , decrease 1' )
74- if IS_REDIS_VERSION_2 :
75- return self .db .zincrby (REDIS_KEY , proxy .string (), - 1 )
76- return self .db .zincrby (REDIS_KEY , - 1 , proxy .string ())
77- # otherwise delete proxy
70+ if IS_REDIS_VERSION_2 :
71+ self .db .zincrby (REDIS_KEY , proxy .string (), - 1 )
7872 else :
73+ self .db .zincrby (REDIS_KEY , - 1 , proxy .string ())
74+ score = self .db .zscore (REDIS_KEY , proxy .string ())
75+ logger .info (f'{ proxy .string ()} score decrease 1, current { score } ' )
76+ if score <= PROXY_SCORE_MIN :
7977 logger .info (f'{ proxy .string ()} current score { score } , remove' )
80- return self .db .zrem (REDIS_KEY , proxy .string ())
78+ self .db .zrem (REDIS_KEY , proxy .string ())
8179
8280 def exists (self , proxy : Proxy ) -> bool :
8381 """
@@ -112,17 +110,19 @@ def all(self) -> List[Proxy]:
112110 """
113111 return convert_proxy_or_proxies (self .db .zrangebyscore (REDIS_KEY , PROXY_SCORE_MIN , PROXY_SCORE_MAX ))
114112
115- def batch (self , start , end ) -> List [Proxy ]:
113+ def batch (self , cursor , count ) -> List [Proxy ]:
116114 """
117115 get batch of proxies
118- :param start: start index
119- :param end: end index
116+ :param cursor: scan cursor
117+ :param count: scan count
120118 :return: list of proxies
121119 """
122- return convert_proxy_or_proxies (self .db .zrevrange (REDIS_KEY , start , end - 1 ))
120+ cursor , proxies = self .db .zscan (REDIS_KEY , cursor , count = count )
121+ return cursor , convert_proxy_or_proxies ([i [0 ] for i in proxies ])
123122
124123
125124if __name__ == '__main__' :
126125 conn = RedisClient ()
127126 result = conn .random ()
128127 print (result )
128+
0 commit comments