count.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 13:22:19 (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. return redis_data.hget(key, 'topic') or 0
  67. @classmethod
  68. def user_reply_count(cls, userId, value=None):
  69. key = 'count:user:%s' % str(userId)
  70. if value is not None:
  71. pipe = redis_data.pipeline()
  72. pipe.hincrby(key, 'replies', value)
  73. pipe.execute()
  74. return redis_data.hget(key, 'replies') or 0
  75. @classmethod
  76. def user_message_count(cls, userId, value=None, clear=False):
  77. key = 'count:user:%s' % str(userId)
  78. if value is not None:
  79. pipe = redis_data.pipeline()
  80. pipe.hincrby(key, 'message', value)
  81. pipe.execute()
  82. if clear:
  83. redis_data.hset(key, 'message', 0)
  84. return redis_data.hget(key, 'message') or 0
  85. @classmethod
  86. def user_email_time(cls, userId, value=None):
  87. key = 'count:user:%s' % str(userId)
  88. if value is not None:
  89. redis_data.hset(key, 'email', value)
  90. return redis_data.hget(key, 'email') or '2015-1-1 1:1:1'