文档章节

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

adbug
 adbug
发布于 2015/04/30 10:25
字数 1102
阅读 25
收藏 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
成都
Windows下安装Redis服务及安装PHP的Redis扩展

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

申文哲
07/25
0
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
laravel中redis的使用

杂项 laravel采用predis进行管理redis 使用composer安装predis 或者在laravel下自带的composer.json中添加 Non-static method Redis::set() cannot be called statically, assuming $this f......

eatnothing
2016/05/18
1K
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
LAMP+Redis缓存数据库整合

LAMP在企业生产环境中,除了将MYSQL单独部署在其他服务器、由于MYSQL数据库压力会很大,还会对MYSQL实现主从复制及读写分离,同时会对PHP网站进行调优,通常PHP的优化手段包括:PHP代码本身优...

小凡6
2017/11/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

docker中安装了RabbitMQ后无法访问其Web管理页面

在官网找了"$ docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management"这条安装命令,在docker上安装了RabbitMQ,,结果输入http://localhost:8080并不......

钟然千落
23分钟前
0
0
spring-cloud | 分布式session共享

写在前面的话 各位小伙伴,你们有福了,这一节不仅教大家怎么实现分布式session的问题,还用kotlin开发,喜欢kotlin的小伙伴是不是很开心! 以前在写Android的时候,就对客户端请求有一定的认...

冯文议
42分钟前
0
0
c语言之内存分配笔记

先看一个数组: short array[5] = {1,2} // 这儿定义的一个int类型的数组,数组第1和第2个元素值是1和2.其余后面默认会给值为0; 或者 short array[] = {1,2};//这儿数组第1和第2个元素,数组...

DannyCoder
今天
4
0
Shell | linux安装包不用选择Y/N的方法

apt-get install -y packageOR echo "y" | sudo apt-get install package

云迹
今天
2
0
Hadoop的大数据生态圈

基于Hadoop的大数据的产品圈 大数据产品的一句话概括 Apache Hadoop: 是Apache开源组织的一个分布式计算开源框架,提供了一个分布式文件系统子项目(HDFS)和支持MapReduce分布式计算的软件架...

zimingforever
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部