VideoGiftRedisService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class VideoGiftRedisService extends BaseRedisService
  10. {
  11. var $video_gift_db; //:gift_id hash数据
  12. var $video_gift_zset; //:gift_id zset数据
  13. /**
  14. +----------------------------------------------------------
  15. * 架构函数
  16. +----------------------------------------------------------
  17. * @access public
  18. +----------------------------------------------------------
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->video_gift_db = $this->prefix.'video_gift:';
  24. $this->video_gift_zset = $this->prefix.'video_gift_zset';
  25. }
  26. // public function test_add_redis($user_list){
  27. //
  28. //
  29. // set_time_limit(0);
  30. // $gift_id = 0;
  31. // $pipe = $this->redis->multi();
  32. // foreach($user_list as $k=>$v){
  33. // $gift_id = $v['id'];
  34. //
  35. // if($gift_id){
  36. // $data = $v;
  37. // $pipe->hMSet($this->video_gift_db.$gift_id,$data)
  38. // ;
  39. // }
  40. // }
  41. //
  42. // $replies = $pipe->exec();
  43. // $this->set_gift_id($gift_id);
  44. //
  45. // }
  46. /*
  47. * 添加
  48. */
  49. public function insert_db($gift_id,$data){
  50. $pipe = $this->redis->multi();
  51. if(!$gift_id){
  52. $gift_id = $this->get_gift_id();
  53. }else{
  54. $pipe->hSet($this->auto_id_db,'gift_id',$gift_id);
  55. }
  56. $data['id'] = $gift_id;
  57. $pipe->hMSet($this->video_gift_db,array($gift_id=>json_encode($data)));
  58. // $pipe->hMSet($this->video_gift_db.$gift_id,$data);
  59. $pipe->zAdd($this->video_gift_zset,0,$gift_id);
  60. $replies = $pipe->exec();
  61. if($replies[0]!==false){
  62. return $gift_id;
  63. }else{
  64. return 0;
  65. }
  66. }
  67. /*
  68. * 删除视频
  69. */
  70. public function del_db($gift_id){
  71. $this->redis->zRem($this->video_gift_zset,$gift_id);
  72. return $this->redis->hDel($this->video_gift_db,$gift_id);
  73. //return $this->redis->delete($this->video_gift_db.$gift_id);
  74. }
  75. /*
  76. * 更新视频信息
  77. */
  78. public function update_db($gift_id,$data){
  79. $old_data = $this->redis->hGet($this->video_gift_db,$gift_id);
  80. $old_data = json_decode($old_data,true);
  81. $data = array_merge($old_data,$data);
  82. return $this->redis->hSet($this->video_gift_db,$gift_id,json_encode($data));
  83. // return $this->redis->hMSet($this->video_gift_db.$gift_id,$data);
  84. }
  85. /*
  86. * 获取视频单个字段
  87. */
  88. public function getOne_db($gift_id,$field){
  89. $old_data = $this->redis->hGet($this->video_gift_db,$gift_id);
  90. $old_data = json_decode($old_data,true);
  91. if($old_data[$field]){
  92. return $old_data[$field];
  93. }else{
  94. return false;
  95. }
  96. // return $this->redis->hGet($this->video_gift_db.$gift_id,$field);
  97. }
  98. /*
  99. * 获取多个字段
  100. */
  101. public function getRow_db($gift_id,$fields=''){
  102. $old_data = $this->redis->hGet($this->video_gift_db,$gift_id);
  103. $old_data = json_decode($old_data,true);
  104. return $old_data;
  105. }
  106. /*
  107. * 获取全部礼物
  108. */
  109. public function getAll($num=300){
  110. if($num==-1){
  111. $data = $this->redis->hGetAll($this->video_gift_db);
  112. }else{
  113. $gift_keys = $this->redis->zRevRange($this->video_gift_zset,0,$num,true);
  114. $keys = array_keys($gift_keys);
  115. $data = $this->redis->hMGet($this->video_gift_db,$keys);
  116. }
  117. return $data;
  118. }
  119. /*
  120. * 批量删除礼物
  121. */
  122. public function hdel($field_arr){
  123. $data = $this->redis->hDel($this->video_gift_db,$field_arr);
  124. return $data;
  125. }
  126. }//类定义结束
  127. ?>