PHP Framework CodeIgniter uses Redis to explain

Source: Internet
Author: User
Tags php framework php programming phpinfo codeigniter
This article mainly introduces the PHP framework CodeIgniter use of Redis method, combined with examples of Redis installation, setup and CodeIgniter use of Redis's related operating skills and considerations, the need for friends can refer to the next

This example describes the PHP framework CodeIgniter using Redis. Share to everyone for your reference, as follows:

1. Installing Redis

First, the Redis service (Redis database) must already be installed on the computer and run, as described in another article://www.jb51.net/article/138173.htm

2, Installation Phpredis

① Download

Project Address: Https://github.com/phpredis/phpredis (regardless of this), it is mentioned that the Windows version of the Phpredis to compile their own, of course, we can not be so reckless.

Next I went through the detour, from the beginning of the http://windows.php.net/downloads/pecl/snaps/redis/20160319/(this can be no matter) downloaded, has been bad, In fact, this VC14 is the 7.0 version of PHP, we need to be 7.1 version, so has been a mistake, can not find the problem, until we find this:

http://pecl.php.net/package-stats.php

The corresponding version point goes in:

Http://pecl.php.net/package/redis/3.1.1/windows

Download the corresponding version of 7.1.

② Installation

Will download the extracted php_redis.dll in the PHP interpreter ext, will find the MySQL and other modules are also placed here, and then open php.ini, Find ;extension=php_bz2.dll , add on it, extension=php_redis.dll

That is, in the extension configuration area of the head, add the configuration of this redis. The installation is complete.

③ Viewing configuration information

Restart the server or restart the computer, index.php the path to add a viewing page: phpinfo.php, add:

<?php echo phpinfo ();? >

Then visit http://yourdomain.com/phpinfo.php, you can see the configuration information to find out if there is no Redis configuration successful information, if any, the configuration is complete.

3, PHP native way to operate Redis

Native Redis class library, do not need Config/redis.php$redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6379);//$redis->set (' Key10 ') , ' xx10 ', 20);//The third parameter is the duration, the unit is the second, if not filled with the permanent echo $redis->get (' Key10 ');

4, Configuration redis.php

Create a file redis.php under Myapplication/config:

<?phpdefined (' BasePath ') or exit (' No Direct script access allowed ');/** * Created by Phpstorm. * DATE:2017/2/9 * time:13:32 * * * $config [' socket_type '] = ' TCP '; $config [' host '] = ' 127.0.0.1 '; $config [' password '] = null;$ config[' port ' = 6379; $config [' timeout '] = 0;? >

This configuration file is required whether you are using the framework's Redis library or the following custom Redis library.

In addition to configuring redis.php, you must also

application/config/config.php Configure the type of cache we use, which is the default:

$config [' sess_driver '] = ' files '; $config [' sess_cookie_name '] = ' ci_session '; $config [' sess_expiration '] = 7200;$ config[' sess_save_path ' = NULL, $config [' sess_match_ip '] = FALSE, $config [' sess_time_to_update '] = +; $config [' Sess_ Regenerate_destroy '] = FALSE;

If we use Redis, we'll configure it like this:

$config [' sess_driver '] = ' redis '; $config [' sess_cookie_name '] = ' ci_session '; $config [' sess_expiration '] = 0; $config [' Sess_save_path '] = ' tcp://127.0.0.1:xxxx '; $config [' sess_match_ip '] = FALSE; $config [' sess_time_to_update '] = 600;$ config[' Sess_regenerate_destroy ') = TRUE;

5. Redis Library with CI framework

Framework of Redis Library $this->load->driver (' cache '); $this->cache->redis->save (' Key11 ', ' xx11 ');//note here, The third parameter is time, in the custom Redis library will explain the Echo $this->cache->redis->get (' Key11 ');

6. Using a custom Redis class library

①rediscli_default.php

The custom Redis class library can be copied from system/libraries/cache/drivers/cache_redis.php, renamed to Rediscli_default.php, and the class name changed to Rediscli_default, There is no need to change and you can add more methods yourself. Put it under the myapplication/libraries/rediscli/drivers/

②rediscli.php

Create a rediscli.php under myapplication/libraries/rediscli/

<?phpdefined (' BasePath ') or exit (' No Direct script access allowed ');/** * Created by Phpstorm. * DATE:2017/2/9 * time:20:00 */class REDISCLI extends ci_driver_library {public $valid _drivers, public $CI; function __ Construct () {  $this->ci = & Get_instance ();  $this->valid_drivers = Array (   ' default '  );}}

③ Call

Custom class, need to configure $this->load->driver (' rediscli '); if ($this->rediscli->default->is_supported ()) {echo $ This->rediscli->default->get (' Key2 ');}

④ time

The library for this custom Redis library is the same as the framework, and here is a set of instructions.

$this->cache->redis->save (' Key11 ', ' xx11 ', 1000);

This is the save value, the 3rd parameter is the time, this time cannot be omitted. By looking at the function you can see that the default value of this parameter is 60 seconds, not permanent, so this parameter cannot be omitted.

7. Attention to this situation

Text storage $this->load->driver (' cache ', Array (' adapter ' = ' redis ', ' backup ' = ' file '); $this->cache-> Save (' Key5 ', ' xx5 ', 10000), Echo $this->cache->get (' Key5 ');//xx5

This code means that the first use of Redis to store, if not found, the use of text storage. You will find that a text file is stored in Myapplication/cache, and each key will have a text.

Because there is no error, so the moment may not know exactly where this data exists.

This is still less use, after all, Redis is for faster.

Articles you may be interested in:

Examples of TCP server and client functions implemented by PHP programming

PHP simple implementation of regular matching provincial urban methods

PHP closures definition and use simple example PHP tips

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.