1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* For use please add the following code to the end of the config.core.php file: |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* |
16
|
|
|
* 'cache' => array( |
17
|
|
|
* |
18
|
|
|
* 'storage' => 'memcached', |
19
|
|
|
* 'servers' => array( |
20
|
|
|
* array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), |
21
|
|
|
* ), |
22
|
|
|
* ), |
23
|
|
|
* ), |
24
|
|
|
* |
25
|
|
|
* </code> |
26
|
|
|
* |
27
|
|
|
* Memcached usage: |
28
|
|
|
* |
29
|
|
|
* <code> |
30
|
|
|
* // Get the Memcache connection and get an item from the cache |
31
|
|
|
* $name = Memcached::connection()->get('name'); |
32
|
|
|
* |
33
|
|
|
* // Get the Memcache connection and place an item in the cache |
34
|
|
|
* Memcached::connection()->set('name', 'Robin'); |
35
|
|
|
* |
36
|
|
|
* // Get an item from the Memcache instance |
37
|
|
|
* $name = Memcached::get('name'); |
38
|
|
|
* |
39
|
|
|
* // Store data on the Memcache server |
40
|
|
|
* Memcached::set('name', 'Robin'); |
41
|
|
|
* </code> |
42
|
|
|
* |
43
|
|
|
* @package Pimf |
44
|
|
|
* @author Gjero Krsteski <[email protected]> |
45
|
|
|
* |
46
|
|
|
* @method get($key) |
47
|
|
|
* @method put($key, $value, $expiration) |
48
|
|
|
* @method forget($key); |
49
|
|
|
*/ |
50
|
|
|
class Memcached |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* @var \Memcached |
54
|
|
|
*/ |
55
|
|
|
protected static $connection; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \Memcached |
59
|
|
|
*/ |
60
|
|
|
public static function connection() |
61
|
|
|
{ |
62
|
|
|
if (static::$connection === null) { |
63
|
|
|
static::$connection = static::connect( |
64
|
|
|
Config::get('cache.memcached.servers') |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return static::$connection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a new Memcached connection instance. |
73
|
|
|
* |
74
|
|
|
* @param array $servers |
75
|
|
|
* @param null $memcache |
76
|
|
|
* |
77
|
|
|
* @return \Memcached|null |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
*/ |
80
|
|
|
protected static function connect(array $servers, $memcache = null) |
81
|
|
|
{ |
82
|
|
|
if (!$memcache) { |
83
|
|
|
$memcache = new \Memcached(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($servers as $server) { |
87
|
|
|
$memcache->addServer($server['host'], $server['port'], $server['weight']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($memcache->getVersion() === false) { |
91
|
|
|
throw new \RuntimeException('could not establish memcached connection!'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $memcache; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Dynamically pass all other method calls to the Memcache instance. |
99
|
|
|
* |
100
|
|
|
* @param $method |
101
|
|
|
* @param $parameters |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public static function __callStatic($method, $parameters) |
106
|
|
|
{ |
107
|
|
|
return call_user_func_array(array(static::connection(), $method), $parameters); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|