Redis与phpredis的安装

  categories:资料  tags:  author:

来源:互联网

一、 准备条件

虚拟机、能上网

二、 安装redis

1、下载安装

wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz

tar -zxf redis-2.2.13.tar.gz

cd redis-2.2.13

make

sudo make install

这时Redis 的可执行文件被放到了/usr/local/bin

2、下载配置文件和init启动脚本:

wget https://github.com/ijonas/dotfiles/raw/master/etc/init.d/redis-server

wget https://github.com/ijonas/dotfiles/raw/master/etc/redis.conf

sudo mv redis-server /etc/init.d/redis-server

sudo chmod +x /etc/init.d/redis-server

sudo mv redis.conf /etc/redis.conf

3、初始化用户和日志路径

第一次启动Redis前,建议为Redis单独建立一个用户,并新建data和日志文件夹

sudo useradd redis

sudo mkdir -p /var/lib/redis

sudo mkdir -p /var/log/redis

sudo chown redis.redis /var/lib/redis

sudo chown redis.redis /var/log/redis

4、设置开机自动启动,关机自动关闭

sudo update-rc.d redis-server defaults

5、启动Redis:

sudo /etc/init.d/redis-server start

6、启动client客户端连接:

$ redis-cli

redis> set foo bar

OK

redis> get foo

“bar”

三、 安装phpredis

下载phpredis

sudo apt-get install git-core

git clone https://github.com/owlient/phpredis.git

cd phpredis

/usr/bin/phpize 使用phpize工具

./configure –with-php-config=/usr/bin/php-config

make

sudo make install

接下来在php.ini中添加extension=redis.so

四、 常见问题:

make命令找不到–sudo apt-get build-dep gcc

五、 常用命令:

Redis::__construct

描述: 创建一个Redis客户端

范例: $redis = new Redis();

connect, open

描述: 实例连接到一个Redis.

参数:host: string        port: int

返回值:BOOL 成功返回:TRUE;失败返回:FALSE

范例: $redis->connect(‘127.0.0.1′, 6379);

get

描述:

获取有关指定键的值

参数:key

返回值:string或BOLL 如果键不存在,则返回FALSE。否则,与此相关的关键值返回。

范例: $redis->get(‘key’);

set

描述: 设置关键值参数

参数:Key Value

返回值:BOOL

范例: $redis->set(‘key’, ‘value’)

setnx

描述: 如果在数据库中不存在该键,设置关键值参数

参数:key value

返回值:BOOL

范例: $this->redis->setnx(‘key’, ‘value’);

delete

描述: 删除指定的键

参数:一个键,或不确定数目的参数,每一个关键的数组:key1 key2 key3 … keyN

返回值:删除的项数

范例: $redis->set(‘key1′, ‘val1′);

$redis->set(‘key2′, ‘val2′);

$redis->set(‘key3′, ‘val3′);

$redis->set(‘key4′, ‘val4′);

$redis->delete(‘key1′, ‘key2′);

$redis->delete(array(‘key3′, ‘key4′));

exists

描述:验证指定的键是否存在

参数key

返回值:Bool

范例: $this->set(‘key’, ‘value’);

$this->exists(‘key’);

$this->exists(‘NonExistingKey’);

incr

描述: 数字递增存储键值键.如果第二个参数被填满,它将被用来作为整数值递增

参数:key value:将被添加到键的值

返回值:INT the new value

范例:  $redis->incr(‘key1′);

$redis->incr(‘key1′);

$redis->incr(‘key1′);

$redis->incr(‘key1′);

decr

描述:数字递减存储键值。如果第二个参数被填满,它将被用来作为整数值递减

参数:key value:将被添加到键的值

返回值:INT the new value

范例:  $redis->decr(‘key1′);

$redis->decr(‘key1′);

$redis->decr(‘key1′);

getMultiple

描述:取得所有指定键的值。如果一个或多个键不存在,该数组中该键的值为假

参数:其中包含键值的列表数组

返回值:返回包含所有键的值的数组

范例:  $redis->set(‘key1′, ‘value1′);

$redis->set(‘key2′, ‘value2′);

$redis->set(‘key3′, ‘value3′);

$redis->getMultiple(array(‘key1′, ‘key2′, ‘key3′));

rPush

描述:由列表头部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。

参数:key,value

返回值:LONG The new length of the list in case of success, FALSE in case of Failure.

范例:  $redis->delete(‘key1′);

$redis->lPush(‘key1′, ‘C’); // returns 1

$redis->lPush(‘key1′, ‘B’); // returns 2

$redis->lPush(‘key1′, ‘A’); // returns 3

lPop

描述:返回和移除列表的最后一个元素

参数:key

返回值:STRING if command executed successfully BOOL FALSE in case of failure (empty list)

范例:  $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->lPop(‘key1′);

rPop

描述:返回和移除列表的第一个元素

参数:key

返回值:STRING if command executed successfully BOOL FALSE in case of failure (empty list)

范例: $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->rPop(‘key1′);

lSize

描述:返回的列表的大小。如果列表不存在或为空,该命令返回0。如果该键不是列表,该命令返回FALSE。

参数:Key

返回值: LONG The size of the list identified by Key exists. BOOL FALSE if the data type identified by Key is not list

范例:  $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->lSize(‘key1′);

$redis->rPop(‘key1′);

$redis->lSize(‘key1′);

lGet

描述:返回指定键存储在列表中指定的元素。 0第一个元素,1第二个… -1最后一个元素,-2的倒数第二…错误的索引或键不指向列表则返回FALSE。

参数:key index

返回值: String the element at this index

Bool FALSE if the key identifies a non-string data type, or no value corresponds to this index in the list Key.

范例:  $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->lGet(‘key1′, 0);

$redis->lGet(‘key1′, -1);

$redis->lGet(‘key1′, 10);

lSet

描述:为列表指定的索引赋新的值,若不存在该索引返回false.

参数:key index value

返回值:BOOL TRUE if the new value is setted. FALSE if the index is out of range, or data type identified by key is not a list.

范例:  $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->lGet(‘key1′, 0);

$redis->lSet(‘key1′, 0, ‘X’);

$redis->lGet(‘key1′, 0);

lGetRange

描述: 返回在该区域中的指定键列表中开始到结束存储的指定元素,lGetRange(key, start, end)。0第一个元素,1第二个元素… -1最后一个元素,-2的倒数第二…

参数:key start end

返回值:Array containing the values in specified range.

范例:  $redis->rPush(‘key1′, ‘A’);

$redis->rPush(‘key1′, ‘B’);

$redis->rPush(‘key1′, ‘C’);

$redis->lGetRange(‘key1′, 0, -1);

lRemove

描述: 从列表中从头部开始移除count个匹配的值。如果count为零,所有匹配的元素都被删除。如果count是负数,内容从尾部开始删除。

参数:key count value

返回值: LONG the number of elements to remove BOOL FALSE if the value identified by key is not a list.

范例:  $redis->lPush(‘key1′, ‘A’);

$redis->lPush(‘key1′, ‘B’);

$redis->lPush(‘key1′, ‘C’);

$redis->lPush(‘key1′, ‘A’);

$redis->lPush(‘key1′, ‘A’);

$redis->lGetRange(‘key1′, 0, -1);

$redis->lRemove(‘key1′, ‘A’, 2);

$redis->lGetRange(‘key1′, 0, -1);

sAdd

描述: 为一个Key添加一个值。如果这个值已经在这个Key中,则返回FALSE。

参数:key value

返回值:BOOL TRUE if value didn’t exist and was added successfully, FALSE if the value is already present.

范例:  $redis->sAdd(‘key1′ , ’set1′);

$redis->sAdd(‘key1′ , ’set2′);

$redis->sAdd(‘key1′ , ’set2′);

sRemove

描述:删除Key中指定的value值

参数:key member

返回值:BOOL TRUE if the member was present in the set, FALSE if it didn’t.

范例:  $redis->sAdd(‘key1′ , ’set1′);

$redis->sAdd(‘key1′ , ’set2′);

$redis->sAdd(‘key1′ , ’set3′);

$redis->sRemove(‘key1′, ’set2′);

sMove

描述:将Key1中的value移动到Key2中

参数:srcKey dstKey member

返回值:BOOL If the operation is successful, return TRUE. If the srcKey and/or dstKey didn’t exist, and/or the member didn’t exist in srcKey, FALSE is returned.

范例:  $redis->sAdd(‘key1′ , ’set11′);

$redis->sAdd(‘key1′ , ’set12′);

$redis->sAdd(‘key1′ , ’set13′);

$redis->sAdd(‘key2′ , ’set21′);

$redis->sAdd(‘key2′ , ’set22′);

$redis->sMove(‘key1′, ‘key2′, ’set13′);

sContains

描述: 检查集合中是否存在指定的值。

参数:key value

返回值:BOOL TRUE if value is a member of the set at key key, FALSE otherwise.

范例:  $redis->sAdd(‘key1′ , ’set1′);

$redis->sAdd(‘key1′ , ’set2′);

$redis->sAdd(‘key1′ , ’set3′);

$redis->sContains(‘key1′, ’set1′);

$redis->sContains(‘key1′, ’setX’);

sSize

描述:返回集合中存储值的数量

参数:key

返回值:LONG the cardinality of the set identified by key, 0 if the set doesn’t exist.

范例:  $redis->sAdd(‘key1′ , ’set1′);

$redis->sAdd(‘key1′ , ’set2′);

$redis->sAdd(‘key1′ , ’set3′);

$redis->sSize(‘key1′);

$redis->sSize(‘keyX’);

sPop

描述:随机移除并返回key中的一个值

参数:key

返回值:String popped value Bool FALSE if set identified by key is empty or doesn’t exist.

范例:  $redis->sAdd(‘key1′ , ’set1′);

$redis->sAdd(‘key1′ , ’set2′);

$redis->sAdd(‘key1′ , ’set3′);

$redis->sPop(‘key1′);

$redis->sPop(‘key1′);

sInter

描述: 返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。

参数:key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.

返回值:Array, contain the result of the intersection between those keys. If the intersection beteen the different sets is empty, the return value will be empty array.

范例: $redis->sAdd(‘key1′, ‘val1′);

$redis->sAdd(‘key1′, ‘val2′);

$redis->sAdd(‘key1′, ‘val3′);

$redis->sAdd(‘key1′, ‘val4′);

$redis->sAdd(‘key2′, ‘val3′);

$redis->sAdd(‘key2′, ‘val4′);

$redis->sAdd(‘key3′, ‘val3′);

$redis->sAdd(‘key3′, ‘val4′);

var_dump($redis->sInter(‘key1′, ‘key2′, ‘key3′));

输出:

array(2) {[0]=>string(4) “val4″ [1]=>string(4) “val3″}

sInterStore

描述: 执行sInter命令并把结果储存到新建的变量中。

参数: Key: dstkey, the key to store the diff into.

Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.

返回值:INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

范例:  $redis->sAdd(‘key1′, ‘val1′);

$redis->sAdd(‘key1′, ‘val2′);

$redis->sAdd(‘key1′, ‘val3′);

$redis->sAdd(‘key1′, ‘val4′);

$redis->sAdd(‘key2′, ‘val3′);

$redis->sAdd(‘key2′, ‘val4′);

$redis->sAdd(‘key3′, ‘val3′);

$redis->sAdd(‘key3′, ‘val4′);

var_dump($redis->sInterStore(‘output’, ‘key1′, ‘key2′, ‘key3′));

var_dump($redis->sMembers(‘output’));

输出:

int(2)

array(2) {

[0]=>

string(4) “val4″

[1]=>

string(4) “val3″

}

sUnion

描述:返回一个所有指定键的并集

参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

返回值:Array of strings: The union of all these sets.

范例:  $redis->delete(’s0′, ’s1′, ’s2′);

$redis->sAdd(’s0′, ‘1′);

$redis->sAdd(’s0′, ‘2′);

$redis->sAdd(’s1′, ‘3′);

$redis->sAdd(’s1′, ‘1′);

$redis->sAdd(’s2′, ‘3′);

$redis->sAdd(’s2′, ‘4′);

var_dump($redis->sUnion(’s0′, ’s1′, ’s2′));

输出:

array(4) {

[0]=>

string(1) “3″

[1]=>

string(1) “4″

[2]=>

string(1) “1″

[3]=>

string(1) “2″

}

sUnionStore

描述: 执行sUnion命令并把结果储存到新建的变量中。

参数:  Key: dstkey, the key to store the diff into.

Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

返回值:INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

范例:  $redis->delete(’s0′, ’s1′, ’s2′);

$redis->sAdd(’s0′, ‘1′);

$redis->sAdd(’s0′, ‘2′);

$redis->sAdd(’s1′, ‘3′);

$redis->sAdd(’s1′, ‘1′);

$redis->sAdd(’s2′, ‘3′);

$redis->sAdd(’s2′, ‘4′);

var_dump($redis->sUnionStore(‘dst’, ’s0′, ’s1′, ’s2′));

var_dump($redis->sMembers(‘dst’));

输出:

int(4)

array(4) {

[0]=>

string(1) “3″

[1]=>

string(1) “4″

[2]=>

string(1) “1″

[3]=>

string(1) “2″

}

sDiff

描述:返回第一个集合中存在并在其他所有集合中不存在的结果

参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

返回值:Array of strings: The difference of the first set will all the others.

范例:  $redis->delete(’s0′, ’s1′, ’s2′);

$redis->sAdd(’s0′, ‘1′);

$redis->sAdd(’s0′, ‘2′);

$redis->sAdd(’s0′, ‘3′);

$redis->sAdd(’s0′, ‘4′);

$redis->sAdd(’s1′, ‘1′);

$redis->sAdd(’s2′, ‘3′);

var_dump($redis->sDiff(’s0′, ’s1′, ’s2′));

输出:

array(2) {

[0]=>

string(1) “4″

[1]=>

string(1) “2″

}

sDiffStore

描述: 执行sDiff命令并把结果储存到新建的变量中。

参数: Key: dstkey, the key to store the diff into.

Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis

返回值:INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

范例:  $redis->delete(’s0′, ’s1′, ’s2′);

$redis->sAdd(’s0′, ‘1′);

$redis->sAdd(’s0′, ‘2′);

$redis->sAdd(’s0′, ‘3′);

$redis->sAdd(’s0′, ‘4′);

$redis->sAdd(’s1′, ‘1′);

$redis->sAdd(’s2′, ‘3′);

var_dump($redis->sDiffStore(‘dst’, ’s0′, ’s1′, ’s2′));

var_dump($redis->sMembers(‘dst’));

Return value: the number of elements of s0 that are neither in s1 nor in s2.

int(2)

array(2) {

[0]=>

string(1) “4″

[1]=>

string(1) “2″

}

sMembers, sGetMembers

描述:返回集合的内容

参数:Key: key

返回值:An array of elements, the contents of the set.

范例: $redis->delete(’s’);

$redis->sAdd(’s’, ‘a’);

$redis->sAdd(’s’, ‘b’);

$redis->sAdd(’s’, ‘a’);

$redis->sAdd(’s’, ‘c’);

var_dump($redis->sMembers(’s’));

输出:

array(3) {

[0]=>

string(1) “c”

[1]=>

string(1) “a”

[2]=>

string(1) “b”

}

getSet

描述: 返回当前的Key的值,并修改Key为新的值。

参数: Key: key

STRING: value

返回值:A string, the previous value located at this key.

范例:  $redis->set(‘x’, ‘42′);

$exValue = $redis->getSet(‘x’, ‘lol’); // return ‘42′, replaces x by ‘lol’

$newValue = $redis->get(‘x’)’ // return ‘lol’

Redis根本是使用内存存储,持久化的关键是这三条指令:SAVE BGSAVE LASTSAVE …

当接收到SAVE指令的时候,Redis就会dump数据到一个文件里面。

值得一说的是它的独家功能:存储列表和集合,这是它与mc之流相比更有竞争力的地方。

不介绍mc里面已经有的东东,只列出特殊的:

TYPE key — 用来获取某key的类型

KEYS pattern — 匹配所有符合模式的key,太淫\荡了,比如KEYS * 就列出所有的key了,当然,复杂度O(n)

RANDOMKEY – 返回随机的一个key

RENAME oldkey newkey — key也可以改名

列表操作,精华

RPUSH key string — 将某个值加入到一个key列表头部

LPUSH key string — 将某个值加入到一个key列表末尾

LLEN key — 列表长度

LRANGE key start end — 返回列表中某个范围的值,相当于mysql里面的分页查询那样

LTRIM key start end — 只保留列表中某个范围的值

LINDEX key index — 获取列表*****定索引号的值,要注意是O(n)复杂度

LSET key index value — 设置列表中某个位置的值

LPOP key

RPOP key — 和上面的LPOP一样,就是类似栈或队列的那种取头取尾指令,可以当成消息队列来使用了

集合操作

SADD key member — 增加元素

SREM key member — 删除元素

SCARD key — 返回集合大小

SISMEMBER key member — 判断某个值是否在集合中

SINTER key1 key2 … keyN — 获取多个集合的交集元素

SMEMBERS key — 列出集合的所有元素



快乐成长 每天进步一点点      京ICP备18032580号-1