ActionTimeRedis.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * ActionTimeRedis
  4. * 用法:
  5. * //首先得到key,(可以为多个参数)
  6. * $Key = HelperKey::generateActionTimeKey(ActionTimeRedis::TYPE_GET_GROUP);
  7. * // get
  8. * $indexSlideData = GroupTopicRedis::get($Key);
  9. * // set
  10. * $rs = GroupTopicRedis::set($Key, $data);
  11. * $Id$
  12. */
  13. class ActionTimeRedis extends RedisAr{
  14. /**
  15. * 操作类型:groupinfo
  16. */
  17. const TYPE_GET_GROUP = 'group_info';
  18. const TYPE_GET_INDEX = 'get_index';
  19. const TYPE_GET_CITY_TOPIC = 'get_city_topics';
  20. const TYPE_GET_RECOMMEND = 'get_recommend';
  21. /**
  22. * 操作类型:topicinfo
  23. */
  24. const TYPE_GET_TOPIC = 'topic_info';
  25. const TYPE_GET_SCORELOG = 'score_log';
  26. /**
  27. * 操作类型:subjecttopic
  28. */
  29. const TYPE_GET_SUBJECT_TOPIC = 'subject_topic';
  30. /**
  31. * _prefix
  32. * 前缀
  33. *
  34. * @var mixed
  35. */
  36. public static $_prefix = 'atime_';
  37. /**
  38. * get
  39. *
  40. * @param mixed $key
  41. * @return void
  42. */
  43. public static function get($key) {
  44. try {
  45. return Yii::app()->redis->getClient()->hgetall($key);
  46. } catch (Exception $e) {
  47. throw new CException(Yii::t('model', $e->getMessage()));
  48. }
  49. }
  50. /**
  51. * set
  52. *
  53. * @param mixed $data
  54. * @return void
  55. * @param $time 在redis里的有效时间
  56. */
  57. public static function set($key, $data, $time = 86400) {
  58. try {
  59. $return = Yii::app()->redis->getClient()->hmset($key, $data);
  60. if($time != null){
  61. return Yii::app()->redis->getClient()->expire($key, $time);
  62. }else{
  63. return $return;
  64. }
  65. } catch (Exception $e) {
  66. throw new CException(Yii::t('model', $e->getMessage()));
  67. }
  68. }
  69. /**
  70. * remove
  71. * hDel每次只能删除单个Key,
  72. * 所以此处需要一个循环来逐个删除字段
  73. *
  74. * @param mixed $key
  75. * @return void
  76. */
  77. public static function remove($key) {
  78. try {
  79. $keys = Yii::app()->redis->getClient()->hkeys($key);
  80. foreach ($keys as $k) {
  81. Yii::app()->redis->getClient()->hdel($key, $k);
  82. }
  83. return true;
  84. } catch (Exception $e) {
  85. throw new CException(Yii::t('model', $e->getMessage()));
  86. }
  87. }
  88. }