Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
Real first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Oct 17, 2016
1 parent 8284852 commit 25589cd
Show file tree
Hide file tree
Showing 6 changed files with 541 additions and 2 deletions.
130 changes: 130 additions & 0 deletions Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,137 @@

namespace dcb9\redis;

use yii\di\Instance;

class Cache extends \yii\caching\Cache
{
/**
* @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
* This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
* redis connection as an application component.
* After the Cache object is created, if you want to change this property, you should only assign it
* with a Redis [[Connection]] object.
*/
public $redis = 'redis';

/**
* Initializes the redis Cache component.
* This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
* @throws \yii\base\InvalidConfigException if [[redis]] is invalid.
*/
public function init()
{
parent::init();
$this->redis = Instance::ensure($this->redis, Connection::className());
$this->redis->open();
}


/**
* @inheritdoc
*/
public function exists($key)
{
$key = $this->buildKey($key);

return (bool)$this->redis->exists($key);
}

/**
* @inheritdoc
*/
protected function getValue($key)
{
return $this->redis->get($key);
}

/**
* @inheritdoc
*/
protected function getValues($keys)
{
$response = $this->redis->mget($keys);
$result = [];
$i = 0;
foreach ($keys as $key) {
$result[$key] = $response[$i++];
}

return $result;
}

/**
* @inheritdoc
*/
protected function setValue($key, $value, $expire)
{
if ($expire == 0) {
return (bool)$this->redis->set($key, $value);
} else {
return (bool)$this->redis->setex($key, $expire, $value);
}
}

/**
* @inheritdoc
*/
protected function setValues($data, $expire)
{
$args = [];
foreach ($data as $key => $value) {
$args[] = $key;
$args[] = $value;
}
$failedKeys = [];
if ($expire == 0) {
$this->redis->mset($args);
} else {
$expire = (int)($expire * 1000);
$this->redis->multi();
$this->redis->mset($args);
$index = [];
foreach ($data as $key => $value) {
$this->redis->expire($key, $expire);
$index[] = $key;
}
$result = $this->redis->exec();
array_shift($result);
foreach ($result as $i => $r) {
if ($r != 1) {
$failedKeys[] = $index[$i];
}
}
}

return $failedKeys;
}


/**
* @inheritdoc
*/
protected function addValue($key, $value, $expire)
{
if ($expire == 0) {
return (bool)$this->redis->set($key, $value);
}

return (bool)$this->redis->setex($key, $expire, $value);
}

/**
* @inheritdoc
*/
protected function deleteValue($key)
{
return (bool)$this->redis->del($key);
}

/**
* @inheritdoc
*/
protected function flushValues()
{
return $this->redis->flushdb();
}
}
108 changes: 106 additions & 2 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,113 @@

namespace dcb9\redis;

use yii\base\Component;
use Redis;
use Yii;
use yii\base\Configurable;
use RedisException;

class Connection extends Component
/**
* Class Connection
* @package dcb9\redis
*/
class Connection extends Redis implements Configurable
{
/**
* @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'.
* If [[unixSocket]] is specified, hostname and port will be ignored.
*/
public $hostname = 'localhost';
/**
* @var integer the port to use for connecting to the redis server. Default port is 6379.
* If [[unixSocket]] is specified, hostname and port will be ignored.
*/
public $port = 6379;
/**
* @var string the unix socket path (e.g. `/var/run/redis/redis.sock`) to use for connecting to the redis server.
* This can be used instead of [[hostname]] and [[port]] to connect to the server using a unix socket.
* If a unix socket path is specified, [[hostname]] and [[port]] will be ignored.
*/
public $unixSocket;
/**
* @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send.
* See http://redis.io/commands/auth
*/
public $password;
/**
* @var integer the redis database to use. This is an integer value starting from 0. Defaults to 0.
*/
public $database = 0;
/**
* @var float value in seconds (optional, default is 0.0 meaning unlimited)
*/
public $connectionTimeout = 0.0;

/**
* Constructor.
* The default implementation does two things:
*
* - Initializes the object with the given configuration `$config`.
* - Call [[init()]].
*
* If this method is overridden in a child class, it is recommended that
*
* - the last parameter of the constructor is a configuration array, like `$config` here.
* - call the parent implementation at the end of the constructor.
*
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
}

/**
* Returns the fully qualified name of this class.
* @return string the fully qualified name of this class.
*/
public static function className()
{
return get_called_class();
}

/**
* Establishes a DB connection.
* It does nothing if a DB connection has already been established.
* @throws RedisException if connection fails
*/
public function open()
{
if ($this->unixSocket !== null) {
$isConnected = $this->connect($this->unixSocket);
} else {
$isConnected = $this->connect($this->hostname, $this->port, $this->connectionTimeout);
}

if ($isConnected === false) {
throw new RedisException('Connect to redis server error.');
}

if ($this->password !== null) {
$this->auth($this->password);
}

if ($this->database !== null) {
$this->select($this->database);
}
}

/**
* @return bool
*/
public function ping()
{
return parent::ping() === '+PONG';
}

public function flushdb()
{
return parent::flushDB();
}
}
Loading

0 comments on commit 25589cd

Please sign in to comment.