count.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2017 jianglin
  5. # File Name: count.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2017-03-29 21:28:52 (CST)
  9. # Last Update:星期日 2017-4-2 15:24:37 (CST)
  10. # By:
  11. # Description: 一些统计信息
  12. # **************************************************************************
  13. from flask import request
  14. from .extension import redis_data
  15. class Count(object):
  16. @classmethod
  17. def board_topic_count(cls, boardId, value=None):
  18. key = 'count:board:%s' % str(boardId)
  19. if value is not None:
  20. pipe = redis_data.pipeline()
  21. pipe.hincrby(key, 'topic', value)
  22. pipe.execute()
  23. return redis_data.hget(key, 'topic') or 0
  24. @classmethod
  25. def board_post_count(cls, boardId, value=None):
  26. key = 'count:board:%s' % str(boardId)
  27. if value is not None:
  28. pipe = redis_data.pipeline()
  29. pipe.hincrby(key, 'post', value)
  30. pipe.execute()
  31. return redis_data.hget(key, 'post') or 0
  32. @classmethod
  33. def topic_reply_count(cls, topicId, value=None):
  34. key = 'count:topic:%s' % str(topicId)
  35. if value is not None:
  36. pipe = redis_data.pipeline()
  37. pipe.hincrby(key, 'replies', value)
  38. pipe.execute()
  39. return redis_data.hget(key, 'replies') or 0
  40. @classmethod
  41. def topic_read_count(cls, topicId, value=None):
  42. key = 'count:topic:%s' % str(topicId)
  43. expire_key = 'expire:topic:read:{}'.format(request.remote_addr)
  44. if not redis_data.exists(expire_key):
  45. # 设置三分钟之内,阅读次数不增加
  46. redis_data.set(expire_key, '1')
  47. redis_data.expire(expire_key, 180)
  48. if value is not None:
  49. redis_data.hincrby(key, 'read', value)
  50. return redis_data.hget(key, 'read') or 0
  51. @classmethod
  52. def reply_liker_count(cls, replyId, value=None):
  53. key = 'count:reply:%s' % str(replyId)
  54. if value is not None:
  55. pipe = redis_data.pipeline()
  56. pipe.hincrby(key, 'liker', value)
  57. pipe.execute()
  58. return redis_data.hget(key, 'liker') or 0
  59. @classmethod
  60. def user_topic_count(cls, userId, value=None):
  61. key = 'count:user:%s' % str(userId)
  62. if value is not None:
  63. pipe = redis_data.pipeline()
  64. pipe.hincrby(key, 'topic', value)
  65. pipe.execute()
  66. cls.forums_post_count(1)
  67. cls.forums_topic_count(1)
  68. return redis_data.hget(key, 'topic') or 0
  69. @classmethod
  70. def user_reply_count(cls, userId, value=None):
  71. key = 'count:user:%s' % str(userId)
  72. if value is not None:
  73. pipe = redis_data.pipeline()
  74. pipe.hincrby(key, 'replies', value)
  75. pipe.execute()
  76. cls.forums_post_count(1)
  77. return redis_data.hget(key, 'replies') or 0
  78. @classmethod
  79. def user_message_count(cls, userId, value=None, clear=False):
  80. key = 'count:user:%s' % str(userId)
  81. if value is not None:
  82. pipe = redis_data.pipeline()
  83. pipe.hincrby(key, 'message', value)
  84. pipe.execute()
  85. if clear:
  86. redis_data.hset(key, 'message', 0)
  87. return redis_data.hget(key, 'message') or 0
  88. @classmethod
  89. def user_email_time(cls, userId, value=None):
  90. key = 'count:user:%s' % str(userId)
  91. if value is not None:
  92. redis_data.hset(key, 'email', value)
  93. return redis_data.hget(key, 'email') or '2015-1-1 1:1:1'
  94. @classmethod
  95. def forums_user_count(cls, value=None):
  96. key = 'count:forums'
  97. if value is not None:
  98. redis_data.hincrby(key, 'user', value)
  99. return redis_data.hget(key, 'user') or 0
  100. @classmethod
  101. def forums_topic_count(cls, value=None):
  102. key = 'count:forums'
  103. if value is not None:
  104. redis_data.hincrby(key, 'topic', value)
  105. return redis_data.hget(key, 'topic') or 0
  106. @classmethod
  107. def forums_post_count(cls, value=None):
  108. key = 'count:forums'
  109. if value is not None:
  110. redis_data.hincrby(key, 'post', value)
  111. return redis_data.hget(key, 'post') or 0