Application Example of PHP connection Redis

Source: Internet
Author: User
Tags mongodb redis mysql database redis server redis tutorial


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.