phpredis
安装php的redis扩展:
http://pecl.php.net/package/redis
也可以用PHP直接连redis:
http://www.cnblogs.com/kudosharry/articles/2534694.html
<?php
/*******************************************************************************
* Redis PHP Bindings - http://code.google.com/p/redis/
*
* Copyright 2009 Ludovico Magnocavallo
* Released under the same license as Redis.
*
* Version: 0.1
*
* $Revision$
* $Date$
*
******************************************************************************/ class Redis { var $server;
var $port;
var $_sock; function Redis($host, $port=6379) {
$this->host = $host;
$this->port = $port;
} function connect() {
if ($this->_sock)
return;
if ($sock = fsockopen($this->host, $this->port, $errno, $errstr)) {
$this->_sock = $sock;
return;
}
$msg = "Cannot open socket to {$this->host}:{$this->port}";
if ($errno || $errmsg)
$msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
trigger_error("$msg.", E_USER_ERROR);
} function disconnect() {
if ($this->_sock)
@fclose($this->_sock);
$this->_sock = null;
} function &ping() {
$this->connect();
$this->_write("PING\r\n");
return $this->_simple_response();
} function &do_echo($s) {
$this->connect();
$this->_write("ECHO " . strlen($s) . "\r\n$s\r\n");
return $this->_get_value();
} function &set($name, $value, $preserve=false) {
$this->connect();
$this->_write(
($preserve ? 'SETNX' : 'SET') .
" $name " . strlen($value) . "\r\n$value\r\n"
);
return $preserve ? $this->_numeric_response() : $this->_simple_response();
} function &get($name) {
$this->connect();
$this->_write("GET $name\r\n");
return $this->_get_value();
} function &incr($name, $amount=1) {
$this->connect();
if ($amount == 1)
$this->_write("INCR $name\r\n");
else
$this->_write("INCRBY $name $amount\r\n");
return $this->_numeric_response();
} function &decr($name, $amount=1) {
$this->connect();
if ($amount == 1)
$this->_write("DECR $name\r\n");
else
$this->_write("DECRBY $name $amount\r\n");
return $this->_numeric_response();
} function &exists($name) {
$this->connect();
$this->_write("EXISTS $name\r\n");
return $this->_numeric_response();
} function &delete($name) {
$this->connect();
$this->_write("DEL $name\r\n");
return $this->_numeric_response();
} function &keys($pattern) {
$this->connect();
$this->_write("KEYS $pattern\r\n");
return explode(' ', $this->_get_value());
} function &randomkey() {
$this->connect();
$this->_write("RANDOMKEY\r\n");
$s =& trim($this->_read());
$this->_check_for_error($s);
return $s;
} function &rename($src, $dst, $preserve=False) {
$this->connect();
if ($preserve) {
$this->_write("RENAMENX $src $dst\r\n");
return $this->_numeric_response();
}
$this->_write("RENAME $src $dst\r\n");
return trim($this->_simple_response());
} function &push($name, $value, $tail=true) {
// default is to append the element to the list
$this->connect();
$this->_write(
($tail ? 'RPUSH' : 'LPUSH') .
" $name " . strlen($value) . "\r\n$value\r\n"
);
return $this->_simple_response();
} function <rim($name, $start, $end) {
$this->connect();
$this->_write("LTRIM $name $start $end\r\n");
return $this->_simple_response();
} function &lindex($name, $index) {
$this->connect();
$this->_write("LINDEX $name $index\r\n");
return $this->_get_value();
} function &pop($name, $tail=true) {
$this->connect();
$this->_write(
($tail ? 'RPOP' : 'LPOP') .
" $name\r\n"
);
return $this->_get_value();
} function &llen($name) {
$this->connect();
$this->_write("LLEN $name\r\n");
return $this->_numeric_response();
} function &lrange($name, $start, $end) {
$this->connect();
$this->_write("LRANGE $name $start $end\r\n");
return $this->_get_multi();
} function &sort($name, $query=false) {
$this->connect();
if ($query === false) {
$this->_write("SORT $name\r\n");
} else {
$this->_write("SORT $name $query\r\n");
}
return $this->_get_multi();
} function &lset($name, $value, $index) {
$this->connect();
$this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
return $this->_simple_response();
} function &sadd($name, $value) {
$this->connect();
$this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
return $this->_numeric_response();
} function &srem($name, $value) {
$this->connect();
$this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
return $this->_numeric_response();
} function &sismember($name, $value) {
$this->connect();
$this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
return $this->_numeric_response();
} function &sinter($sets) {
$this->connect();
$this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
return $this->_get_multi();
} function &smembers($name) {
$this->connect();
$this->_write("SMEMBERS $name\r\n");
return $this->_get_multi();
} function &scard($name) {
$this->connect();
$this->_write("SCARD $name\r\n");
return $this->_numeric_response();
} function &select_db($name) {
$this->connect();
$this->_write("SELECT $name\r\n");
return $this->_simple_response();
} function &move($name, $db) {
$this->connect();
$this->_write("MOVE $name $db\r\n");
return $this->_numeric_response();
} function &save($background=false) {
$this->connect();
$this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
return $this->_simple_response();
} function &lastsave() {
$this->connect();
$this->_write("LASTSAVE\r\n");
return $this->_numeric_response();
} function &_write($s) {
while ($s) {
$i = fwrite($this->_sock, $s);
if ($i == 0)
break;
$s = substr($s, $i);
}
} function &_read($len=1024) {
if ($s = fgets($this->_sock))
return $s;
$this->disconnect();
trigger_error("Cannot read from socket.", E_USER_ERROR);
} function _check_for_error(&$s) {
if (!$s || $s[0] != '-')
return;
if (substr($s, 0, 4) == '-ERR')
trigger_error("Redis error: " . trim(substr($s, 4)), E_USER_ERROR);
trigger_error("Redis error: " . substr(trim($this->_read()), 5), E_USER_ERROR);
} function &_simple_response() {
$s =& trim($this->_read());
if ($s[0] == '+')
return substr($s, 1);
if ($err =& $this->_check_for_error($s))
return $err;
trigger_error("Cannot parse first line '$s' for a simple response", E_USER_ERROR);
} function &_numeric_response($allow_negative=True) {
$s =& trim($this->_read());
$i = (int)$s;
if ($i . '' == $s) {
if (!$allow_negative && $i < 0)
$this->_check_for_error($s);
return $i;
}
if ($s == 'nil')
return null;
trigger_error("Cannot parse '$s' as numeric response.");
} function &_get_value() {
$s =& trim($this->_read());
if ($s == 'nil')
return '';
else if ($s[0] == '-')
$this->_check_for_error($s);
$i = (int)$s;
if ($i . '' != $s)
trigger_error("Cannot parse '$s' as data length.");
$buffer = '';
while ($i > 0) {
$s = $this->_read();
$l = strlen($s);
$i -= $l;
if ($l > $i) // ending crlf
$s = rtrim($s);
$buffer .= $s;
}
if ($i == 0) // let's restore the trailing crlf
$buffer .= $this->_read();
return $buffer;
} function &_get_multi() {
$results = array();
$num =& $this->_numeric_response(false);
if ($num === false)
return $results;
while ($num) {
$results[] =& $this->_get_value();
$num -= 1;
}
return $results;
} } ?>
常见问题:
http://www.taocms.org/392.html
http://segmentfault.com/q/1010000000095030
phpredis的更多相关文章
- linux下安装Redis以及phpredis模块
一:redis的安装 1. 首先上官网下载Redis 压缩包,地址:http://redis.io/download 下载 2. 通过远程管理工具,将压缩包拷贝到Linux服务器中,执行解压操作 3. ...
- Mac Pro 编译安装 Redis 的 PHP 客户端 phpredis
1.去官网下载 redis 扩展源码包 https://github.com/phpredis/phpredis 2.安装 redis 扩展 /usr/local/src/mac-sdk/source ...
- phpRedis安装、配置及简单使用
安装phpRedis前,请先安装Redis,再安装phpRedis插件. 1.下载安装 在linux服务器上,命令行执行以下命令(cd ./usr local/src 一般源码放在这里(推荐源码安装) ...
- phpredis中文文档 [转]
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...
- php-redis扩展安装
1 phpredis 在php中访问redis需要安装 https://github.com/phpredis/phpredis 基本上安装上面的readme既可以完成安装,需要注意的是在编译安装的时 ...
- PHP-redis中文文档
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/wi ...
- [Linux]安装phpredis扩展
1.下载phpredis,解压并进入目录,在目录下运行phpize /usr/local/php/bin/phpize ./configure --enable-redis-igbinary --wi ...
- 通过PHP扩展phpredis操作redis
我们使用phpredis,这个扩展能让你用PHP操作redis. 源码下载: phpize ./configure ); var_dump($result); echo $redis->get( ...
- 安装 phpredis 扩展
/************************************************//********************* phpredis ***************** ...
- mac 安装phpredis扩展
curl -O https://nodeload.github.com/nicolasff/phpredis/zip/master tar -zxf master cd phpredis-master ...
随机推荐
- lock语句的递归问题
原文地址 前几天在网上闲逛,无意中看到有这么一道题及其答案,如下: 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由. ) { i--; tes ...
- javaSE-基础部分整理
JavaSE基础部分整理 1.java介绍 1.Java分为三个部分: javaSE,javaEE,javaME; java重要性之一:跨平台(操作系统). 跨平台:一次编译,到处运行. Java虚拟 ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- javascript默认中文(汉字/标点)长度均为1的解决
javascript默认中文(汉字/标点)长度均为1 与后台(java)不一致, function calculate(str) { //var str="你好,哈哈哈000111lll&q ...
- careercup-排序和查找 11.3
11.3 给定一个排序后的数组,包含n个整数,但这个数组已被旋转很多次,次数不详.请编写代码找出数组中的某个元素.可以假定数组元素原先是按从小到大的顺序排序的. 解法: 可以直接从开始一个一个比较,也 ...
- 【转】windows环境下利用doxygen生成代码文档
作者:jiangwenna http://www.jiangwenna.com/windows-doxygen-doc/ Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统 ...
- 【十分钟教会你汇编】MIPS编程入门(妈妈说标题要高大上,才会有人看>_<!)
无意中找到一篇十分好用,而且篇幅也不是很大的入门教程,通篇阅后,再把“栗子”敲一遍,基本可以有一个比较理性的认识,从而方便更好地进一步深入学习. 废话不多说,上干货(英语好的直接跳过本人的渣翻译了哈— ...
- iOS/object-c: 枚举类型 enum,NS_ENUM,NS_OPTIONS
一般情况下,我们采用C风格的enum关键字可以定义枚举类型. enum{ UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFro ...
- Android开发--UI之Bundle的使用
Android开发–UI之Bundle的使用 最近,把之前学过的东西大体的整理了以下,并且想把学过的心得分享给大家.我自己做了一个小小的demo,以便说明具体的应用. 这里的两个界面是通过第一个界面输 ...
- java_一对一自由聊天
客户端 package cn.qianfeng.kaoti03; import java.io.BufferedReader; import java.io.IOException; import j ...