一、Connection
1、connect - 连接到服务器
$redis->connect('127.0.0.1', 6379);
$redis->connect('127.0.0.1'); // port 6379 by default
$redis->connect('tls://127.0.0.1', 6379); // enable transport level security.
$redis->connect('tls://127.0.0.1'); // enable transport level security, port 6379 by default.
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
$redis->connect('/tmp/redis.sock'); // unix domain socket.
$redis->connect('127.0.0.1', 6379, 1, NULL, 100); // 1 sec timeout, 100ms delay between reconnection attempts.
$redis->connect('unix://redis.sock'); // relative path to unix domain socket requires version 5.0.0 or higher.
2、pconnect - 连接到服务器(持久)
$redis->pconnect('127.0.0.1', 6379);
$redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before.
$redis->pconnect('tls://127.0.0.1', 6379); // enable transport level security.
$redis->pconnect('tls://127.0.0.1'); // enable transport level security, port 6379 by default.
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection than the three before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
$redis->pconnect('unix://redis.sock'); // relative path to unix domain socket requires version 5.0.0 or higher.
3、auth - 向服务器进行身份验证
$redis->auth('foobared');
4、select 更改当前连接的选定数据库
$redis->select(0); // switch to DB 0
$redis->set('x', '42'); // write 42 to x
$redis->move('x', 1); // move to DB 1
$redis->select(1); // switch to DB 1
$redis->get('x'); // will return 42
5、setOption - 设置客户端选项
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // Don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // Use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // Use igBinary serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK); // Use msgpack serialize/unserialize
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
/* Options for the SCAN family of commands, indicating whether to abstract
empty results from the user. If set to SCAN_NORETRY (the default), phpredis
will just issue one SCAN command at a time, sometimes returning an empty
array of results. If set to SCAN_RETRY, phpredis will retry the scan command
until keys come back OR Redis returns an iterator of zero
*/
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY);
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
6、ping - 检查当前连接状态
/* When called without an argument, PING returns `TRUE` */
$redis->ping();
/* If passed an argument, that argument is returned. Here 'hello' will be returned */
$redis->ping('hello');
7、getOption - 设置客户端选项
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // Don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // Use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // Use igBinary serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK); // Use msgpack serialize/unserialize
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
/* Options for the SCAN family of commands, indicating whether to abstract
empty results from the user. If set to SCAN_NORETRY (the default), phpredis
will just issue one SCAN command at a time, sometimes returning an empty
array of results. If set to SCAN_RETRY, phpredis will retry the scan command
until keys come back OR Redis returns an iterator of zero
*/
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY);
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
8、echo - 回显给定字符串
9、close - 关闭连接
二、Server
-
$redis->bgRewriteAOF();
-
bgSave -异步将数据集保存到磁盘(在后台)
$redis->bgSave();
-
config - 获取或设置redis服务器配置参数
$redis->config("GET", "*max-*-entries*");
$redis->config("SET", "dir", "/var/run/redis/dumps/");
-
dbSize - 获取或设置redis服务器配置参数返回选定数据库中的密钥数
$count = $redis->dbSize();
echo "Redis has $count keys\n";
-
$redis->flushAll();
-
flushDb -从当前数据库中删除所有密钥
$redis->flushDb();
-
info - 获取有关服务器的信息和统计信息
$redis->info(); /* standard redis INFO command */
$redis->info("COMMANDSTATS"); /* Information on the commands that have been run (>=2.6 only)
$redis->info("CPU"); /* just CPU information from Redis INFO */
-
$redis->lastSave();
-
$redis->resetStat();
-
save -同步将数据集保存到磁盘(等待完成)
$redis->save();
-
slaveOf - 使服务器成为另一个实例的从属服务器,或将其提升为主服务器
$redis->slaveOf('10.0.1.7', 6379);
/* ... */
$redis->slaveOf();
-
time -返回当前服务器时间
$redis->time();
-
slowLog - 访问redis slowlog条目
// Get ten slowLog entries
$redis->slowLog('get', 10);
// Get the default number of slowLog entries
$redis->slowLog('get');
// Reset our slowLog
$redis->slowLog('reset');
// Retrieve slowLog length
$redis->slowLog('len');
三、Keys and Strings
String
- append - 将值附加到键
$redis->set('key', 'value1');
$redis->append('key', 'value2'); /* 12 */
$redis->get('key'); /* 'value1value2' */
- bitCount - 计算字符串中的设置位
- bitOp - 在字符串之间执行按位操作
- decr,decrBy- 减小键的值
$redis->decr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value -1 */
$redis->decr('key1'); /* -2 */
$redis->decr('key1'); /* -3 */
$redis->decrBy('key1', 10); /* -13 */
- get - 获取密钥的值
$redis->get('key');
- getBit - 返回存储在键上的字符串值中偏移处的位值
$redis->set('key', "\x7f"); // this is 0111 1111
$redis->getBit('key', 0); /* 0 */
$redis->getBit('key', 1); /* 1 */
- getRange - 获取存储在键上的字符串的子字符串
$redis->set('key', 'string value');
$redis->getRange('key', 0, 5); /* 'string' */
$redis->getRange('key', -5, -1); /* 'value' */
- getset -设置键的字符串值并返回其旧值
$redis->set('x', '42');
$exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol'
$newValue = $redis->get('x')' // return 'lol'
- incr,incrby-增加密钥的值
$redis->incr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value 1 */
$redis->incr('key1'); /* 2 */
$redis->incr('key1'); /* 3 */
$redis->incr('key1'); /* 4 */
$redis->incrBy('key1', 10); /* 14 */
- incrbyfloat-将键的浮点值增加给定的量
$redis->incrByFloat('key1', 1.5); /* key1 didn't exist, so it will now be 1.5 */
$redis->incrByFloat('key1', 1.5); /* 3 */
$redis->incrByFloat('key1', -1.5); /* 1.5 */
$redis->incrByFloat('key1', 2.5); /* 4 */
- mget,getmultiple-获取所有给定键的值
$redis->set('key1', 'value1');
$redis->set('key2', 'value2');
$redis->set('key3', 'value3');
$redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
$redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value1', `FALSE`);
- mset,msetnx-将多个键设置为多个值
$redis->mSet(array('key0' => 'value0', 'key1' => 'value1'));
- set-设置键的字符串值
// Simple key -> value set
$redis->set('key', 'value');
// Will redirect, and actually make an SETEX call
$redis->set('key','value', 10);
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
$redis->set('key', 'value', Array('nx', 'ex'=>10));
// Will set a key, if it does exist, with a ttl of 1000 miliseconds
$redis->set('key', 'value', Array('xx', 'px'=>1000));
- setbit-设置或清除存储在键上的字符串值中偏移的位
$redis->set('key', "*"); // ord("*") = 42 = 0x2f = "0010 1010"
$redis->setBit('key', 5, 1); /* returns 0 */
$redis->setBit('key', 7, 1); /* returns 0 */
$redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */
- setex,psetex-设置密钥的值和过期时间
$redis->setEx('key', 3600, 'value'); // sets key → value, with 1h TTL.
$redis->pSetEx('key', 100, 'value'); // sets key → value, with 0.1 sec TTL.
- setNx-设置密钥的值,仅当密钥不存在时
$redis->setNx('key', 'value'); /* return TRUE */
$redis->setNx('key', 'value'); /* return FALSE */
- setrange-从指定偏移量开始在键处覆盖字符串的一部分
$redis->set('key', 'Hello world');
$redis->setRange('key', 6, "redis"); /* returns 11 */
$redis->get('key'); /* "Hello redis" */
- strlen-获取存储在密钥中的值的长度
$redis->set('key', 'value');
$redis->strlen('key'); /* 5 */
keys
- del,delete-删除密钥
$redis->set('key1', 'val1');
$redis->set('key2', 'val2');
$redis->set('key3', 'val3');
$redis->set('key4', 'val4');
$redis->delete('key1', 'key2'); /* return 2 */
$redis->delete(array('key3', 'key4')); /* return 2 */
- dump-返回存储在指定键上的值的序列化版本。
$redis->set('foo', 'bar');
$val = $redis->dump('foo'); // $val will be the Redis encoded key value
- exists-确定是否存在密钥
$redis->set('key', 'value');
$redis->exists('key'); /* 1 */
$redis->exists('NonExistingKey'); /* 0 */
$redis->mset(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']);
$redis->exists(['foo', 'bar', 'baz]); /* 3 */
$redis->exists('foo', 'bar', 'baz'); /* 3 */
- expire,settimeout,pexpire-设置密钥的生存时间(秒)
$redis->set('x', '42');
$redis->expire('x', 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$redis->get('x'); // will return `FALSE`, as 'x' has expired.
- expireat,pexpireat-将密钥的到期时间设置为unix时间戳
$redis->set('x', '42');
$now = time(NULL); // current timestamp
$redis->expireAt('x', $now + 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$redis->get('x'); // will return `FALSE`, as 'x' has expired.
- keys,getkeys-查找与给定模式匹配的所有键
$allKeys = $redis->keys('*'); // all keys will match this.
$keyWithUserPrefix = $redis->keys('user*');
- scan-扫描键空间中的键(redis>=2.8.0)
- migrate-原子地将密钥从一个redis实例传输到另一个实例
$redis->migrate('backup', 6379, 'foo', 0, 3600);
$redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
$redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
/* Migrate multiple keys (requires Redis >= 3.0.6)
$redis->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600);
- move-将密钥移动到另一个数据库
$redis->select(0); // switch to DB 0
$redis->set('x', '42'); // write 42 to x
$redis->move('x', 1); // move to DB 1
$redis->select(1); // switch to DB 1
$redis->get('x'); // will return 42
- object-检查redis对象的内部
$redis->object("encoding", "l"); // → ziplist
$redis->object("refcount", "l"); // → 1
$redis->object("idletime", "l"); // → 400 (in seconds, with a precision of 10 seconds).
- persist-从密钥中移除过期
$redis->persist('key');
- random key-从键空间返回随机键
$key = $redis->randomKey();
$surprise = $redis->get($key); // who knows what's in there.
- rename,rename key-重命名密钥
$redis->set('x', '42');
$redis->rename('x', 'y');
$redis->get('y'); // → 42
$redis->get('x'); // → `FALSE`
- renameNx -重命名一个密钥,仅当新密钥不存在时
- type-确定存储在键上的类型
$redis->type('key');
- sort-对列表、集合或排序集合中的元素进行排序
- TTL,PTTL-有时间为钥匙而活
$redis->ttl('key');
- restore-使用提供的序列化值创建密钥,该值以前是通过dump获得的
$redis->set('foo', 'bar');
$val = $redis->dump('foo');
$redis->restore('bar', 0, $val); // The key 'bar', will now be equal to the key 'foo'
Hashes
- hdel-删除一个或多个哈希字段
- hash -确定是否存在哈希字段
- hget-获取哈希字段的值
- hgetall-获取散列中的所有字段和值
- hincrby-将哈希字段的整数值按给定的数字递增
- hincrbyfloat-将哈希字段的浮点值增加给定的量
- hkeys-将所有字段都放入哈希表中
- hlen-获取哈希中的字段数
- hmget-获取所有给定哈希字段的值
- hmset-将多个哈希字段设置为多个值
- hset-设置哈希字段的字符串值
- HSENNX -仅当字段不存在时设置哈希字段的值
- hvals-获取哈希中的所有值
- hscan-扫描成员的哈希密钥
Lists
- blpop,brpop-删除并获取列表中的第一个/最后一个元素
- brpoplpush-从列表中弹出一个值,将其推送到另一个列表并返回
- lindex,lget-按索引从列表中获取元素
- linsert-在列表中的另一个元素之前或之后插入元素
- Llen,lsize-获取列表的长度/大小
- lpop-删除并获取列表中的第一个元素
- lpush-将一个或多个值前置到列表
- 仅当列表存在时,向列表预置值
- lrange,lgetrange-从列表中获取一系列元素
- lrem,lremove-从列表中移除元素
- lset-根据元素的索引设置列表中元素的值
- ltrim,listtim-将列表修剪到指定范围
- rpop-删除并获取列表中的最后一个元素
- rpoplPush—删除列表中的最后一个元素,将其附加到另一个列表并返回(redis>=1.1)
- rpush-将一个或多个值附加到列表
- RPurx-将一个值追加到一个列表,只有当列表存在时
Sets
- sadd-将一个或多个成员添加到集合
- scard,ssize-获取集合中的成员数
- sdiff-减去多个集合
- sdiffstore-减去多个集合并将结果集存储在一个键中
- 烧结-多组相交
- sinterstore-交叉多个集合并将结果集存储在一个键中
- sismember,scontains-确定给定值是否是集合的成员
- smembers,sgetmembers-集合中的所有成员
- smove-将成员从一个集合移动到另一个集合
- spop-从集合中移除并返回随机成员
- srandmember-从一个集合中获取一个或多个随机成员
- srem,sremove-从集合中移除一个或多个成员
- Sunion-添加多个集合
- sunionstore-添加多个集合并将结果集存储在一个键中
- sscan-扫描集合中的成员
Sorted sets
- ZADD-将一个或多个成员添加到排序的集合中或更新它的分数,如果它已经存在
- zcard,zsize-获取排序集中的成员数
- zcount-计算排序集中的成员,其分数在给定值内
- zincrby-增加排序集中成员的得分
- zinter-将多个排序集相交,并将结果排序集存储在新键中
- Zrange-按索引返回排序集中的一系列成员
- Zrangebyscore,Zrevrangebyscore-按分数返回排序集中的一系列成员
- ZrangeBeEX-从共享相同分数的成员返回词表范围
- Zrank,Zrevrank-确定排序集中成员的索引
- zrem,zdelite-从排序集中删除一个或多个成员
- zremrangebyrank,zdeletrangebyrank-删除给定索引内已排序集合中的所有成员
- zremrangebyscore,zdeletrangebyscore-删除给定分数内已排序集合中的所有成员
- Zrevrange-返回按索引排序的集合中的一系列成员,分数从高到低排序
- zscore-获取与排序集中给定成员关联的分数
- zunion-添加多个排序集并将结果排序集存储在一个新键中
- zscan-扫描已排序的成员集
Pub/sub
- psubscribe-按模式订阅频道
- 发布-将消息发布到频道
- 订阅-订阅频道
- pub sub-对pub/sub子系统的自省
Transactions
- multi, exec, discard - 输入和退出事务模式
- watch, unwatch - 监视另一个客户端修改的密钥。
Scripting
- eval-评估lua脚本服务器端
- evalsha-从脚本的sha1散列(而不是脚本本身)计算lua脚本服务器端
- script -执行redis script命令在脚本子系统上执行各种操作
- GetLasterror-最后一条错误消息(如果有)
- clear last error-清除最后一条错误消息
- _ prefix-使用phpredis的前缀设置作为值前缀的实用方法
- _ unserialize-使用设置的序列化程序取消数据序列化的实用方法
- _ serialize-使用设置的序列化程序序列化数据的实用程序方法