forked from willfarrell/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.redis.php
130 lines (110 loc) · 2.93 KB
/
class.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
<?php
/**
* redis Database Class
*
* PHP Version 5
*
* @category N/A
* @package N/A
* @author will Farrell <[email protected]>
* @copyright 2001 - 2012 willFarrell.ca
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version GIT: <git_id>
* @link http://willFarrell.ca
*/
require_once 'Predis/Autoloader.php'; // https://github.com/nrk/predis
Predis\Autoloader::register();
/**
* Database
*
* @category N/A
* @package N/A
* @author Original Author <[email protected]>
* @author Another Author <[email protected]>
* @copyright 2001 - 2011 willFarrell.ca
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Release: <package_version>
* @link http://willFarrell.ca
*/
class Redis extends MySQL
{
var $connection_redis; //The redis database connection
/**
* Constructor
* prefix in teh form of 'prefix:'
*/
function __construct($prefix = '')
{
$this->connection_redis = new Predis\Client(
array(
'host' => '127.0.0.1',
'port' => 6379
),
array('prefix' => $prefix)
);
}
/**
* Destructor
*/
function __destruct()
{
}
//-- Generic --//
// http://redis.io/commands#generic
function del($key)
{
return $this->connection_redis->del($key);
}
//-- String --//
// http://redis.io/commands#string
/*function mget($key_array = array())
{
return json_decode($this->connection_redis->get($key));
}*/
function get($key)
{
return json_decode($this->connection_redis->get($key));
}
/*function mset($key_value_array = array())
{
return $this->connection_redis->set($key, json_encode($value));
}*/
function set($key, $value)
{
return $this->connection_redis->set($key, json_encode($value));
}
//-- Hash --//
// http://redis.io/commands#hash
function hgetall($hash_key)
{
$object = $this->connection_redis->hgetall($hash_key);
foreach ($object as $key => $value) {
$object[$key] = json_decode($value);
}
return $object;
}
/*function hmget($hash_key, $field_array = array())
{
$object = $this->connection_redis->hmget($hash_key);
foreach ($object as $key => $value) {
$object[$key] = json_decode($value);
}
return $object;
}*/
function hget($hash_key, $field)
{
return json_decode($this->connection_redis->hget($hash_key, $field));
}
function hmset($hash_key, $field_value_array = array())
{
foreach ($field_value_array as $key => $value) {
$this->hset($hash_key, $key, json_decode($value));
}
return;
}
function hset($hash_key, $field, $value)
{
return $this->connection_redis->hset($hash_key, $field, json_encode($value));
}
};
?>