MCPcopy Create free account
hub / github.com/NanmiCoder/MediaCrawler / IpCache

Class IpCache

proxy/base_proxy.py:54–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52
53
54class IpCache:
55 def __init__(self):
56 self.cache_client: AbstractCache = CacheFactory.create_cache(cache_type=config.CACHE_TYPE_REDIS)
57
58 def set_ip(self, ip_key: str, ip_value_info: str, ex: int):
59 """
60 Set IP with expiration time, Redis is responsible for deletion after expiration
61 :param ip_key:
62 :param ip_value_info:
63 :param ex:
64 :return:
65 """
66 self.cache_client.set(key=ip_key, value=ip_value_info, expire_time=ex)
67
68 def load_all_ip(self, proxy_brand_name: str) -> List[IpInfoModel]:
69 """
70 Load all unexpired IP information from Redis
71 :param proxy_brand_name: Proxy provider name
72 :return:
73 """
74 all_ip_list: List[IpInfoModel] = []
75 all_ip_keys: List[str] = self.cache_client.keys(pattern=f"{proxy_brand_name}_*")
76 try:
77 for ip_key in all_ip_keys:
78 ip_value = self.cache_client.get(ip_key)
79 if not ip_value:
80 continue
81 all_ip_list.append(IpInfoModel(**json.loads(ip_value)))
82 except Exception as e:
83 utils.logger.error("[IpCache.load_all_ip] get ip err from redis db", e)
84 return all_ip_list

Callers 3

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected