Home>

redis cache class file location:

"ci \ system \ libraries \ cache \ drivers \ cache_redis.php"

<?php
/**
 * codeigniter
 *
 * an open source application development framework for php 5.2.4 or newer
 *
 * notice of license
 *
 * licensed under the open software license version 3.0
 *
 * this source file is subject to the open software license (osl 3.0) that is
 * bundled with this package in the files license.txt/license.rst. it is
 * also available through the world wide web at this url:
 * http://opensource.org/licenses/osl-3.0
 * if you did not receive a copy of the license and are unable to obtain it
 * through the world wide web, please send an email to
 * [email protected] so we can send you a copy immediately.
 *
 * @package codeigniter
 * @author ellislab dev team
 * @copyright copyright (c) 2008-2014, ellislab, inc. (http://ellislab.com/)
 * @license http://opensource.org/licenses/osl-3.0 open software license (osl 3.0)
 * @link http://codeigniter.com
 * @since version 3.0
 * @filesource
 * /
defined ("basepath") or exit ("no direct script access allowed");
/**
 * codeigniter redis caching class
 *
 * @package codeigniter
 * @subpackage libraries
 * @category core
 * @author anton lindqvist<[email protected]>
 * @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" =>null,    "port" =>6379,    "timeout" =>0
    * /
  );
  /**
   * redis connection
   *
   * @var redis
   * /
  protected $_redis;
  /**
   * get cache
   *
   * @param string like * $key *
   * @return array (hash)
   * /
  public function keys ($key)
  {
    return $this->_ redis->keys ($key);
  }
  /**
   * get cache
   *
   * @param string cache id
   * @return mixed
   * /
  public function get ($key)
  {
    return $this->_redis->get ($key);
  }
  /**
   * mget cache
   *
   * @param array cache id array
   * @return mixed
   * /
  public function mget ($keys)
  {
    return $this->_ redis->mget ($keys);
  }
  /**
   * 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)
  {
    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)
  {
    return ($this->_redis->delete ($key) === 1);
  }
  /**
   * hincrby a raw value
   *
   * @param string $id cache id
   * @param string $field cache id
   * @param int $offset step/value to add
   * @return mixed new value on success or false on failure
   * /
  public function hincrby ($key, $field, $value=1)
  {
    return $this->_ redis->hincrby ($key, $field, $value);
  }
  /**
   * hincrbyfloat a raw value
   *
   * @param string $id cache id
   * @param string $field cache id
   * @param int $offset step/value to add
   * @return mixed new value on success or false on failure
   * /
  public function hincrbyfloat ($key, $field, $value=1)
  {
    return $this->_ redis->hincrbyfloat ($key, $field, $value);
  }
  /**
   * lpush a raw value
   *
   * @param string $key cache id
   * @param string $value value
   * @return mixed new value on success or false on failure
   * /
  public function lpush ($key, $value)
  {
    return $this->_ redis->lpush ($key, $value);
  }
   /**
   * rpush a raw value
   *
   * @param string $key cache id
   * @param string $value value
   * @return mixed new value on success or false on failure
   * /
  public function rpush ($key, $value)
  {
    return $this->_ redis->rpush ($key, $value);
  }
  /**
   * rpop a raw value
   *
   * @param string $key cache id
   * @param string $value value
   * @return mixed new value on success or false on failure
   * /
  public function rpop ($key)
  {
    return $this->_redis->rpop ($key);
  }
   /**
   * brpop a raw value
   *
   * @param string $key cache id
   * @param string $ontime blocking wait time
   * @return mixed new value on success or false on failure
   * /
  public function brpop ($key, $ontime=0)
  {
    return $this->_redis->brpop ($key, $ontime);
  }
  /**
   * llen a raw value
   *
   * @param string $key cache id
   * @return mixed value on success or false on failure
   * /
  public function llen ($key)
  {
    return $this->_redis->llen ($key);
  }
  /**
   * 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->exists ($id)
      ?$this-&_ redis->incr ($id, $offset)
      :false;
  }
  /**
   * incrby a raw value
   *
   * @param string $key cache id
   * @param int $offset step/value to add
   * @return mixed new value on success or false on failure
   * /
  public function incrby ($key, $value=1)
  {
    return $this->_ redis->incrby ($key, $value);
  }
  /**
   * set a value expire time
   *
   * @param string $key cache id
   * @param int $seconds expire seconds
   * @return mixed new value on success or false on failure
   * /
  public function expire ($key, $seconds)
  {
    return $this->_ redis->expire ($key, $seconds);
  }
  /**
   * 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 hset ($alias, $key, $value)
  {
    return $this->_ redis->hset ($alias, $key, $value);
  }
  /**
   * 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 hget ($alias, $key)
  {
    return $this->_ redis->hget ($alias, $key);
  }
  /**
   * increment a raw value
   *
   * @param string $id cache id
   * @return mixed new value on success or false on failure
   * /
  public function hkeys ($alias)
  {
    return $this->_ redis->hkeys ($alias);
  }
  /**
   * 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 hgetall ($alias)
  {
    return $this->_ redis->hgetall ($alias);
  }
  /**
   * 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 hmget ($alias, $key)
  {
    return $this->_ redis->hmget ($alias, $key);
  }
  /**
   * del a key 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 hdel ($alias, $key)
  {
    return $this->_ redis->hdel ($alias, $key);
  }
  /**
   * del a key value
   *
   * @param string $id cache id
   * @return mixed new value on success or false on failure
   * /
  public function hvals ($alias)
  {
    return $this->_redis->hvals ($alias);
  }
  /**
   * 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 hmset ($alias, $array)
  {
    return $this->_ redis->hmset ($alias, $array);
  }
  /**
   * 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->exists ($id)
      ?$this-&_ redis->decr ($id, $offset)
      :false;
  }
  /**
   * 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"))
    {
      return $this->_setup_redis ();
    }
    else
    {
      log_message ("debug", "the redis extension must be loaded to use redis cache.");
      return false;
    }
  }
  /**
   * 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);
    $config =! empty ($config ["redis"])?$config ["redis"]:$config;
    $this->_redis=new redis ();
    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->
      return false;
    }
    if (isset ($config ["password"]))
    {
      $this->_redis->auth ($config ["password"]);
    }
    return true;
  }
  /**
   * class destructor
   *
   * closes the connection to redis if present.
   *
   * @return void
   * /
  public function __destruct ()
  {
    if ($this->_redis)
    {
      $this->_redis->close ();
    }
  }
}
/* end of file cache_redis.php * /
/* location:./system/libraries/cache/drivers/cache_redis.php * /
php
  • Previous Difference between JSONparse () and eval () parsing json in JavaScript
  • Next 67 Manipulating Data in ASPNET 20: Using JOINs in TableAdapters