文档章节

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
成都
Windows下安装Redis服务及安装PHP的Redis扩展

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

申文哲
07/25
0
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扩展

首先 下载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
redis&memcached for windows 安装与使用

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

梦梦阁
10/11
0
0
redis配置文件详解及实现主从同步切换

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

JAYZ_HAO
2017/09/04
0
0

没有更多内容

加载失败,请刷新页面

加载更多

关于组件化的最初步

一个工程可能会有多个版本,有国际版、国内版、还有针对各种不同的渠道化的打包版本、这个属于我们日常经常见到的打包差异化版本需求。 而对于工程的开发,比如以前的公司,分成了有三大块业...

DannyCoder
28分钟前
0
0
Spring的Resttemplate发送带header的post请求

private HttpHeaders getJsonHeader() { HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); ......

qiang123
昨天
2
0
Spring Cloud Gateway 之 Only one connection receive subscriber allowed

都说Spring Cloud Gateway好,我也来试试,可是配置了总是报下面这个错误: java.lang.IllegalStateException: Only one connection receive subscriber allowed. 困扰了我几天的问题,原来...

ThinkGem
昨天
25
0
学习设计模式——观察者模式

1. 认识观察者模式 1. 定义:定义对象之间一种一对多的依赖关系,当一个对象状态发生变化时,依赖该对象的其他对象都会得到通知并进行相应的变化。 2. 组织结构: Subject:目标对象类,会被...

江左煤郎
昨天
2
0
emoji

前言:随着iOS系统版本的升级,对原生emoji表情的支持也越来越丰富。emoji表情是unicode码中为表情符号设计的一组编码,当然,还有独立于unicode的另一套编码SBUnicode,在OS系统中,这两种编...

HeroHY
昨天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部