phpredis函数使用方法详解

phpredis函数文档详解

<strong>连接到 redis 服务</strong>

<pre>
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//查看服务是否运行
echo "Server is running: " . $redis->ping();

//输出结果
Connection to server sucessfully
Server is running: PONG
?>
</pre>


<strong>Redis set函数</strong>


<pre>
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->flushAll();
echo "Connection to server sucessfully";
//设置 redis 字符串数据
$redis->set("tutorial-name", "Redis tutorial");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("tutorial-name");

//输出结果
Connection to server sucessfully
Stored string in redis:: Redis tutorial
?>
</pre>


<strong>Redis lpush函数</strong>


<pre>
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//每次运行清除redis所有缓存数据
$redis->flushAll();
//存储数据到列表中(可以想象上车 从前门一个个进入 所以先进入的都是排在后面)
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$redis->lpush("tutorial-list", "c++");
$redis->lpush("tutorial-list", "php");
// 获取存储的数据并输出
$arList = $redis->lrange("tutorial-list", 0 ,2);

print_r($arList);

//结果 读取了0到2的范围
Array
(
[0] => php
[1] => c++
[2] => Mysql
)

?>
</pre>

 

 

<strong>Redis keys函数</strong>


<pre>
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->flushAll();
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$redis->lpush("tutorial-list", "c++");
$redis->lpush("tutorial-list", "php");

// 获取数据并输出
$arList = $redis->keys("*");
echo "Stored keys in redis:: ";
print_r($arList);

//输出结果 返回了 所有key
Stored keys in redis:: Array ( [0] => tutorial-list )
?>
</pre>

 

 

 

 

posted @ 2019-11-12 14:33  newmiracle宇宙  阅读(291)  评论(0编辑  收藏  举报