count.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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-1 20:47:11 (CST)
  10. # By:
  11. # Description: 一些统计信息
  12. # **************************************************************************
  13. from .extension import redis_data
  14. class Count(object):
  15. @classmethod
  16. def board_topic_count(cls, boardId, value=None):
  17. key = 'count:board:%s' % str(boardId)
  18. if value is not None:
  19. pipe = redis_data.pipeline()
  20. pipe.hincrby(key, 'topic', value)
  21. pipe.execute()
  22. return redis_data.hget(key, 'topic') or 0
  23. @classmethod
  24. def board_post_count(cls, boardId, value=None):
  25. key = 'count:board:%s' % str(boardId)
  26. if value is not None:
  27. pipe = redis_data.pipeline()
  28. pipe.hincrby(key, 'post', value)
  29. pipe.execute()
  30. return redis_data.hget(key, 'post') or 0
  31. @classmethod
  32. def topic_reply_count(cls, topicId, value=None):
  33. key = 'count:topic:%s' % str(topicId)
  34. if value is not None:
  35. pipe = redis_data.pipeline()
  36. pipe.hincrby(key, 'replies', value)
  37. pipe.execute()
  38. return redis_data.hget(key, 'replies') or 0
  39. @classmethod
  40. def topic_read_count(cls, topicId, value=None):
  41. key = 'count:topic:%s' % str(topicId)
  42. if value is not None:
  43. pipe = redis_data.pipeline()
  44. pipe.hincrby(key, 'read', value)
  45. pipe.execute()
  46. return redis_data.hget(key, 'read') or 0
  47. @classmethod
  48. def reply_liker_count(cls, replyId, value=None):
  49. key = 'count:reply:%s' % str(replyId)
  50. if value is not None:
  51. pipe = redis_data.pipeline()
  52. pipe.hincrby(key, 'likers', value)
  53. pipe.execute()
  54. return redis_data.hget(key, 'likers') or 0
  55. @classmethod
  56. def user_topic_count(cls, userId, value=None):
  57. key = 'count:user:%s' % str(userId)
  58. if value is not None:
  59. pipe = redis_data.pipeline()
  60. pipe.hincrby(key, 'topic', value)
  61. pipe.execute()
  62. return redis_data.hget(key, 'topic') or 0
  63. @classmethod
  64. def user_reply_count(cls, userId, value=None):
  65. key = 'count:user:%s' % str(userId)
  66. if value is not None:
  67. pipe = redis_data.pipeline()
  68. pipe.hincrby(key, 'replies', value)
  69. pipe.execute()
  70. return redis_data.hget(key, 'replies') or 0
  71. @classmethod
  72. def user_message_count(cls, userId, value=None, clear=False):
  73. key = 'count:user:%s' % str(userId)
  74. if value is not None:
  75. pipe = redis_data.pipeline()
  76. pipe.hincrby(key, 'message', value)
  77. pipe.execute()
  78. if clear:
  79. redis_data.hset(key, 'message', 0)
  80. return redis_data.hget(key, 'message') or 0