[TOC]
Redis 数据库是key-value类型存储方式, 支持的数据类型有 字符串
(string)、 哈希
(Hash)、 列表
(list)、 集合
(set)、 有序集合
(Zset).
现在学习一下字符串类型的PHP
相关操作
一. PHP连接Redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
function () {
if(
$this->config->load(
'redis',
TRUE,
TRUE)){
$this->redis =
new Redis();
try
{
$config =
$this->config->item(
'redis');
if ($config[
'socket_type'] ===
'unix')
{
$success =
$this->redis->connect($config[
'socket']);
}
else
{
$success =
$this->redis->connect(
$config[
'host']
,$config[
'port']
,$config[
'timeout']
);
}
if ( ! $success)
{
log_message(
'error'
,
'Cache: Redis connection failed. Check your configuration.'
);
}
if (
isset($config[
'password']) &&
!
$this->redis->auth($config[
'password'])
)
{
log_message(
'error'
,
'Cache: Redis authentication failed.'
);
}
if (
isset($config[
'database']) &&
!
$this->redis->select($config[
'database'])
)
{
log_message(
'error'
,
'Cache: Redis select database failed.'
);
}
}
catch (RedisException $e)
{
log_message(
'error'
,
'Cache: Redis connection refused ('.$e->getMessage().
')'
);
}
}
else{
return
false;
}
}
|
二. PHP操作Redis
1. 设置数据 set
1
2
3
4
5
|
*
@param test value
*
@return boolean
*/
$this->redis->set(
'key',
'test');
|
2. 获取数据 get
1
2
3
4
|
*
@return value
*/
$this->redis->get(
'key');
|
3. 获取字符串中的一段 getrange
OR substr
1
2
3
4
5
6
|
*
@param start
*
@param end
*
@return string
*/
$this->redis->getrange($key, $start, $end);
|
讲解: 当$start或者$end为负数时,从尾部开始
4. 设置并获取数据 getset
1
2
3
4
5
|
*
@param value
*
@return string
*/
$this->redis->getset($key, $value);
|
5. 获取指定偏移量上的位 getbit
1
2
3
4
5
6
|
*
@param key
*
@param offset
*
@return string
*/
$this->redis->getbit($key, $offset);
|
注:当 offset 比字符串值的长度大,或者 key 不存在时,返回 0
6. 设置或清除指定偏移量上的位 setbit
1
2
3
4
5
6
|
*
@param key
*
@param offset
*
@return string
*/
$this->redis->setbit($key, $offset, $value);
|
7. 获取所有给定key的值 mget
1
2
3
4
5
|
*
@param key
*
@return string
|
8. 设置带过期时间的数据(单位:秒) setex
1
2
3
4
5
6
7
|
*
@param key
*
@param timeout
*
@param value
*
@return boolean
*/
$this->redis->setex($key, $timeout, $value);
|
9. 不存在key时设置数据 setnx
1
2
3
4
5
6
|
*
@param key
*
@param test value
*
@return boolean
*/
$this->redis->setnx(
'key',
'test');
|
10. 覆盖key中的数据(类是字符串替换) setrange
1
2
3
4
5
6
7
|
*
@param key
*
@param offset
*
@param value
*
@return int 字符串长度
*/
$this->redis->setrange($key, $offset, $value);
|
注:超过部分直接显示
11. 获取key中字符串长度 strlen
1
2
3
4
|
*
@return value
*/
$this->redis->strlen(
'key');
|
12. 设置多个key-value数据 mset
1
2
3
4
|
*
@return boolean
*/
$this->redis->mset($arr);
|
13. 设置多个ke-value数据,仅当key不存在 msetnx
1
2
3
4
|
*
@return boolean
*/
$this->redis->msetnx($arr);
|
14. 设置带过期时间的数据(单位:毫秒) psetex
1
2
3
4
5
6
7
8
|
*
@param key
*
@param milliseconds
*
@param value
*
@return boolean
*@不成功, undefined method Redis::psetex()
*/
$this->redis->psetex($key, $milliseconds, $value);
|
15. 数字自增一 incr
1
2
3
4
5
|
*
@param key
*
@return value
*/
$this->redis->incr(
'key');
|
16. 数字自增N incrby
1
2
3
4
5
6
|
*
@param key
*
@param N
*
@return value
*/
$this->redis->incrby(
'key',$N);
|
17. 数字自增浮点型 incrbyfloat
1
2
3
4
5
|
*
@param N.M
*
@return value
*/
$this->redis->incrbyfloat(
'key',$N);
|
18. 数字自减一 decr
1
2
3
4
5
|
*
@param key
*
@return value
*/
$this->redis->decr(
'key');
|
19. 数字自增N decrby
1
2
3
4
5
|
*
@param N
*
@return value
*/
$this->redis->decrby(
'key',$N);
|
20. 追加数据 append
1
2
3
4
5
6
|
*
@param key
*
@param value
*
@return int 字符串数量
*/
$this->redis->append($key, $value);
|
21. 删除数据 delete
1
2
3
4
5
6
|
*
@param key
*
@return boolean
*/
$this->redis->delete($key1, $key2);
$this->redis->delete(
array($key1, $key2));
|
21. 检测key是否存在 exists
1
2
3
4
5
|
*
@param key
*
@return boolean
*/
$this->redis->exists($key);
|
Author @Snaker95