forked from xackery/kohana-phpredis
/
Redis.php
167 lines (144 loc) · 3.63 KB
/
Redis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Redis connection wrapper/helper.
*
* You may get a redis instance using `RedisLibrary::instance('name')` where
* name is the [config](redis/config) group.
*
* This class provides connection instance management via Redis Driver, as
* well as quoting, escaping and other related functions.
*
* @package Kohana/Redis
* @category Base
* @author Xackery
* @license http://kohanaphp.com/license
*/
abstract class Kohana_Redis {
/**
* @var string default instance name
*/
public static $default = 'default';
/**
* @var array Redis instances
*/
public static $instances = array();
/**
* Get a singleton Redis instance. If configuration is not specified,
* it will be loaded from the redis configuration file using the same
* group as the name.
*
* // Load the default redis
* $rd = RedisLibrary::instance();
*
* // Create a custom configured instance
* $rd = RedisLibrary::instance('custom', $config);
*
* @param string $name instance name
* @param array $config configuration parameters
* @return Redis
*/
public static function instance($name = NULL, array $config = NULL)
{
if ($name === NULL)
{
// Use the default instance name
$name = RedisLibrary::$default;
}
if ( ! isset(RedisLibrary::$instances[$name]))
{
if ($config === NULL)
{
// Load the configuration for this redis
$config = Kohana::$config->load('redis')->$name;
}
if ( ! isset($config['type']))
{
throw new Kohana_Exception('Redis type not defined in :name configuration',
array(':name' => $name));
}
// Set the driver class name
$driver = 'Redis_'.ucfirst($config['type']);
// Create the redis connection instance
$driver = new $driver($name, $config);
// Store the redis instance
RedisLibrary::$instances[$name] = $driver;
}
return RedisLibrary::$instances[$name];
}
/**
* @var string the last query executed
*/
public $last_query;
// Instance name
protected $_instance;
// Raw server connection
protected $_connection;
// Configuration array
protected $_config;
/**
* Stores the redis configuration locally and name the instance.
*
* [!!] This method cannot be accessed directly, you must use [Redis::instance].
*
* @return void
*/
public function __construct($name, array $config)
{
// Set the instance name
$this->_instance = $name;
// Store the config locally
$this->_config = $config;
}
/**
* Disconnect from redis when the object is destroyed.
*
* // Destroy the redis instance
* unset(RedisLibrary::instances[(string) $rd], $rd);
*
* [!!] Calling `unset($rd)` is not enough to destroy the redise, as it
* will still be stored in `RedisLibrary::$instances`.
*
* @return void
*/
public function __destruct()
{
$this->disconnect();
}
/**
* Returns the redis instance name.
*
* echo (string) $rd;
*
* @return string
*/
public function __toString()
{
return $this->_instance;
}
/**
* Connect to redis. This is called automatically when the first
* query is executed.
*
* $rd->connect();
*
* @throws Redis_Exception
* @return void
*/
abstract public function connect();
/**
* Disconnect from redis. This is called automatically by [RedisLibrary::__destruct].
* Clears the redis instance from [RedisLibrary::$instances].
*
* $rd->disconnect();
*
* @return boolean
*/
public function disconnect()
{
unset(RedisLibrary::$instances[$this->_instance]);
return TRUE;
}
public function expire($key, $seconds)
{
}
} // End Redis_Connection