phpphp rediss pphp rediss哪个好

Lumen中使用速度更快的PhpRedis扩展 - 推酷
Lumen中使用速度更快的PhpRedis扩展
欢迎关注我的博客
Lumen的确是一款适合做API,速度很快的框架。但是在项目中使用Redis时发现Lumen默认使用的
predis/predis
会拖慢整体速度,特别是在高并发的情况下,所以寻思着使用
代替,毕竟
是C语言写的模块,性能上肯定优于
文中例子已经整理成一个
包,文末有简单介绍。
[TargetLiu/PHPRedis]
编译安装PhpRedis
是C语言写的模块,需要编译安装。安装方法网上一搜一大把,请根据自己的环境选择相应的方法安装即可。
两个可能用得上的链接:
Lumen中使用PhpRedis
很简单,只需要在
bootstrap/app.php
中添加下列代码将PhpRedis注入容器即可:
$app-&singleton('phpredis', function(){
$redis = new R
$redis-&pconnect('127.0.0.1'); //建立连接
$redis-&select(1); //选择库
$redis-&auth('xxxx'); //认证
unset($app-&availableBindings['redis']);
绑定后即可通过
app('phpredis')
了,具体使用方法可以看相应的官方文档。
Lumen中为PhpRedis增加Cache驱动
由于实际使用中更多的将Redis用于缓存,Lumen自带的Redis缓存驱动是基于
predis/predis
实现,我们现在新建一个驱动以支持
新增Cache驱动详细方法可以查看
,这里指罗列一些关键的点。更多的内容也可以查看
我们首先创建一个
ServiceProvider
namespace App\P
use Illuminate\Support\Facades\C
use Illuminate\Support\ServiceP
use TargetLiu\PHPRedis\PHPRedisS
class CacheServiceProvider extends ServiceProvider
* Perform post-registration booting of services.
* @return void
public function boot()
Cache::extend('phpredis', function ($app) {
return Cache::repository(new PHPRedisStore($app-&make('phpredis'), $app-&config['cache.prefix']));
* Register bindings in the container.
* @return void
public function register()
这样就建立一个名为
的驱动。再创建一个基于
Illuminate\Contracts\Cache\Store
契约的缓存操作类用以操作
namespace TargetLiu\PHPR
use Illuminate\Contracts\Cache\S
class PHPRedisStore implements Store
* The Redis database connection.
* @var \Illuminate\Redis\Database
protected $
* A string that should be prepended to keys.
* @var string
protected $
* Create a new Redis store.
\Illuminate\Redis\Database
* @return void
public function __construct($redis, $prefix = '')
$this-&redis = $
$this-&setPrefix($prefix);
* Retrieve an item from the cache by key.
string|array
* @return mixed
public function get($key)
if (!is_null($value = $this-&connection()-&get($this-&prefix . $key))) {
return is_numeric($value) ? $value : unserialize($value);
* Retrieve multiple items from the cache by key.
* Items not found in the cache will have a null value.
* @return array
public function many(array $keys)
$return = [];
$prefixedKeys = array_map(function ($key) {
return $this-&prefix . $
}, $keys);
$values = $this-&connection()-&mGet($prefixedKeys);
foreach ($values as $index =& $value) {
$return[$keys[$index]] = is_numeric($value) ? $value : unserialize($value);
* Store an item in the cache for a given number of minutes.
* @return void
public function put($key, $value, $minutes)
$value = is_numeric($value) ? $value : serialize($value);
$this-&connection()-&set($this-&prefix . $key, $value, (int) max(1, $minutes * 60));
* Store multiple items in the cache for a given number of minutes.
* @return void
public function putMany(array $values, $minutes)
foreach ($values as $key =& $value) {
$this-&put($key, $value, $minutes);
* Increment the value of an item in the cache.
* @return int|bool
public function increment($key, $value = 1)
return $this-&connection()-&incrBy($this-&prefix . $key, $value);
* Decrement the value of an item in the cache.
* @return int|bool
public function decrement($key, $value = 1)
return $this-&connection()-&decrBy($this-&prefix . $key, $value);
* Store an item in the cache indefinitely.
* @return void
public function forever($key, $value)
$value = is_numeric($value) ? $value : serialize($value);
$this-&connection()-&set($this-&prefix . $key, $value);
* Remove an item from the cache.
* @return bool
public function forget($key)
return (bool) $this-&connection()-&delete($this-&prefix . $key);
* Remove all items from the cache.
* @return void
public function flush()
$this-&connection()-&flushDb();
* Get the Redis connection instance.
* @return \Predis\ClientInterface
public function connection()
return $this-&
* Get the Redis database instance.
* @return \Illuminate\Redis\Database
public function getRedis()
return $this-&
* Get the cache key prefix.
* @return string
public function getPrefix()
return $this-&
* Set the cache key prefix.
* @return void
public function setPrefix($prefix)
$this-&prefix = !empty($prefix) ? $prefix . ':' : '';
通过以上两个步骤基本上就完成了Cache驱动的创建,现在只需要在
bootstrap/app.php
中注入新建的Cache驱动然后配置
CACHE_DRIVER = phpredis
,最后再在
config/cache.php
中加入相应的驱动代码即可
'phpredis' =& [
'driver' =& 'phpredis'
Cache的使用请查看Lumen
一个基于PhpRedis的Lumen扩展包
[TargetLiu/PHPRedis]
composer require targetliu/phpredis
安装及使用方法请看
这个包只是我做的一个简单示例,引入了
并做了最简单的缓存驱动。目前支持根据
获取Redis配置、Cache的基本读写等。
Session和Queue可以继续使用Lumen自带的Redis驱动,两者互不影响。下一步如有需要可以继续完善这两部分的驱动。
这个包将根据自己工作需求以及大家的已经进一步完善。
欢迎大家提出意见共同完善。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致php - predis with phpiredis clustering - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I'm trying to use redis clusters with predis + phpiredis as I saw that phpiredis improves the performance.
This is my PHP code:
$cluster = [
'tcp://127.0.0.1:30001',
'tcp://127.0.0.1:30002',
'tcp://127.0.0.1:30003',
'tcp://127.0.0.1:30004',
'tcp://127.0.0.1:30005',
'tcp://127.0.0.1:30006',
$client = new Predis\Client($cluster,
'connections' =& [
=& 'Predis\Connection\PhpiredisStreamConnection'
I get the following error:
Fatal error: Uncaught exception 'Predis\Response\ServerException' with message 'MOVED .0.1:30002' in /var/www/html/predis-1.0/src/Client.php:365 Stack trace: #0 /var/www/html/predis-1.0/src/Client.php(330): Predis\Client->onErrorResponse(Object(Predis\Command\StringGet), Object(Predis\Response\Error)) #1 /var/www/html/predis-1.0/src/Client.php(310): Predis\Client->executeCommand(Object(Predis\Command\StringGet)) #2 /var/www/html/redisTests/predis.php(52): Predis\Client->__call('get', Array) #3 /var/www/html/redisTests/predis.php(52): Predis\Client->get('test') #4 {main} thrown in /var/www/html/predis-1.0/src/Client.php on line 365
Now if i set only the first 2 clusters ips, it works fine. Can someone please explain how come?
Can someone tell me whats wrong? is predis + phpiredis fully support clustering?
Thanks in advance for your help!
Know someone who can answer?
Share a link to this
via , , , or .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Browse other questions tagged
Stack Overflow works best with JavaScript enabled博客访问: 2842407
博文数量: 306
博客积分: 8003
博客等级: 大校
技术积分: 4187
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: 系统运维
1.下载:php_redis.dll:2.下载后由于里面有两个模块分别是vc6,vc9编译的,我们需要知道我们的Php是vc6还是vc9的:新建一个reids.php<?phpphpinfo();?>3.安装模块把php_redis.dll模块放到php安装目录下/ext/中在php.ini里的;extension=php_bz2.dll之前增加extension=php_redis.dll4.测试重启apache之后,访问redis.php<?phpphpinfo();$redis = new Redis();$redis->connect("192.168.60.6","6379");$redis->set("test","Hello World");echo $redis->get("test");?>测试结果
阅读(15139) | 评论(3) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
wang1352083: 请问楼主,我按照楼主的url下载了php_redis.dll,但是重启apache,报错:
PHP Warning:&&PHP Startup: Unable to load dynamic library 'D:\webserver\phpext\php_r.....你系统是32位还是64位?要不试试里面的另一个vc6版本吧
wang1352083: 请问楼主,我按照楼主的url下载了php_redis.dll,但是重启apache,报错:
PHP Warning:&&PHP Startup: Unable to load dynamic library 'D:\webserver\phpext\php_r.....请确定一下dll是32位还是64位,和你的系统是否一致
请问楼主,我按照楼主的url下载了php_redis.dll,但是重启apache,报错:
PHP Warning:&&PHP Startup: Unable to load dynamic library 'D:\webserver\phpext\php_redis.dll' - 找不到指定的模块。
而我的mongodb的dll放在相同的目录下就没问题.我用的zendserver5.3.9
请登录后评论。redis安装配置教程及phpredis扩展安装测试
(window.slotbydup=window.slotbydup || []).push({
id: '2611110',
container: s,
size: '240,200',
display: 'inlay-fix'
您当前位置: &
[ 所属分类
作者:zhanhailiang 日期:推荐阅读:Redis持久化策略
关于Redis更多资料阅读1. 下载redis-2.8.17.tar.gz:http://download.redis.io/releases/redis-2.8.17.tar.gz;2. 编译安装配置如下:[redis-2.8.17]# make[redis-2.8.17]# make PREFIX=/usr/local/redis-2.8.17 install[redis-2.8.17]# ln -s /usr/local/redis-2.8.17/bin/redis-benchmark /usr/bin/redis-benchmark[redis-2.8.17]# ln -s /usr/local/redis-2.8.17/bin/redis-check-aof /usr/bin/redis-check-aof[redis-2.8.17]# ln -s /usr/local/redis-2.8.17/bin/redis-check-dump /usr/bin/redis-check-dump[redis-2.8.17]# ln -s /usr/local/redis-2.8.17/bin/redis-cli /usr/bin/redis-cli[redis-2.8.17]# ln -s /usr/local/redis-2.8.17/bin/redis-server /usr/bin/redis-server [redis-2.8.17]# cd utils[utils]# ./install_server.sh Welcome to the redis service installerThis script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis-2.8.17/conf/redis_6379.confPlease select the redis log file name [/var/log/redis_6379.log] /usr/local/redis-2.8.17/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis-2.8.17/data/6379Please select the redis executable path [/usr/bin/redis-server] Selected config:Port
: 6379Config file
: /usr/local/redis-2.8.17/conf/redis_6379.confLog file
: /usr/local/redis-2.8.17/log/redis_6379.logData dir
: /usr/local/redis-2.8.17/data/6379Executable
: /usr/bin/redis-serverCli Executable : /usr/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.接着开启AOF模式:appendonly yes:############################## APPEND ONLY MODE ############################### # By default Redis asynchronously dumps the dataset on disk. This mode is# good enough in many applications, but an issue with the Redis process or# a power outage may result into a few minutes of writes lost (depending on# the configured save points).## The Append Only File is an alternative persistence mode that provides# much better durability. For instance using the default data fsync policy# (see later in the config file) Redis can lose just one second of writes in a# dramatic event like a server power outage, or a single write if something# wrong with the Redis process itself happens, but the operating system is# still running correctly.## AOF and RDB persistence can be enabled at the same time without problems.# If the AOF is enabled on startup Redis will load the AOF, that is the file# with the better durability guarantees.## Please check http://redis.io/topics/persistence for more information. appendonly yes
# The name of the append only file (default: "appendonly.aof") appendfilename "appendonly_6379.aof"通过redis服务命令重启下redis:[redis-2.8.17]# /etc/init.d/redis_6379 --helpPlease use start, stop, restart or status as first argument[redis-2.8.17]# /etc/init.d/redis_6379 restartPlease use start, stop, restart or status as first argument3. 下载igbinary扩展包(redis扩展包&#8211;enable-redis-igbinary依赖igbinary扩展包):http://pecl.php.net/get/igbinary-1.2.1.tgz[redis-2.8.17]# /usr/local/bin/phpize[redis-2.8.17]# ./configure --with-php-config=/usr/local/php/bin/php-config
--enable-igbinary[redis-2.8.17]# make && make install4. 下载redis扩展包:http://download.redis.io/releases/redis-2.8.17.tar.gz[redis-2.8.17]# /usr/local/php/bin/phpize[redis-2.8.17]# ./configure --with-php-config=/usr/local/php/bin/php-config
--enable-redis
--enable-redis-igbinary[redis-2.8.17]# make && make install5. 修改php.ini配置:; /usr/local/php/etc/php.iniextension=igbinary.soextension=redis.so6. 测试扩展是否正常加载:[redis-2.8.17]# /usr/local/php/bin/php -m[PHP Modules]...igbinary...redis... [Zend Modules]7. 测试代码如下:&?php$redis = new Redis();$redis-&connect('127.0.0.1', 6379);$count = $redis-&dbSize();echo "Redis has $count keys\n"; $ret = $redis-&get('test5');var_dump($ret);8. 安装配置Redis WEB管理工具phpRedisAdmin:/ErikDubbelboer/phpRedisAdmin[phpredisadmin]# git clone /ErikDubbelboer/phpRedisAdmin.git[phpredisadmin]# cd phpRedisAdmin[phpredisadmin]# git clone /nrk/predis.git vendor然后配置相应nginx配置即可看到当前Redis服务状态:至此redis环境配置就完成了。
本文数据库(综合)相关术语:系统安全软件
转载请注明本文标题:本站链接:
分享请点击:
1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
当你的才华还撑不起你的野心时,那你就应该静下心来学习。当你的经济还撑不起你的梦想时,那你就应该踏实的去工作!
手机客户端
,专注代码审计及安全周边编程,转载请注明出处:http://www.codesec.net
转载文章如有侵权,请邮件 admin[at]codesec.net安装redis shell& wget /files/redis-2.0.4.tar.gz shell& tar zxvf redis-2.0.4.tar.gz shell& mv redis-2.0.4 redis shell& cd redis shell& make shell& redis-server 不要关 shell& redis-cli redis&set foo bar OK redis&get foo “bar” 安装phpredis模块 /owlient/phpredis 下载phpredis 解压 shell& cd phpredis shell& /usr/local/php/bin/phpize 这个phpize是安装php模块的 shell& ./configure –with-php-config=/usr/local/php/bin/php-config shell& make shell& make install 接下来在php.ini中添加extension=redis.so 先要看看有没有extension_dir=/……. 重启apache或者nginx php代码测试 $redis = new Redis(); $redis-&connect(‘127.0.0.1′,6379); $redis-&set(‘test’,'hello world!’); echo $redis-&get(‘test’); ?& 输出hello world! /p/php-redis/ ================ # redis目前提供四种数据类型:string,list,set及zset(sorted set)。 # * string是最简单的类型,你可以理解成与Memcached一模一个的类型,一个key对应一个value,其上支持的操作与Memcached的操 作类似。但它的功能更丰富。 # * list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。 # * set是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合求交并差等操作。操作中key理解为集合的名字。 # * zset是set的一个升级版本,他在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解了有两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区}

我要回帖

更多关于 php redis 扩展 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信