Install redis under wamp

About Redis

Redis is completely open source and free. It complies with BSD protocol and is a high-performance key value database.

Redis and other key value caching products have the following three features:

  • Redis supports data persistence. Data in memory can be saved on disk. It can be loaded again for use when restarting.
  • Redis not only supports simple key value data, but also provides the storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, that is, data backup in master slave mode.        

Redis installation

Install redis on each platform. Query: http://www.runoob.com/redis/redis-install.html

php extension Redis

After installing redis on Windows, if you need to run redis on a running php project, you need to extend php's support for redis. You need to install 2 dll extension files (php? Igbinary and php? Redis).

First, you need to select the corresponding extension file according to your php version information.

php_igbinary.dll :   http://windows.php.net/downloads/pecl/releases/igbinary/1.1.1/

php_redis.dll  :  http://windows.php.net/downloads/pecl/releases/redis/2.2.7/

Put the downloaded and unzipped php_igbinary.dll and php_redis.dll into the ext directory of PHP

Then modify php.ini (wamp address: wamp\bin\apache\bin) and add

; php-redis
extension=php_igbinary.dll
extension=php_redis.dll

Restart apache and check phpinfo to see the redis extension information

php operation Redis

After the above steps are completed, you can read and write redis in the php program.

thinkPHP has encapsulated the redis class for operation. Refer to the following:

class CacheRedis extends Cache {
	 /**
	 * Schema function
     * @param array $options Cache parameters
     * @access public
     */
    public function __construct($options=array()) {
        if ( !extension_loaded('redis') ) {
            throw_exception(L('_NOT_SUPPERT_').':redis');
        }
        if(empty($options)) {
            $options = array (
                'host'          => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
                'port'          => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
                'timeout'       => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
                'persistent'    => false,
            );
        }
        $this->options =  $options;
        $this->options['expire'] =  isset($options['expire'])?  $options['expire']  :   C('DATA_CACHE_TIME');
        $this->options['prefix'] =  isset($options['prefix'])?  $options['prefix']  :   C('DATA_CACHE_PREFIX');        
        $this->options['length'] =  isset($options['length'])?  $options['length']  :   0;        
        $func = $options['persistent'] ? 'pconnect' : 'connect';
        $this->handler  = new Redis;
        $options['timeout'] === false ?
            $this->handler->$func($options['host'], $options['port']) :
            $this->handler->$func($options['host'], $options['port'], $options['timeout']);
    }

    /**
     * Read cache
     * @access public
     * @param string $name Cache variable name
     * @return mixed
     */
    public function get($name) {
        N('cache_read',1);
        $value = $this->handler->get($this->options['prefix'].$name);
        $jsonData  = json_decode( $value, true );
        return ($jsonData === NULL) ? $value : $jsonData;	//Check whether to return JSON parsing array for JSON data true, and return source data for false
    }

    /**
     * Write cache
     * @access public
     * @param string $name Cache variable name
     * @param mixed $value  Store data
     * @param integer $expire  Effective time (seconds)
     * @return boolen
     */
    public function set($name, $value, $expire = null) {
        N('cache_write',1);
        if(is_null($expire)) {
            $expire  =  $this->options['expire'];
        }
        $name   =   $this->options['prefix'].$name;
        //Cache array / object data to ensure data integrity
        $value  =  (is_object($value) || is_array($value)) ? json_encode($value) : $value;
        if(is_int($expire)) {
            $result = $this->handler->setex($name, $expire, $value);
        }else{
            $result = $this->handler->set($name, $value);
        }
        if($result && $this->options['length']>0) {
            // Record cache queue
            $this->queue($name);
        }
        return $result;
    }

    /**
     * Delete cache
     * @access public
     * @param string $name Cache variable name
     * @return boolen
     */
    public function rm($name) {
        return $this->handler->delete($this->options['prefix'].$name);
    }

    /**
     * Clear cache
     * @access public
     * @return boolen
     */
    public function clear() {
        return $this->handler->flushDB();
    }

}

Tags: Redis PHP Windows Apache

Posted on Thu, 30 Apr 2020 01:23:09 -0700 by Klojum