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封装的更多相关文章

  1. Redis 详解 (一) StackExchange.Redis Client

    这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...

  2. StackExchange.Redis Client

    StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓 ...

  3. 咏南中间件开始支持redis client接口调用

    咏南中间件开始支持redis client接口调用 咏南中间件封装了redis client接口,可以支持REDIS了. 如下图,将数据集写入REDIS缓存,和从REDIS缓存获取数据: proced ...

  4. 【轮子狂魔】手把手教你自造Redis Client

    为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居 ...

  5. 教你写个简单到的 Redis Client 框架 - .NET Core

    目录 1,关于 Redis RESP 定义数据类型 2,定义异步消息状态机 3,定义命令发送模板 4,定义 Redis Client 5,实现简单的 RESP 解析 6,实现命令发送客户端 7,如何使 ...

  6. 深入浅出 Redis client/server交互流程

    综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...

  7. redis client protocol 分解

    在官方网站http://redis.io/topics/protocol我们必须redis通信协议做说明. 根据以下某些原因.我想解决redis client protocol: 1.足够了解通信协议 ...

  8. Redis client Python usage

    http://www.yiibai.com/redis/redis_sorted_sets.html mport redis r_server = redis.Redis('localhost') # ...

  9. Redis+Jedis封装工具类

    package com.liying.monkey.core.util; import java.io.IOException; import java.util.ArrayList; import ...

随机推荐

  1. Java 7 新特性try-with-resources语句

    1.什么是try-with-resources语句 try-with-resources 语句是一个声明一个或多个资源的 try 语句.一个资源作为一个对象,必须在程序结束之后随之关闭. try-wi ...

  2. python常用模块之sys模块

    python常用模块之sys模块 1.sys.argv[]:命令行参数List,第一个元素是程序本身 # 写一个简单的python程序,代码如下: #!/usr/bin/python #coding= ...

  3. 【pandas】生日转年龄

    数据挖掘比赛中,获得的数据中可能有个人的生日,在数据分析中并不需要生日,而是需要年龄.不同年龄会呈现不同的状态,比如收入.健康.居住条件等,年龄能够很好的把不同样本的差异性进行大范围的划分.下面讲述如 ...

  4. Could not load the "light_rain.png" image referenced from a nib in the bundle with identifier

    导入图片文件夹的时候勾选create groups

  5. 类中的迭代器__iter__

    什么是迭代器 有iter()函数返回,可以通过next(it)函数取值的对象就是迭代器 迭代器协议 迭代器协议是指对象能够使用next函数获取下一项数据,在没有下一项数据时触发一个StopIterat ...

  6. Loj 2028 随机序列

    Loj 2028 随机序列 连续的乘号会将序列分成若干个块,块与块之间用加减号连接: \[ (a_1*a_2*...a_i)\pm(a_{i+1}*a_{i+2}*...a_j)\pm... \] 除 ...

  7. 通过Excel生成批量SQL语句,处理大量数据的好办法

    我们经常会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Insert,有的是Update等等,少量的数据我们可以采取最原始的办法,也就是在SQL里面用Insert into ...

  8. [Luogu4715]「英语」Z 语言

    luogu description 你有一个长度为\(n\)的串\(A\)和一个长度为\(m\)的串\(B\),字符集大小\(2^{31}\),且同一个串中没有相同的元素. 定义\(B\)串与\(A_ ...

  9. 【DUBBO】 Dubbo内核实现之动态编译

    转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577483 我们运行的java代码,一般都是编译之后的字节码.Dubbo为了实 ...

  10. LG2590 [ZJOI2008]树的统计

    题意 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: ...