php 远程调用redis
<?php
$redis_conf = array (
"active_code"=>array(
"host" => "14.29.64.112",
"port" => "8899",
"pass" => "funova2014"
)
);
#微信签名
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"]; $token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
#连接redis
function getSocket(){
include_once ("phpRedis.class.php");
$phpRedis = new phpRedis ();
$phpRedis->master = "active_code";
include ("redis.conf.php");
#授权
$phpRedis->auth ($redis_conf ["active_code"]['pass']);
return $phpRedis;
}
#获取激活码
function index($key){
#连接redis
$redis=$this->getSocket();
$redis->connect();
return $redis->lpop("$key");
#return $redis->hgetall('code_hash');
}
#把已经读取的激活码写入redis中
function set_code($code,$userid){
#连接redis
$redis=$this->getSocket();
$redis->connect();
return $redis->hset('code_hash_wx',$code,$userid);
#return $redis->hgetall('code_hash');
}
<?php
#class Php5RedisException extends Exception {}
class phpRedis
{
/*
* $port 端口 6379
* $host 主机 192.168.1.28
*/
// redisS务器的地址 private $md5key = "funova_aws_redis"; public $master="active_code"; static private $_sock = NULL;
// 保存数据库连接资源
public static $linkID = array ();
/**
* 链接redis服务器
*/
public function connect() {
error_reporting(E_ALL ^ E_NOTICE);
include("redis.conf.php");
$this->host = $redis_conf [$this->master]['host'];
$this->port = $redis_conf [$this->master]['port']; if (!is_null(self::$linkID[$this->master]))
return;
$sock = fsockopen ( $this->host, $this->port, $errno, $errstr );
if ($sock) {
self::$linkID [$this->master] = $sock;
$this->debug('Connected');
return;
}
$msg = "Cannot open socket to {$this->host}:{$this->port}";
if ($errno || $errmsg)
$msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
error_log ( "$msg." ,3,"/tmp/hivePhpLog/redis.log");
}
/**
* 错误提示
* @param unknown_type $msg
*/
private function debug($msg){
//echo sprintf("[php_redis] %s\n", $msg);
}
/**
* 对cmd接收到的函数进行处理
* 并且返回数据
*/
private function cmdResponse() {
// Read the response
$s = trim ( $this->read () );
switch ($s [0]) {
case '-' : // Error message
throw new Php5RedisException ( substr ( $s, 1 ) );
break;
case '+' : // Single line response
return substr ( $s, 1 );
case ':' : //Integer number
return substr ( $s, 1 ) + 0;
case '$' : //Bulk data response
$i = ( int ) (substr ( $s, 1 ));
if ($i == - 1)
return null;
$buffer = '';
if ($i == 0){
$s = $this->read ();
}
while ( $i > 0 ) {
$s = $this->read ();
$l = strlen ( $s );
$i -= $l;
if ($i < 0)
$s = substr ( $s, 0, $i );
$buffer .= $s;
}
return $buffer;
break;
case '*' : // Multi-bulk data (a list of values)
$i = ( int ) (substr ( $s, 1 ));
if ($i == - 1)
return null;
$res = array ();
for($c = 0; $c < $i; $c ++) {
$res [] = $this->cmdResponse ();
}
return $res;
break;
default :
error_log ( 'Unknown responce line: ' . $s,3,"redis.log");
break;
}
}
/**
* 接收命令并且返回结果集
* @param unknown_type $command
*/
private function cmd($command) {
$this->debug('Command: '.$command);
$this->connect ();
$s = $command . "\r\n";
while ( $s ) {
$i = fwrite ( self::$linkID [$this->master], $s );
if ($i == 0)
break;
$s = substr ( $s, $i );
}
return $this->cmdResponse ();
}
/**
* 读取指定长度的字符
* @param unknown_type $len
*/
private function read($len = 1024) {
if ($s = fgets ( self::$linkID [$this->master] )) {
$this->debug('Read: '.$s.' ('.strlen($s).' bytes)');
return $s;
}
$this->disconnect ();
error_log ( "Cannot read from socket.",3,"redis.log" );
}
/**
* 关闭redis链接
*/
private function disconnect() {
if (self::$linkID [$this->master])
@fclose ( self::$linkID [$this->master] );
self::$linkID [$this->master] = null;
}
/**
* L_C_48单一数据
* @param $key
* @param $value
*/
function set($key, $value) {
return $this->cmd ("SET $key $value");
}
/**
* 批量L_C_48数据
* @param $array
*/
function setArray($array) {
if(is_array($array)){
foreach($array as $key =>$val){
$this->cmd ("SET $key $val");
}
}
return;
}
/**
* 根据$key获取数据
* @param $key
*/
function get($key) {
return $this->cmd ("GET $key");
}
/**
* 返回匹配指定模式的所有key
* @param $pattern
*/
function keys($pattern) {
return $this->cmd ("KEYS $pattern");
}
/**
* 设定L_C_181
*/
function setExpire($key,$expire){
return $this->cmd ("expire $key $expire ");
}
/**
* 从队到左边入队一个元素
*/
function lpush($key,$value){
if ($this->master == "debug") { // 内部
return $this->cmd("lpush $key $value");
} else {
return $this->cmd("lpush $key '$value'");
}
}
/**
* 从队到左边出队一个元素
*/
function lpop($key){
return $this->cmd("lpop $key");
}
/**
* 验证密码
*/
function auth ( $pass ){
return $this->cmd("auth $pass");
}
/**
* 发送信息
*/
function publish($key,$content){
/**
* 当前是否在开发模式
*/
if ($this->master == "debug") { // 内部
return $this->cmd("publish $key $content");
} else {
return $this->cmd("publish $key '$content'");
}
}
/**
* setnx
*/
function setnx($key,$value){
return $this->setnx("SETNX $key $value");
}
/**
* L_C_26记录
*/
function delete($key){
return $this->cmd("DEL $key");
}
/**
* 监听频道
*/
function subscribe($key){
return $this->cmd("SUBSCRIBE $key");
}
/**
* 读取list文件
*/
function lrange($key){
return $this->cmd("LRANGE $key 0 -1");
}
/**
* 插入hash数组
*/
function hset($key,$filed, $value){
return $this->cmd("hset $key $filed $value");
}
/**
* 删除list
*/
function lrem($key,$value){
return $this->cmd("lrem $key -2 ".$value);
}
/**
*获取所有的值
*/
function hgetall($key){
return $this->cmd("hgetall $key");
}
/**
*判断key是否存在
*/
function exists_key($key){ return $this->cmd("EXISTS $key");
}
/**
*判断判断key中有没有当前这个域
*/
function exists_filed($key,$field){
return $this->cmd("hexists $key $field");
}
/**
*返回并删除链表尾元素
*/
function rpop($key){
return $this->cmd("rpop $Key");
}
/**
*
*/
function rpush($key,$value){
return $this->cmd("rpush $Key $value");
} }
?>
php 远程调用redis的更多相关文章
- Spring远程调用技术<2>-Hessian和Burlap
上篇谈到RMI技术,加上Spring的封装,用起来很方便,但也有一些限制 这里的Hessian和Burlap解决了上篇提到的限制,因为他们是基于http的轻量级远程服务. Hessian,和RMI一样 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- 架构设计:一种远程调用服务的设计构思(zookeeper的一种应用实践)
在深入学习zookeeper我想先给大家介绍一个和zookeeper相关的应用实例,我把这个实例命名为远程调用服务.通过对这种应用实例的描述,我们会对zookeeper应用场景会有深入的了解. 远程调 ...
- 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】
一.远程调用概述 1.远程调用的定义 在一个程序中就像调用本地中的方法一样调用另外一个远程程序中的方法,但是整个过程对本地完全透明,这就是远程调用.spring已经能够非常成熟的完成该项功能了. 2. ...
- 深入浅出Alljoyn——实例分析之远程调用(Method)篇
深入浅出就是很深入的学习了很久,还是只学了毛皮,呵呵! 服务端完整代码: #include <qcc/platform.h> #include <assert.h> #incl ...
- .Net组件程序设计之远程调用(二)
.Net组件程序设计之远程调用(二) 激活模式 引用封送对象激活类型两种, 一种是客户端激活类型,一种是服务器端激活. 客户端激活对象 客户端激活方式:当客户端创建一个远程对象时,客户端得到的是一个新 ...
- .Net组件程序设计之远程调用(一)
.Net组件程序设计之远程调用(一) 1应用程序域 我们知道我们写的C#代码是在操作系统逻辑体系结构中最上层的,然而操作系统本身是不会认识C#代码的,它只认识机器代码.那我们写的程序经过编译后是编译成 ...
- Spring远程调用技术<3>-Spring的HTTP Invoker
前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙. 另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...
- Spring远程调用技术<1>-RMI
在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI) 适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...
随机推荐
- 虚拟内存,MMU/TLB,PAGE,Cache之间关系
转:http://hi.baidu.com/gilbertjuly/item/6690ba0dfdf57adfdde5b040 虚拟地址VA到物理地址PA以页page为单位.通常page的大小为4K. ...
- OneThink框架的文章详情页分页
Application/Home/Controller/ArticleController.class.php的detail函数修改结果如下: /* 文档模型详情页 */public function ...
- Hive使用简介
---恢复内容开始--- 指定分隔符 HIVE输出到文件的分隔符 ,列与列之间是'\1'(ASCII码1,在vim里显示为^A),列内部随着层数增加,分隔符依次为'\2','\3','\4'等. 例: ...
- GDUT决赛题解
决赛,我自我认为题目难度更大,反而我的心态更好了. 由于放轻松的时候反而效果更好,跟昨天的观点一样,凡是可以1A的,才算这题做得好. A.数目不大,关键是看懂题(我自己连输入输出是什么都不清楚.... ...
- ARP协议具体解释之ARP动态与静态条目的生命周期
ARP协议详细解释之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自己主动加入和删除. q 每一个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉 ...
- 使用JMeter3.0实战之分布式并发测试以及web API接口测试
简介: 该文档是以Apche JMeter-3.0为例进行编写的,通过网上的学习资料和官方文档的说明手册学习后,进行项目操作实践,将测试的过程记录下提供给大家学习. 本博文的内容主要是进行配置JMet ...
- 在redhat下使用x11vnc进行桌面共享
1.在redhat上安装x11vnc时.你须要注意下面几个方面: (1)下载x11vnc的源代码包: 网址例如以下所看到的: http://sourceforge.net/projects/libvn ...
- 大巧不工web前端设计修炼之道—笔记
设计原则: 深入人心的设计--别让我思考 简洁是一种文化,一种需求,一种思想 ·排版 ·字体(衬线 | | 无衬线)
- iOS-仿支付宝加载web网页添加进度条
代码地址如下:http://www.demodashi.com/demo/11727.html 目前市场上APP常会嵌入不少的h5页面,参照支付宝显示web页面的方式, 做了一个导航栏下的加载进度条. ...
- Quartz.Net线程处理用到的两个Attribute
1.DisallowConcurrentExecution 加到IJob实现类上,主要防止相同JobDetail并发执行. 简单来说,现在有一个实现了IJob接口的CallJob,触发器设置的时间是每 ...