Analysis of php Framework CodeIgniter Using redis

  • 2021-09-20 19:38:08
  • OfStack

In this paper, an example is given to describe the method of using redis in php framework CodeIgniter. Share it for your reference, as follows:

1. Install redis

First, the redis service (redis database) must be installed and running on the computer. For details, see another article: https://www.ofstack.com/article/138173. htm

2. Install phpredis

① Download

Project address: https://github.com/phpredis/phpredis (you can ignore this). It is mentioned here that the windows version of phpredis should be compiled by itself, but of course we can't be so foolhardy.

Let's talk about the detours I have taken. 1 started to download from http://windows.php.net/downloads/pecl/snaps/redis/20160319/(this can also be ignored). I can't get it straight. In fact, this vc14 is version 7.0 of php. What we need is version 7.1

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

Click on the corresponding version:

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

Download the version corresponding to 7.1.

② Installation

Put the downloaded and decompressed php_redis. dll in ext of php interpreter, and you will find that modules such as mysql are also placed here, then open php. ini and find it ;extension=php_bz2.dll On top of which you add extension=php_redis.dll ,

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

③ View configuration information

Restart the server or restart the computer. Add a viewing page under the path of index. php: phpinfo. php, and add:


<?php
 echo phpinfo();
?>

Then visit http://yourdomain.com/phpinfo.php, and you can see the configuration information, looking for any information about the successful configuration of redis, and if so, the configuration is completed.

3. redis is operated in the native way of php


//  Native redis Class library, not required config/redis.php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
//$redis->set('key10','xx10',20);// No. 1 3 Parameter is the duration, the unit is seconds, if not filled in, it is permanent 
echo $redis->get('key10');

4. Configure redis. php

Create the file redis. php under myApplication/config:


<?php
defined('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 use the framework's redis library or the following custom redis library.

In addition to configuring redis. php, you must also configure the

application/config/config. php configures the cache type we use, and the default is this:


$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'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

If we use redis, we should 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 using CI framework


//  Framed redis Library 
$this->load->driver('cache');
$this->cache->redis->save('key11','xx11');// Note here that the first 3 The parameters are time, which is defined in the custom redis Library will explain 
echo $this->cache->redis->get('key11');

6. Use a custom redis class library

① Rediscli_default. php

Custom redis class library can be copied from system/libraries/Cache/drivers/Cache_redis. php, renamed Rediscli_default.php, and the class name is also changed to Rediscli_default, but there is no need to change it, and you can add more methods by yourself. Place it under myApplication/libraries/Rediscli/drivers/

② Rediscli. php

Create an Rediscli. php under myApplication/libraries/Rediscli/


<?php
defined ( '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, which needs to be configured 
$this->load->driver('rediscli');
if ($this->rediscli->default->is_supported())
{
 echo $this->rediscli->default->get('key2');
}

Time

This custom redis library and framework of the library is 1, in this focus on 1 below.


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

This is the saved value, and the third parameter is the time, which cannot be omitted. As you can see by looking at the function, the default value of this parameter is 60 seconds, not permanent, so this parameter cannot be omitted.

7. Pay 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

What this code means is to first use redis for storage, and if it is not found, use text storage. You will find text files stored in myApplication/cache, and each key will have 1 text.

Because there is no error, you may not know where this data exists at 1 o'clock.

This is still less used, after all, redis is used for faster speed.

More readers interested in CodeIgniter can check the topics of this site: "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Summary of Excellent Development Framework of php", "Introduction to ThinkPHP", "Summary of Common Methods of ThinkPHP", "Introduction to Zend FrameWork Framework", "Introduction to php Object-Oriented Programming", "Introduction to php + mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on CodeIgniter framework.


Related articles: