hot_topic_client.py 611 B

12345678910111213141516171819202122
  1. import time
  2. from pqai_agent.toolkit.hot_topic_toolkit import HotTopicToolkit
  3. class HotTopicClient:
  4. def __init__(self):
  5. self._cache = None
  6. self._cache_time = 0
  7. self._cache_ttl = 300 # 5分钟
  8. self._toolkit = HotTopicToolkit()
  9. def get_hot_topics(self):
  10. now = time.time()
  11. if self._cache and (now - self._cache_time) < self._cache_ttl:
  12. return self._cache
  13. topics = self._toolkit.get_hot_topics()
  14. self._cache = topics
  15. self._cache_time = now
  16. return topics
  17. # 全局可用实例
  18. hot_topic_client = HotTopicClient()