public function getAllClicks() { $redis = get_redis(); $key = 'goods_clicks'; return $redis->zRange($key, 0, -1, true); // return $redis->zincrby($key,1,$goods_id); }
/** * 保用用户信息到redis * * @param Requests $requests * @author yangyifan <*****@*****.**> */ public function getSaveUserInfo(Request $requests) { $user_info = unserialize(urldecode($requests->get('user_info'))); //保存用户信息到redis hash表 load_func('instanceof,swoole'); //返回状态 get_redis()->hSet(config('config.user_list_hash_table'), $user_info->id, serialize($user_info)) != false ? $this->response(200, 'success') : $this->response(400, trans('response.save_user_info_to_redis_error')); }
/** * 获得用户web socket fd * * @param Request $request */ public function postSocketFd(Request $request) { $user_id = $request->get('id'); //加载函数库 load_func('instanceof,image'); //获得发送对象$fb $user_info = unserialize(get_redis()->hGet(config('config.user_list_hash_table'), $user_id)); if (!empty($user_info)) { $this->response(200, 'success', ['fd' => $user_info->web_socket_fd, 'name' => $user_info->user_name, 'face' => get_user_info_face($user_info->face)]); } else { $this->response(400, trans('response.save_user_socket_to_redis_error')); } }
/** * * @param string $url * 远程文件地址 * @return array false */ public function put($url, $content_type = "") { // get file content $file = file_get_contents($url); // content_type if (!$content_type) { $img_info = getimagesize($url); $content_type = $img_info['mime']; } if (!$file) { // 获取图片失败重试 for ($i = 0; $i < 3; $i++) { $file = file_get_contents($url); if ($file) { break; } } if (!$file) { global $CONF; $redis = get_redis($CONF['mix_cache']); $redis->hSet("oss_pic_log", $url, json_encode($file)); } } $__ = explode(".", $url); $ext = end($__); // file name $obj_name = md5($file) . "." . $ext; // select bucket $num = crc32($obj_name); $k = $num % count($this->_buckets) + 1; $bucket = $this->_buckets[$k]['name']; $host = $this->_buckets[$k]['host']; // 判断图片是否存在 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://{$host}/" . $obj_name); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, 1); $res = curl_exec($ch); if (preg_match("/HTTP\\/1\\.1 404 Not Found/i", $res)) { // put $result = $this->client->putObject(array('Bucket' => $bucket, 'Key' => $obj_name, 'Content' => $file, 'ContentType' => $content_type)); } // return $result->getETag(); return "http://{$host}/" . $obj_name; }
/** * 获得在线好友 * * @param $params array 用户登录名和密码参数 * @return int * @author yangyifan <*****@*****.**> */ public static function onlineUser() { load_func('instanceof,image,common'); $online_user = get_redis()->hGetAll(config('config.user_list_hash_table')); $item = []; //获得全部我的好友 $my_friends = FriendsModel::getMyFriends(); foreach ($online_user as $user) { $user = unserialize($user); //如果是自己,则跳过 || 如果不是自己好友,则跳过 if ($user->id == is_user_login() || !in_array($user->id, $my_friends)) { continue; } $item[] = ['id' => $user->id, 'name' => $user->user_name, 'face' => get_user_info_face($user->face), 'url' => action("User\\UserController@getIndex", ['id' => $user->id])]; } $data = [['name' => '在线好友', 'nums' => count($item), 'id' => 1, 'item' => $item]]; return $data; }
/** * 关闭连接时 * * @param swoole_websocket_server $server * @param $fd */ public function onClose(swoole_websocket_server $server, $fd) { //销毁当前连接对象 get_redis()->hDel($this->config['websocket_list'], $fd); echo "connection close: " . $fd . $this->swoole_config['package_eof']; }
function read_queue() { return get_redis()->rpop(REDIS_QUEUE_KEY); }
/** * 执行持久化数据处理 * * @param $ops * @return array * @author yangyifan <*****@*****.**> */ private function persistentFop($ops) { if (!empty($ops)) { $persistent_fop_ids = []; foreach ($ops as $v) { $persistent_fop_id = $this->disk->getDriver()->persistentFop($this->clientOriginalName, $v); //引入函数库 load_func('instanceof,swoole'); //写入队列 $redis = get_redis(); $redis->rPush(config('queue.queue_name.qiniu_persistentFop'), $persistent_fop_id); //发送task send_task_to_swoole_server(url('tools/upload/persistent-status'), ['persistent_fop_id' => $persistent_fop_id], url('tools/upload/persistent-success-callback')); array_push($persistent_fop_ids, $persistent_fop_id); } return $persistent_fop_ids; } }
/** * 释放锁 */ private function freeLock() { $redis = get_redis(); $redis->sRem('lock_stock', 1); }