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. Ext JS 5 关于Store load返回json错误信息或异常的处理

    关于在store load的时候服务器返回错误信息或服务器出错的处理.ExtJS4应该也能用,没测试过. 这里有两种情况: 服务器返回错误json错误信息,状态为200 服务器异常,状态为500 一. ...

  2. Paths中的几个重要元素

    Paths中的几个重要元素 Points void CGContextMoveToPoint (    CGContextRef c,    CGFloat x,    CGFloat y ); 指定 ...

  3. 在VS2013创建WebService并在IIS中发布和使用

    创建WebService 第一步:打开VS2013,新建空项目,注意选择.NET Framework的版本.这里我选择的是.NET Framework 4 新建好项目后,在项目中添加一个WebServ ...

  4. 【DUBBO】dobbo的application的配置项

    Dubbo:application的配置项[一]:配置项 <dubbo:application name="服务名字" owner="拥有者" organ ...

  5. liunx系统环境下,爆出该错误"org.eclipse.wst.validation" has been removed解决办法

    导出maven工程遇到的问题,"org.eclipse.wst.validation" has been removed,还以为是工程本身的问题,后来发现是eclipse的问题. ...

  6. spring--注入方式

    1.正常方式: 在一个“value”标签注入值,并附有“property”标签结束. <beans xmlns="http://www.springframework.org/sche ...

  7. webpack extract-text-webpack-plugin

    备注:  提炼上面引用的css   1. 插件配置 const path = require("path"); const extracttextplugin = require( ...

  8. toxiproxy 安装试用

    备注:    实际上是一个代理工具,但是又不是简单的进行代理(tcp,可以配置策略,toxics 实现延迟,模拟故障,    对于这个大家可能了解的就是netflix 公司的chaos monkey, ...

  9. oracle11g卸载(win10)

    oracle11g卸载(win10) 0.已在win10安装oracle11g 1.开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. 2.运行Unive ...

  10. Opencv2.2 移植到am335x-y开发板

    1.虚拟机上运行cmake-gui,报找不到文件,指示安装. 2.下载opencv2.2.0 http://opencv.org/downloads.html 3.cmake-gui,配置参考< ...