文档章节

redis命令和php Redis命令 - Redis and PHP 对比

adbug
 adbug
发布于 2015/04/30 10:25
字数 1102
阅读 26
收藏 0

In my previous post (Awesomeness of Redis – Redis Installation and Configuration), I explained how to install and configure Redis server, a ultra fast Key-Value pair engine. As a follow up, this post (Awesomeness of Redis - redis and php) will focus on how to use PHP to connect to Redis server using Predis library.

Redis and PHP Connection:

First, you need to download the Predis library from the download page. Once downloaded, extract the contents and you can discard everything other than the Predis folder inside the lib directory. This is the core library files which you will be using from your PHP files to connect to Redis. Copy the directory Predis (which is inside the lib directory) in to the folder where your PHP script lies. Alternatively, you can copy the directory in

/usr/share/php/

for Ubuntu/Debian systems which is defined by default in php.ini as a place to place library files. By placing the directory in the above location, the Predis library will be accessible to all PHP scripts. Now that you have got the library files, It is time to load the library in your PHP code and connect to the Redis server.

The Predis comes with the auto-load function which will pretty much do everything and ease your work. Include the following line at the top of your PHP file.

require 'Predis/Autoloader.php';Predis\Autoloader::register();

These lines include and loads the file Autoloader.php inside the Predis directory which we copied in the last step. Now connect to your redis database from PHP using

$redis = new Predis\Client(array(    'scheme' => 'tcp',    'host'   => '10.0.0.1',    'port'   => 6379,));

Change the Parameter depending on the IP Address and the port in which your installed Redis. If you have set a passkey to Redis Instance, you need to use below snippet. Scheme is by default tcp.

$redis = new Predis\Client(array(   'host' => '10.0.0.1',   'port' =>  6379,   'password' => {PASS_KEY},    'database' => 0));

I have introduced a new parameter, database here. This lets you connect to the database you like, by default, it connects to 0. Now that you are connected, lets look into how to execute commands from PHP in your redis server. It is easy to add, modify, delete Keys from Redis server. I have compiled and listed out few basic and commonly used redis commands and its Predis equivalence below as I did not find the information to be readily available.

Edit
Redis Command PHP Predis Command Comment
SELECT index $redis->select(index) Change the selected database for the current connection
FLUSHDB $redis->flushdb(); Remove all keys from the current database
FLUSHALL $redis->flushall(); Remove all keys from all databases
SAVE $redis->save(); Synchronously save the dataset to disk
QUIT $redis->quit(); Close the connection
PING $redis->ping(); Ping the server
ECHO message $redis->do_echo('ECHO test'); Echo the given string
SET key value
$redis->set('aaa', 'bbb') Set the string value of a key
SETNX key value
$redis->set('aaa', 'ccc', true); Set the value of a key, only if the key does not exist
GET key $redis->get('aaa'); Get the value of a key
INCR key $redis->incr('aaa'); Increment the integer value of a key by one
INCRBY key increment $redis->incr('aaa', 2); Increment the integer value of a key by the given amount
DECR key $redis->decr('aaa'); Decrement the integer value of a key by one
DECRBY key decrement $redis->decr('aaa', 2); Decrement the integer value of a key by the given number
EXISTS key $redis->exists('aaa'); Determine if a key exists
DEL key [key ...] $redis->delete('aaa'); Delete a key
KEYS pattern print_r($redis->keys('*'), true); Find all keys matching the given pattern. In this case, all the keys
RANDOMKEY $redis->randomkey('a*') Return a random key from the keyspace matching the pattern.
RENAME key newkey $redis->rename('a1', 'a0'); Rename a key
RENAMENX key newkey $redis->rename('a0', 'a2', true); Rename a key, only if the new key does not exist
LPUSH key value [value ...]
$redis->push('a0', 'aaa'); Prepend one or multiple values to a list
RPUSH key value [value ...] $redis->push('a0', 'ccc', false); Append one or multiple values to a list
LLEN key $redis->llen('a0'); Get the length of a list
LRANGE key start stop $redis->lrange('sdkjhfskdjfh', 0, 100), true) Get a range of elements from a list
LTRIM key start stop $redis->ltrim('a0', 0, 1); Trim a list to the specified range
LINDEX key index $redis->lindex('a0', 0); Get an element from a list by its index
RPOP key $redis->pop('a0'); Remove and get the last element in a list
LPOP key $redis->pop('a0', false); Remove and get the first element in a list
LSET key index value $redis->lset('a0', 'ccc', 0); Set the value of an element in a list by its index
SADD key member [member ...] $redis->sadd('s0', 'aaa'); Add one or more members to a set
SREM key member [member ...] $redis->srem('s0', 'bbb'); Remove one or more members from a set
SISMEMBER key member $redis->sismember('s0', 'aaa'); Determine if a given value is a member of a set
SINTER key [key ...] $redis->sinter(array('s0', 's1')), true) Intersect multiple sets
SMEMBERS key $redis->smembers('s1'), true); Get all the members in a set
MOVE key db $redis->move('s1', 1); Move a key to another database
BGSAVE $redis->save(true); Asynchronously save the dataset to disk
LASTSAVE $redis->lastsave(); Get the UNIX time stamp of the last successful save to disk
HSET key field value $redis->hset('a', "field","value');
$redis->hset('a', "field2","value");
Set the string value of a hash field
HGET key field
$redis->hget('a','field') Get the value of a hash field
HGETALL key $redis->hgetall('a'); Get all the fields and values in a hash

The above table covers pretty much all the commands in Redis. For more information on Commands available in Redis visit the Command Reference.

This concludes our tutorial on How to Connect to Redis from PHP using client Predis. I would like to extent my thanks to Daniele Alessandri for his excellent work in developing Predis library.

Update: Table updated on May 28, 2012 with Predis Commands to work with Hashes Data Type of Redis.


          Arun Chinnachamy bio photo

Arun Chinnachamy

I am a chemical Engineer from BITS-Pilani. Right now, I work as Technology Lead at MySmartPrice. This is just a place where I write about the things I work and think.

Email Twitter Facebook Github

Awesomeness of Redis - Redis and PHP was published onMay 08, 2012.

© 著作权归作者所有

共有 人打赏支持
adbug
粉丝 6
博文 176
码字总数 198249
作品 0
成都
私信 提问
linux下安装redis扩展

首先 下载redis linux: http://download.redis.io/releases/redis-3.0.5.tar.gz windows: https://github.com/dmajkic/redis/downloads windos下 安装好redis后直接启动 启动后注意欢迎界面不......

bo-少
2015/12/08
62
0
Windows下安装Redis服务及安装PHP的Redis扩展

Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。 它通常被称为数据结构服务器,因为值(value)可以是...

申文哲
07/25
0
0
redis&memcached for windows 安装与使用

redis for windows 很简单只需要3步,主要是安装redis和安装php的redis扩展两个方面(安装玩之后应该进行一定配置 1.Windows安装包下载地址:https://github.com/ServiceStack/redis-windows...

梦梦阁
10/11
0
0
linux安装redis(基于wdlinux面板)

一、安装redis a、下载redis: wget http://download.redis.io/redis-stable.tar.gz tar -zxvf redis-stable.tar.gz cd redis-stable make make install b、在redis安装目录下进入utils目录,......

sky_l
01/26
5
0
redis配置文件详解及实现主从同步切换

redis配置文件详解及实现主从同步切换 redis复制 Redis复制很简单易用,它通过配置允许slave Redis Servers或者Master Servers的复制品。接下来有几个关于redis复制的非常重要特性: 一个Mas...

JAYZ_HAO
2017/09/04
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周二乱弹 —— 其实我在地板也睡不着

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @witt-z :分享歌词: 阴天 在不开灯的房间,当所有思绪都一点一点沉淀。 分享莫文蔚的单曲《阴天》: 《阴天》- 莫文蔚 手机党少年们想听歌,...

小小编辑
10分钟前
9
3
微服务分布式事务实现

https://www.processon.com/view/link/5b2144d7e4b001a14d3d2d30

WALK_MAN
今天
3
0
《大漠烟尘》读书笔记及读后感文章3700字

《大漠烟尘》读书笔记及读后感文章3700字: 在这个浮躁的社会里,你有多久没有好好读完一本书了? 我们总觉得自己和别人不一样,所以当看到别人身上的问题时,很少有“反求诸己”,反思自己。...

原创小博客
今天
4
0
大数据教程(9.5)用MR实现sql中的jion逻辑

上一篇博客讲解了使用jar -jar的方式来运行提交MR程序,以及通过修改YarnRunner的源码来实现MR的windows开发环境提交到集群的方式。本篇博主将分享sql中常见的join操作。 一、需求 订单数据表...

em_aaron
今天
3
0
十万个为什么之什么是resultful规范

起源 越来越多的人开始意识到,网站即软件,而且是一种新型的软件。这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency)、高并发等特点...

尾生
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部