微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[日常] Redis基本使用测试

Redis一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和集群(Cluster)提供高可用性(high availability)。

特点:Redis数据库完全在内存中,使用磁盘仅用于持久性。相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。Redis可以将数据复制到任意数量的从服务器。

命令:测试:ping 返回pong字符串:get set哈希:hmset hget hgetall   hmset student name "taoshihan" point 100   hgetall student   hget student name列表:   lpush lrange   lpush users "taoshihan2"   lrange users 0 -1集合:元素唯一   sadd smembers   sadd people aaa   smembers people有序集合:成员有分数   zadd zrangebyscore   zadd man 2 aaa   zadd man 1 bbb   zrangebyscore man 0 1000订阅:   subscribe publish   subscribe testChat   publish testChat "hello world"事务:   multi exec   multi开启事务,写命令,exec开始执行

PHP:1.pecl install redis;//安装扩展2.配置PHP.ini3.PHP -m|grep redis ;//检测扩展

PHP dis = dis(); dis->connect('127.0.0.1',6379dis->set('name_1','taoshihan'=dis->get('name_1'(<span style="color: #008000">//<span style="color: #008000">hash
<span style="color: #800080">$redis
->hMSet("student",<span style="color: #0000ff">array
("name"=>"taoshihan","point"=>200<span style="color: #000000">));
<span style="color: #800080">$res
=<span style="color: #800080">$redis
->hGet("student","point"<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$res<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">list
<span style="color: #800080">$redis->lPush('users','lisi'<span style="color: #000000">);
<span style="color: #800080">$res=<span style="color: #800080">$redis->lRange('users',-1<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$res<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">set
<span style="color: #800080">$redis->sAdd('people','member1'<span style="color: #000000">);
<span style="color: #800080">$res=<span style="color: #800080">$redis->sMembers('people'<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$res<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">Sorted sets
<span style="color: #800080">$redis->zAdd('man',5,'val5'<span style="color: #000000">);
<span style="color: #800080">$res=<span style="color: #800080">$redis->zRange('man',-1<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$res<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">Pub/sub
<span style="color: #800080">$redis->publish('testChat','hello,world PHP!'<span style="color: #000000">);
<span style="color: #0000ff">function f(<span style="color: #800080">$redis,<span style="color: #800080">$chan,<span style="color: #800080">$msg<span style="color: #000000">) {
<span style="color: #0000ff">echo <span style="color: #800080">$chan.":".<span style="color: #800080">$msg."\r\n"<span style="color: #000000">;
}

<span style="color: #008000">//<span style="color: #008000">$redis->subscribe(array('testChat'),'f');

//transactions
<span style="color: #800080">$ret = <span style="color: #800080">$redis-><span style="color: #000000">multi()
->set('key1','val1'<span style="color: #000000">)
->get('key1'<span style="color: #000000">)
->set('key2','val2'<span style="color: #000000">)
->get('key2'<span style="color: #000000">)
-><span style="color: #008080">exec<span style="color: #000000">();
<span style="color: #008080">var_dump(<span style="color: #800080">$ret);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐