版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1333799
Redis
对于程序开发者来说,拥有很大的优势,能学好这些重点知识一定能提高我们的能力,所以闲时充充电多加了解,在此做一点笔记.前期学习推荐:
继续推荐:
¥9.9
还不错 —— Redis 入门到分布式实践(陈宠) Redis
类的命名空间是根 \phpredis
的命令和参数和 redis.io
中的实际命令对应常用命令有 MULTI、EXEC、DISCARD。
MULTI:标记一个事务块的开始;
EXEC:执行所有事务块内的命令;
DISCARD:取消事务,放弃执行事务块内的所有命令;
UNWATCH:取消 WATCH 命令对所有 key 的监视;
WATCH key:监视 key,如果在事务执行之前 key 被其他命令所改动,那么事务将被打断
$redis = new \Redis();
$redis->pconnect('192.168.236.131', '6379'); #连接redis服务
$redis->set('say','moTzxx say hello !');
//echo $redis->get('say');
$redis->delete('myname');
$redis->set('myname','moTzxx');
echo $redis->get('myname').'<br>'; # 返回:moTzxx
var_dump($redis->del('myname'));# 返回 TRUE(1)
var_dump($redis->get('myname')); # 返回 bool(false)
//$redis->set('tag',10);
$redis->delete('tag');
$redis->incr('tag');
//$redis->incrBy('tag',3);
var_dump($redis->get('tag'));
echo '<hr/>';
$redis->delete('list1');
$redis->lPush('list1','hello');
$redis->rPush('list1','miss');
var_dump($redis->rPop('list1')); #string(4) "miss"
echo '<hr/>';
$redis->delete('set1');
$redis->sAdd('set1','a');
$redis->sAdd('set1',2);
$redis->sAdd('set1','ok');
$redis->sAdd('set1','ok');//TODO 集合的唯一性
$val = $redis->sCard('set1');
var_dump($val); #int(3)
$val2 = $redis->sMembers('set1');
var_dump($val2);#array(3) { [0]=> string(2) "ok" [1]=> string(1) "2" [2]=> string(1) "a" }
echo '<br/>';
$redis->set('key1', 'value', 111);
var_dump($redis->get('key1'));
$redis->getSet('key1','value11');
var_dump($redis->mget(['tag','key1']));
var_dump($redis->type('tag'));
echo '<hr/>';
$redis->delete('driver1');
$redis->hSet("driver1",'name','moTzxx');
$redis->hSet("driver1",'age',12);
$redis->hSet('driver1','gender',1);
$val = $redis->hGet("driver1",'name');
var_dump($val);#string(6) "moTzxx"
$val = $redis->hMGet("driver1",['name','gender']);
var_dump($val); #array(2) { ["name"]=> string(6) "moTzxx" ["gender"]=> string(1) "1" }
echo '<hr/>';
$redis->delete('pats');
$redis->zAdd('pats',1,'cat1'); # 构建一个zset (sorted set) // int(1)
$redis->zAdd('pats',3,'dog3');
$redis->zAdd('pats',2,'monkey2');
$redis->zAdd('pats',2,'pig2');
$val = $redis->zRange('pats',0,-1); #从低到高
var_dump($val); //array(4) { [0]=> string(4) "cat1" [1]=> string(7) "monkey2" [2]=> string(4) "pig2" [3]=> string(4) "dog3" }
$val2 = $redis->zRevRange('pats',0,-1); #从高到低
var_dump($val2);//array(4) { [0]=> string(4) "dog3" [1]=> string(4) "pig2" [2]=> string(7) "monkey2" [3]=> string(4) "cat1" }
var_dump($redis->TYPE('pats')); //zset / int(4)
Redis Sentinal
着眼于高可用,在master
宕机时会自动将slave
提升为master
,继续提供服务。Redis Cluster
着眼于扩展性,在单个redis
内存不足时,使用Cluster
进行分片存储