개발 꿀팁/PHP

codeigniter의 Redis 사용

Jammie 2022. 8. 4. 16:31
반응형

Redis의 구성과 간단한 사용 1:
1.system/config/redis.php:

<?php
$config['redis_host']     = '127.0.0.1';
$config['redis_port']     = '6379';
$config['redis_isopen']   = true;

2./application/config/config.php:

require_once(BASEPATH . "config/redis.php");

3 . ./application/libraries/RedisService.php:

<?php
class RedisService{
    public $CI;
    public $redis;
    public function __construct()
    {
        $this->CI = & get_instance();
        $this->CI->load->driver('cache', array('adapter' => 'redis'));
        $this->redis = $this->CI->cache->get_redis();
        $this->cache = $this->CI->cache;
    }
}

4…/system/libraries/Cache/Cache.php:

protected $valid_drivers    = array(
protected $valid_drivers = array(
		'apc',
		'dummy',
		'file',
		'memcached',
		'redis',
		'wincache'
	);
고치다:
/**
	 * Reference to the driver
	 *
	 * @var mixed
	 */
	//protected $_adapter = 'dummy';
    protected $_adapter = 'redis'; // change by wuyongyu

	/**
	 * Fallback driver
	 *
	 * @var string
	 */
	//protected $_backup_driver = 'dummy';
    protected $_backup_driver = 'redis'; // change by wuyongyu

//추가:

public function get_redis(){
    if($this->_adapter == 'redis'){
        return $this->{$this->_adapter}->get_redis();
    }
    return false;
}

5 . ./system/libraries/Cache/drivers/Cache_redis.php

<?php
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @package	CodeIgniter
 * @author	EllisLab Dev Team
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
 * @copyright	Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
 * @license	http://opensource.org/licenses/MIT	MIT License
 * @link	http://codeigniter.com
 * @since	Version 3.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Redis Caching Class
 *
 * @package	   CodeIgniter
 * @subpackage Libraries
 * @category   Core
 * @author	   Anton Lindqvist <anton@qvister.se>
 * @link
 */
class CI_Cache_redis extends CI_Driver
{
	/**
	 * Default config
	 *
	 * @static
	 * @var	array
	 */
	protected static $_default_config = array(
		'socket_type' => 'tcp',
		'host' => '127.0.0.1',
		'password' => 'adasadsa', // NULL change by wuyongyu
		'port' => 6379,
		'timeout' => 0
	);

	/**
	 * Redis connection
	 *
	 * @var	Redis
	 */
	protected $_redis;

	/**
	 * An internal cache for storing keys of serialized values.
	 *
	 * @var	array
	 */
	protected $_serialized = array();

	// ------------------------------------------------------------------------

	/**
	 * Get cache
	 *
	 * @param	string	Cache ID
	 * @return	mixed
	 */
	public function get($key)
	{
		$value = $this->_redis->get($key);

		if ($value !== FALSE && isset($this->_serialized[$key]))
		{
			return unserialize($value);
		}

		return $value;
	}

	// ------------------------------------------------------------------------

	/**
	 * Save cache
	 *
	 * @param	string	$id	Cache ID
	 * @param	mixed	$data	Data to save
	 * @param	int	$ttl	Time to live in seconds
	 * @param	bool	$raw	Whether to store the raw value (unused)
	 * @return	bool	TRUE on success, FALSE on failure
	 */
	public function save($id, $data, $ttl = 60, $raw = FALSE)
	{
		if (is_array($data) OR is_object($data))
		{
			if ( ! $this->_redis->sIsMember('_ci_redis_serialized', $id) && ! $this->_redis->sAdd('_ci_redis_serialized', $id))
			{
				return FALSE;
			}

			isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE;
			$data = serialize($data);
		}
		elseif (isset($this->_serialized[$id]))
		{
			$this->_serialized[$id] = NULL;
			$this->_redis->sRemove('_ci_redis_serialized', $id);
		}

		return ($ttl)
			? $this->_redis->setex($id, $ttl, $data)
			: $this->_redis->set($id, $data);
	}

	// ------------------------------------------------------------------------

	/**
	 * Delete from cache
	 *
	 * @param	string	Cache key
	 * @return	bool
	 */
	public function delete($key)
	{
		if ($this->_redis->delete($key) !== 1)
		{
			return FALSE;
		}

		if (isset($this->_serialized[$key]))
		{
			$this->_serialized[$key] = NULL;
			$this->_redis->sRemove('_ci_redis_serialized', $key);
		}

		return TRUE;
	}

	// ------------------------------------------------------------------------

	/**
	 * Increment a raw value
	 *
	 * @param	string	$id	Cache ID
	 * @param	int	$offset	Step/value to add
	 * @return	mixed	New value on success or FALSE on failure
	 */
	public function increment($id, $offset = 1)
	{
		return $this->_redis->incr($id, $offset);
	}

	// ------------------------------------------------------------------------

	/**
	 * Decrement a raw value
	 *
	 * @param	string	$id	Cache ID
	 * @param	int	$offset	Step/value to reduce by
	 * @return	mixed	New value on success or FALSE on failure
	 */
	public function decrement($id, $offset = 1)
	{
		return $this->_redis->decr($id, $offset);
	}

	// ------------------------------------------------------------------------

	/**
	 * Clean cache
	 *
	 * @return	bool
	 * @see		Redis::flushDB()
	 */
	public function clean()
	{
		return $this->_redis->flushDB();
	}

	// ------------------------------------------------------------------------

	/**
	 * Get cache driver info
	 *
	 * @param	string	Not supported in Redis.
	 *			Only included in order to offer a
	 *			consistent cache API.
	 * @return	array
	 * @see		Redis::info()
	 */
	public function cache_info($type = NULL)
	{
		return $this->_redis->info();
	}

	// ------------------------------------------------------------------------

	/**
	 * Get cache metadata
	 *
	 * @param	string	Cache key
	 * @return	array
	 */
	public function get_metadata($key)
	{
		$value = $this->get($key);

		if ($value)
		{
			return array(
				'expire' => time() + $this->_redis->ttl($key),
				'data' => $value
			);
		}

		return FALSE;
	}

	// ------------------------------------------------------------------------

	/**
	 * Check if Redis driver is supported
	 *
	 * @return	bool
	 */
	public function is_supported()
	{
		if ( ! extension_loaded('redis'))
		{
			log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
			return FALSE;
		}

		return $this->_setup_redis();
	}

	// ------------------------------------------------------------------------

	/**
	 * Setup Redis config and connection
	 *
	 * Loads Redis config file if present. Will halt execution
	 * if a Redis connection can't be established.
	 *
	 * @return	bool
	 * @see		Redis::connect()
	 */
	protected function _setup_redis()
	{
		$config = array();
		$CI =& get_instance();

		if ($CI->config->load('redis', TRUE, TRUE))
		{
			$config += $CI->config->item('redis');
		}

		$config = array_merge(self::$_default_config, $config);

		$this->_redis = new Redis();
        //log_message('error',$config);
		try
		{
			if ($config['socket_type'] === 'unix')
			{
				$success = $this->_redis->connect($config['socket']);
			}
			else // tcp socket
			{
				$success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
			}

			if ( ! $success)
			{
				log_message('debug', 'Cache: Redis connection refused. Check the config.');
				return FALSE;
			}
		}
		catch (RedisException $e)
		{
			log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
			return FALSE;
		}

		if (isset($config['password']))
		{
			$this->_redis->auth($config['password']);
		}

		// Initialize the index of serialized values.
		$serialized = $this->_redis->sMembers('_ci_redis_serialized');
		if ( ! empty($serialized))
		{
			$this->_serialized = array_flip($serialized);
		}

		return TRUE;
	}

	// ------------------------------------------------------------------------

	/**
	 * Class destructor
	 *
	 * Closes the connection to Redis if present.
	 *
	 * @return	void
	 */
	public function __destruct()
	{
		if ($this->_redis)
		{
			$this->_redis->close();
		}
	}

    public function get_redis(){
        return $this->_redis;
    }

}

6. 컨트롤러에서 사용할 수 있습니다

//초기화:
$this->load->library('RedisService');
$this->redis = new RedisService;

//사용하다:
$this->redis->redis->set('name', 'chenjian');
$this->redis->redis->get('name');

2. Redis 실제 사용
redis를 사용하여 데이터베이스 요청하기캐시를 실행하다.
~ 예 ~:

1. 컨트롤러 중:****

$this->load->model("user"); //초기화
$this->load->library( 'cache_mgr' ); //cache_mgr 라이브러리 파일 참조
/*
get_cache네 가지 파라미터가 있습니다:get_cache(key,모델 안에 들어가는 법, 매개 변수 배열, 캐시 기간),get_cache 방법은 캐시만 가져올 수 있는 것이 아닙니다,만약 캐시가 없다면 model 안의 메소드를 실행하고, 캐시를 실행。
*/
	$this->cache_mgr = new CacheMgr();
    $data = $this->cache_mgr->get_cache($key, array($this->user, 'get_user'), array('age' => $age), 60 * 60);
}

application/libraries/CacheMgr里面的库文件:

<?php
class CacheMgr{
    protected static $redis;
    protected $host;
    protected $port;
    protected $pwd;
    protected $is_open;

    private $_config = array();

    public function __construct(){
        $this->_config = get_config();
        $this->host    = $this->_config['redis_host'];
        $this->port    = $this->_config['redis_port'];
        $this->is_open = $this->_config['redis_isopen'];
        $this->pwd     = $this->_config['redis_pwd'];

        if (! $this->is_open) {
            self::$redis = null;
        } elseif (! self::$redis)
            $this->connect ();
    }

    /**
     *
     * @return cache_mgr
     */
    static function get_instance(){
        $ci = get_instance ();
        $var = __CLASS__;
        return $ci->$var;
    }

    /**
     * 현재 rediscache 서버가 유효한지 여부를 반환합니다
     *
     * @return boolean
     */
    public function is_connected(){
        return self::$redis ? true : false;
    }

    /**
     * rediscache 연결
     */
    private function connect(){
        self::$redis = new Redis();
        if (! @self::$redis->connect ( $this->host, $this->port )) {
            self::$redis = null;
            // echo "not redis";
        } else {
            self::$redis->auth($this->pwd);
        }
    }

    /**
     * 프런트 엔드 코드 읽기/쓰기 캐시.모든 프런트 엔드 코드는 이 방법으로만 캐시를 쓸 수 있습니다
     *
     * @param string $key 키 값,배열 array (group 값, 키 값),group 값은 대량으로 삭제할 수 있습니다
     * @param callback $function
     * @param array $params
     * @param int $expire
     * @return mixed
     */
    public function get_cache($key, $function, $params = array(), $expire = 0){
        // 캐시가 켜져 있지 않으면
        if (! self::$redis) {
            return call_user_func_array ( $function, $params );
        }
        $rs = $this->get ( $key );
        if (! $rs) {
            $rs = call_user_func_array ( $function, $params );
            if (! $rs) $expire = 5 * 60;
            $this->save( $key, $rs, $expire );
        }
        return $rs;
    }

    /**
     * 지울 캐시
     *
     * @param string $key
     */
    public function clear($key){
        return self::$redis ? self::$redis->delete ( $key ) : false;
    }


    /**
     * Get cache
     *
     * @param   string  $key    Cache ID
     * @return  mixed
     */
    public function get($key){
        $data = self::$redis->hMGet($key, array('__ci_value'));
        if ( ! isset($data['__ci_value']) OR $data['__ci_value'] === FALSE){
            return FALSE;
        }
        return unserialize($data['__ci_value']);
    }
    
    

    /**
     * 일반적으로 백그라운드에서 사용할 캐시 생성
     *
     * @param string $key
     * @param string $value
     * @param int $ttl 기한이 지난 초수는 0시이며, 기한을 넘기지 않는다
     * @return bool
     */
    public function save($key, $value, $ttl = 60, $raw = FALSE){
        if ( ! self::$redis->hMSet($key, array('__ci_value' => serialize ($value)))){
            return FALSE;
        }elseif ($ttl){
            self::$redis->expireAt($key, time() + $ttl);
        }
        return TRUE;
    }


    /**
     * 加法,只能对数值型缓存使用,对 key 的值做++操作,并返回
     *
     * @param string $key
     * @param int $value
     */
    public function increment($key, $offset = 1){
        return self::$redis ? self::$redis->hIncrBy($key, 'data', $offset) : false;
    }

    /**
     * 뺄셈, 숫자 캐시에서만 사용할 수 있습니다. key 값 -- 을( 를) 실행한 후 반환합니다
     *
     * @param string $key
     * @param int $value
     */
    function decrement($key, $offset = 1){
        return self::$redis ? self::$redis->hIncrBy($key, 'data', -$offset) : false;
    }

    /**
     * 닫기rides
     *
     * @param string $key
     * @param int $value
     */
    public function close(){
        if (self::$redis) {
            return self::$redis->close ();
            self::$redis = null;
        }
    }

}

 

반응형