PHP 连接Redis 代码

 连接到Redis服务器​
 
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//check whether server is running or not
echo "Server is running: " . $redis->ping();
?>

 当执行程序时,会产生下面的结果:
 
Connection to server sucessfully
Server is running: PONG

 Redis的PHP字符串实例
 
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//set the data in redis string
$redis->set("tutorial-name", "Redis tutorial");
// Get the stored data and print it
echo "Stored string in redis:: " . $redis.get("tutorial-name");
?>




 当执行程序时,会产生下面的结果:
 
Connection to server sucessfully
Stored string in redis:: Redis tutorial




 Redis的PHP列表示例
 
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//store data in redis list
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
// Get the stored data and print it
$arList = $redis->lrange("tutorial-list", 0 ,5);
echo "Stored string in redis:: "
print_r($arList);
?>



当执行程序时,会产生下面的结果:
 
Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql




 Redis的PHP键例
 
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// Get the stored keys and print it
$arList = $redis->keys("*");
echo "Stored keys in redis:: "
print_r($arList);
?>




 当执行程序时,会产生下面的结果:
 
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list



 
相关文章:
Redis使用详细教程 

0 个评论

要回复文章请先登录注册