PHP redis client封装
config1.inc.php
if (!isset($_REQUEST['pageName']) || $_REQUEST['pageName'] != 'txl-app-test') {
$CONFIG_REDIS = array(
array('host' => '127.0.0.1', 'port' => '6379', 'dbIndex' => 0, 'password'=>'aspire987')
);
} else {
$CONFIG_REDIS = array(
array('host' => '192.168.19.29', 'port' => '6379', 'dbIndex' => 0, 'password'=>'3695a77369be021075b480048142a3c2'),
array('host' => '192.168.19.30', 'port' => '6379', 'dbIndex' => 0, 'password'=>'3695a77369be021075b480048142a3c2')
);
}
UtilRedis2.class.php
<?php
class UtilRedis2 { private static $_self = null;
private $_servers = array();
private $_conn = array();
private $_conn_keys = array(); const CONNECT_TIMEOUT = 5; public static function &getInstance() {
// TODO Auto-generated method stub
if (null == self::$_self)
{
self::$_self = new self();
}
return self::$_self;
} private function __construct() {
$this->_servers = $GLOBALS['CONFIG_REDIS'];
} private function getConnection( $key ) { $serverCnt = count( $this->_servers );
$hash = md5( $key );
$serverIndex = $hash % $serverCnt; if ( !isset( $this->_conn[ $serverIndex ] ) ) {
$this->_conn[ $serverIndex ] = new Redis();
$this->_conn[ $serverIndex ]->pconnect(
$this->_servers[$serverIndex]['host'],
$this->_servers[$serverIndex]['port'],
self::CONNECT_TIMEOUT
);
$this->_conn[ $serverIndex ]->auth($this->_servers[$serverIndex]['password']);
$this->_conn[ $serverIndex ]->select( $this->_servers[$serverIndex]['dbIndex'] );
} return $this->_conn[ $serverIndex ];
} public function set( $key, $value, $expires = 0 ) {
$conn = $this->getConnection( $key ); if( $conn->set( $key, $value ) && $expires > 0 )
return $conn->setTimeout($key, $expires);
return true;
} public function setnx( $key, $value) {
$conn = $this->getConnection( $key );
return $conn->setnx( $key, $value );
} public function incr( $key) {
$conn = $this->getConnection( $key );
return $conn->incr( $key );
} public function decr( $key) {
$conn = $this->getConnection( $key );
return $conn->decr( $key );
} public function get ( $key ) {
$conn = $this->getConnection( $key ); $value = $conn->get( $key );
if (is_null(json_decode($value))) { //非json数据的直接返回
return $value;
} else {
return json_decode($value, true);
}
} public function del( $keys ) {
if ( is_array($keys) ) {
foreach ($keys as $key ) {
$conn = $this->getConnection( $key );
$conn->del( $key );
}
} else {
$conn = $this->getConnection( $keys );
$conn->del( $keys );
}
return true;
} public function exists( $key ) {
$conn = $this->getConnection( $key );
return $conn->exists( $key );
} public function sAdd( $key, $value, $expires = 0 ) { $conn = $this->getConnection( $key ); if( $conn->sAdd( $key, json_encode($value) ) && $expires > 0 )
return $conn->setTimeout($key, $expires);
return true;
} public function sPop( $key ) {
$conn = $this->getConnection( $key );
return $conn->sPop( $key );
} public function sMembers( $key ) {
$conn = $this->getConnection( $key );
return $conn->sMembers( $key );
} public function sIsMember( $key, $value ) {
$conn = $this->getConnection( $key );
return $conn->sIsMember( $key, json_encode($value) );
} public function sCard( $key ) {
$conn = $this->getConnection( $key );
return $conn->sCard( $key );
} public function hSet($hash, $key, $value, $expires = 0) {
$conn = $this->getConnection( $hash );
$result = $conn->hSet($hash, $key, $value);
if ($result && $expires > 0) {
return $conn->setTimeout($hash, $expires);
}
return $result;
} public function hSetNx($hash, $key, $value, $expires = 0) {
$conn = $this->getConnection( $hash );
$result = $conn->hSetNx($hash, $key, $value);
if ($result && $expires > 0) {
return $conn->setTimeout($hash, $expires);
}
return $result;
} public function hGet($hash, $key) {
$conn = $this->getConnection( $hash );
return $conn->hGet($hash, $key);
} public function hLen($hash) {
$conn = $this->getConnection($hash);
return $conn->hLen($hash);
} public function hDel($hash, $key) {
$conn = $this->getConnection( $hash );
return $conn->hDel($hash, $key);
} public function hKeys($hash) {
$conn = $this->getConnection($hash);
return $conn->hKeys($hash);
} public function hVals($hash) {
$conn = $this->getConnection($hash);
return $conn->hVals($hash);
} public function hGetAll($hash) {
$conn = $this->getConnection($hash);
return $conn->hGetAll($hash);
} public function hExists($hash, $key) {
$conn = $this->getConnection( $hash );
return $conn->hExists($hash, $key);
} public function hMset($hash, $keyVal, $expires = 0) {
$conn = $this->getConnection( $hash );
$result = $conn->hMset($hash, $keyVal);
if ($result && $expires > 0) {
return $conn->setTimeout($hash, $expires);
}
return $result;
} public function hMGet($hash, $keys) {
$conn = $this->getConnection( $hash );
return $conn->hMGet($hash, $keys);
} public function close() {
$conn = $this->getConnection( 'redis_close' );
return $conn->close();
} public function setTimeout($key, $expires=0) {
return $conn->setTimeout($key, $expires);
} public function keys($keys) { //模糊匹配
$return = array();
foreach ($this->_servers as $serverIndex => $server) {
if ( !isset( $this->_conn_keys[ $serverIndex ] ) ) {
$this->_conn_keys[ $serverIndex ] = new Redis();
$this->_conn_keys[ $serverIndex ]->pconnect(
$server['host'],
$server['port'],
self::CONNECT_TIMEOUT
);
$this->_conn_keys[ $serverIndex ]->auth($server['password']);
$this->_conn_keys[ $serverIndex ]->select( $server['dbIndex'] );
}
$conn = $this->_conn_keys[ $serverIndex ];
$ret = $conn->keys($keys);
$return = array_merge($return, $ret);
}
return $return;
} public function zAdd($key, $score, $value, $expires = 0) {
$conn = $this->getConnection( $hash );
$result = $conn->zAdd($key, $score, $value);
if ($result && $expires > 0) {
return $conn->setTimeout($key, $expires);
}
return $result;
} public function zRevRange($key, $begin = 0, $end = -1, $withScore = false) {
$conn = $this->getConnection( $hash );
if ($withScore) {
$result = $conn->zRevRange($key, $begin, $end, true);
} else {
$result = $conn->zRevRange($key, $begin, $end);
}
return $result;
} public function zRevRank($key, $value) {
$conn = $this->getConnection( $hash );
return $conn->zRevRank($key, $value);
} public function zCard($key) {
$conn = $this->getConnection( $hash );
return $conn->zCard($key);
} public function rPush($key, $val, $expires = 0) {
$conn = $this->getConnection( $key );
$result = $conn->rPush($key, $val);
if ($result && $expires > 0) {
return $conn->setTimeout($key, $expires);
}
return $result;
} public function lPop($key) {
$conn = $this->getConnection( $key );
return $conn->lPop($key);
} public function lLen($key) {
$conn = $this->getConnection( $key );
return $conn->lLen($key);
} public function lRange($key, $start=0, $end=-1) {
$conn = $this->getConnection( $key );
return $conn->lRange($key, $start, $end);
} /**
* 获取/设置过期时间
* @param unknown $key
* @param string $expires 空为获取,有值则为设置
*/
public function ttl($key, $expires = '') {
$conn = $this->getConnection( $key );
if ($expires === '') {
return $conn->ttl($key);
} else {
return $conn->setTimeout($key, $expires);
}
}
}
idlogin.php
<?php /**
* 多方电话h5 - 取号登录接口
*/
$action = '多方电话h5_取号登录';
include_once '../api_driver.php';
include_once 'config.php'; $loginId = isset($request['params']['loginId']) ? $request['params']['loginId'] : ''; $redis = UtilRedis2::getInstance(); $mobile = $redis->get('multitalk_loginId_' . $loginId);
if (!$mobile) {
json_result(101, array(), '登录令牌已失效,请重试!');
}
$params = array(
'credential_key16' => openssl_encrypt($mobile, 'AES-128-ECB', substr(APP_SECRET_API, 0, 16)),
'type' => 'trustvalidate',
'autoRegister' => 1,
'channel' => 'mcontact_www',
);
$params = Api::createPostString($params, '');
$sret = Api::cmd('auth.getPimSt', $params); if ($sret['error_code'] == 0) {
$uret = CenAuth::validate($sret['data']['st']);
$uret['data']['st'] = $sret['data']['st'];
if ($contactUserInfo = Login::authByPassId($uret['data']['pass_id'], $uret['data']['user_id'])) {
// Login::loginToSesstion($uret);
Login::loginToCookie($uret);
//发首次登录奖
if ($initConfig['firstLoginPrize'] && $redis->hSet('dfdh_first_login', $mobile, date('YmdHis'), $activityTotalTime)) {
$st_invited = Dfdh::getStByMobile($mobile);
$data = Dfdh::getLeftTime($st_invited);
if(($data['leftTime'] + $initConfig['firstLoginPrize']) <= $totalLeftTimeLimit){
Dfdh::addTime($mobile, $initConfig['firstLoginPrize'], $initConfig['firstLoginPrizeExpire']);
$award = $initConfig['firstLoginPrize'];
$extend2 = 'H5版首次登录获奖';
$firstLoginFlag = TRUE; $db->Execute("INSERT INTO `h5dfdh_lefttime_log`(`mobile`, `lefttime`, `channel`, `create_time`)VALUES(?, ?, ?, ?)", array($mobile, $initConfig['firstLoginPrize'], $initConfig['getLeftTimeChannel']['login'], date("Y-m-d H:i:s")));
} //邀请者加时长
if($_SESSION['inviter']){
$st_inviter = Dfdh::getStByMobile($_SESSION['inviter']);
$data = Dfdh::getLeftTime($st_inviter);
if(($data['leftTime'] + $initConfig['firstSharePrize']) <= $totalLeftTimeLimit){
$ret = Dfdh::addTime($_SESSION['inviter'], $initConfig['firstSharePrize'], $initConfig['firstSharePrizeExpire']);
if($ret){
$db->Execute("INSERT INTO `h5dfdh_lefttime_log`(`mobile`, `lefttime`, `channel`, `create_time`)VALUES(?, ?, ?, ?)", array($_SESSION['inviter'], $initConfig['firstSharePrize'], $initConfig['getLeftTimeChannel']['share'], date("Y-m-d H:i:s")));
//$redis->rPush($_SESSION['inviter'], $mobile, $activityTotalTime);
$db->Execute("insert into `h5dfdh_invit_log`(`inviter_mobile`, `invited_mobile`, `invit_time`)values(?, ?, ?)", array($_SESSION['inviter'], $mobile, date("Y-m-d H:i:s")));
//$logData 内容中的变量待替换为相应的..
$logData = $biString = $actId."|" . $user_ip . "|" . date('Y/m/d H:i:s') . "|" . $st_inviter . "|".$_SESSION['endpointId']."|" . $_SESSION['inviter'] . "|" . html_entity_decode($url) . "||" . $_SESSION['channel'] . "|" . $action . "|" . $initConfig['firstSharePrize'] . "|".$pageName."|" . $extend1 . "|" . 'H5分享获奖' . "|" . $msg . "|" . date('Ymd');
file_put_contents(BI_LOG_ROOT . 'bi_www_activity_' . date('Ymd') . '.log', $logData.PHP_EOL, FILE_APPEND);
}
}
}
}
$db->Execute("insert into `h5dfdh_login_log`(`mobile`, `ip`, `login_time`)values(?, ?, ?)", array($mobile, $user_ip, date("Y-m-d H:i:s"))); json_result(0, array('mobile' => $mobile, 'firstLogin' => $firstLoginFlag, 'firstLoginPrize' => $initConfig['firstLoginPrize']), 'ok');
} else {
json_result(3, array(), '登录失败');
}
} else {
json_result(1, array(), $sret['msg']);
}
PHP redis client封装的更多相关文章
- Redis 详解 (一) StackExchange.Redis Client
这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...
- StackExchange.Redis Client
StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓 ...
- 咏南中间件开始支持redis client接口调用
咏南中间件开始支持redis client接口调用 咏南中间件封装了redis client接口,可以支持REDIS了. 如下图,将数据集写入REDIS缓存,和从REDIS缓存获取数据: proced ...
- 【轮子狂魔】手把手教你自造Redis Client
为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居 ...
- 教你写个简单到的 Redis Client 框架 - .NET Core
目录 1,关于 Redis RESP 定义数据类型 2,定义异步消息状态机 3,定义命令发送模板 4,定义 Redis Client 5,实现简单的 RESP 解析 6,实现命令发送客户端 7,如何使 ...
- 深入浅出 Redis client/server交互流程
综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...
- redis client protocol 分解
在官方网站http://redis.io/topics/protocol我们必须redis通信协议做说明. 根据以下某些原因.我想解决redis client protocol: 1.足够了解通信协议 ...
- Redis client Python usage
http://www.yiibai.com/redis/redis_sorted_sets.html mport redis r_server = redis.Redis('localhost') # ...
- Redis+Jedis封装工具类
package com.liying.monkey.core.util; import java.io.IOException; import java.util.ArrayList; import ...
随机推荐
- phpstorm、webstorm配置less编译器
1. node.js 安装包 https://nodejs.org/en/download/ 1) 安装js解析器node.js.直接下一步就ok了. 2) 将npm压缩包解压,找到里面的les ...
- awk结合正则匹配
利用awk分析data.csv中label列各取值的分布. 在终端执行head data.csv查看数据: name,business,label,label_name 沧州光松房屋拆迁有限公司,旧房 ...
- PHP 时间戳 转时间 for ios 8*3600
// 时间戳 转时间 int intTimeString = [_model.date intValue]; NSDate *confromTimesp = [NSDate dateWithTimeI ...
- 初用vue遇到的一些问题
1.过滤器: filters: { search(list) { es5 var _self = this; //return list.filter(menu => menu.childs.n ...
- TortoiseGit使用入门
TortoiseGit使用入门 本地使用Git 首先要确定TortoiseGit已找到msysgit,如果先安装msysgit 再装TortoiseGit, 一般TortoiseGit 就会自动的识别 ...
- HDU - 4336:Card Collector(min-max容斥求期望)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- BZOJ2821 作诗(Poetize) 【分块】
BZOJ2821 作诗(Poetize) Description 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好是作诗. 由于时间紧迫,SHY作完诗之后还要虐OI ...
- 解决 mklink 使用中的各种坑(硬链接,软链接/符号链接,目录链接)
通过 mklink 命令可以创建文件或文件夹的链接,而这种链接跟快捷方式是不一样的.然而我们还可能会遇到其使用过程中的一些坑,本文将整理这些坑并提供解决方法. 0x00 背景介绍:mklink m ...
- 给网站添加IPv6 DNS记录
一. 1) 使6box提供的DNS64服务,解析出自己网站对应的IPv6域名 Windows: Win+R打开运行,输入cmd,打开命令提示符,输入以下命令 Nslookup www.6box.cn ...
- LG3565 [POI2014]HOT-Hotels
题意 有一个树形结构,每条边的长度相同,任意两个节点可以相互到达.选3个点.两两距离相等.有多少种方案? 1≤n≤5 000 分析 参照小塘空明的题解. 很明显到一个点距离相等的三个点两两之间距离相等 ...