cache_refresh.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from sql import DB
  2. from configure import conf
  3. from sql.archive import read_archive, get_archive_list_iter, get_blog_archive
  4. from sql.blog import read_blog, get_blog_count, get_archive_blog_count, get_user_blog_count, get_blog_list_iter
  5. from sql.comment import read_comment, read_comment_list_iter, get_user_comment_count
  6. from sql.msg import read_msg, read_msg_list_iter, get_msg_count, get_user_msg_count
  7. from sql.user import (read_user, get_user_list_iter, get_role_list_iter,
  8. get_user_email, get_role_name, check_role, role_authority)
  9. import logging.handlers
  10. import os
  11. refresh_logger = logging.getLogger("main.refresh")
  12. refresh_logger.setLevel(conf["LOG_LEVEL"])
  13. if len(conf["LOG_HOME"]) > 0:
  14. handle = logging.handlers.TimedRotatingFileHandler(
  15. os.path.join(conf["LOG_HOME"], f"redis-refresh.log"), backupCount=10)
  16. handle.setFormatter(logging.Formatter(conf["LOG_FORMAT"]))
  17. refresh_logger.addHandler(handle)
  18. def refresh():
  19. mysql = DB(host=conf["MYSQL_URL"],
  20. name=conf["MYSQL_NAME"],
  21. passwd=conf["MYSQL_PASSWD"],
  22. port=conf["MYSQL_PORT"],
  23. database=conf["MYSQL_DATABASE"])
  24. refresh_logger.info("refresh redis cache started.")
  25. for i in get_archive_list_iter():
  26. read_archive(i[0], mysql, not_cache=True)
  27. get_archive_blog_count(i[0], mysql, not_cache=True)
  28. for i in get_blog_list_iter():
  29. read_blog(i[0], mysql, not_cache=True)
  30. get_blog_archive(i[0], mysql, not_cache=True)
  31. get_blog_count(mysql, not_cache=True)
  32. for i in read_comment_list_iter():
  33. read_comment(i[0], mysql, not_cache=True)
  34. for i in read_msg_list_iter():
  35. read_msg(i[0], mysql, not_cache=True)
  36. get_msg_count(mysql, not_cache=True)
  37. for i in get_user_list_iter():
  38. email = get_user_email(i[0], mysql, not_cache=True)
  39. get_user_blog_count(i[0], mysql, not_cache=True)
  40. get_user_comment_count(i[0], mysql, not_cache=True)
  41. get_user_msg_count(i[0], mysql, not_cache=True)
  42. read_user(email, mysql, not_cache=True)
  43. for i in get_role_list_iter():
  44. get_role_name(i[0], mysql, not_cache=True)
  45. for a in role_authority:
  46. check_role(i[0], a, mysql, not_cache=True)
  47. refresh_logger.info("refresh redis cache finished.")