For large Web sites, Redis is very popular, the use of Redis cache, the site can speed up an instant n times. So how does PHP connect to Redis, and here's an introductory sample code.
<?php
$redis = new Redis (); Create an Object
$redis->connect (' 127.0.0.1 ', 6379); Connect Redis
$redis->select (0); Select the database (default 16 databases, 0-15, which can be modified in the configuration file.) )
$redis->set (' A1 ', ' Www.daixiaorui.com '); Write a record to Redis
echo $redis->get (' A1 '); Read a record from the Redis
?>
Redis PHP String Instance
<?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::" + jedis.get ("Tutorial-name");
?>
When you execute the program, the following results are generated:
Connection to Server sucessfully
Stored string in Redis:: Redis Tutorial
Examples of Redis PHP columns
<?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);
?>
When you execute the program, the following results are generated:
Connection to Server sucessfully
Stored string in Redis::
Redis
Mongodb
Mysql
PHP Key Example of Redis
<?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);
?>
When you execute the program, the following results are generated:
Connection to Server sucessfully
Stored string in Redis::
Tutorial-name
Tutorial-list
It's so simple, it feels a bit like connecting to a MySQL database. Before running the above code, make sure that your computer is installed and starting the Redis service, and verify that the Redis extension is installed in PHP, please check it phpinfo. If not installed, go to the official website next corresponding to the expansion of the PHP version.