-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
139 lines (108 loc) · 3.74 KB
/
Cache.php
File metadata and controls
139 lines (108 loc) · 3.74 KB
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
<?php declare(strict_types=1);
/**
* @file Cache.php
* @author Gabriele Tozzi <gabriele@tozzi.eu>
* @package DoPhp
* @brief Classes for caching
*/
namespace dophp\cache;
require_once 'Exceptions.php';
/**
* Wrapper over \Memcache
*
* prepends a prefix to every key, accept array prefixes
*
* @see \Memcache
*/
class Memcache {
/** The prefix banner */
const BANNER = 'DoPhp';
/** The separator for prefix parts */
const SEP = '::';
/** The key prefix */
protected $_prefix;
/** The wrapped cache object */
/**
* Constructor for the class
*
* @param $prefix str: The prefix to prepend to every key; if null,
* try to generate an unique one for this install
* @param $prepend bool: if true, prepends const DoPhp banner to prefix
*/
public function __construct(string $prefix=null, bool $banner=true) {
$this->_cache = new \Memcache();
$this->_prefix = $prefix ?? hash('crc32', __FILE__) . self::SEP;
if( $banner )
$this->_prefix = self::BANNER . self::SEP . $this->_prefix;
}
public function addServer( string $host, int $port=11211, bool $persistent=true,
int $weight=null, int $timeout=null, int $retry_interval=null,
bool $status=true, callable $failure_callback=null, int $timeoutms=null ) {
return $this->_cache->assServer($host, $port, $persistent, $weight,
$timeout, $retry_interval, $status, $failure_callback, $timeoutms);
}
public function connect( string $host, int $port=null, int $timeout=1 ) {
return $this->_cache->connect($host, $port, $timeout);
}
public function close() {
return $this->_cache->close();
}
public function flush() {
return $this->_cache->flush();
}
public function getStats(string $type, int $slabid=null, int $limit=100) {
return $this->_cache->getStats($type, $slabid, $limit);
}
public function getExtendedStats(string $type, int $slabid=null, int $limit=100) {
return $this->_cache->getExtendedStats($type, $slabid, $limit);
}
public function getServerStatus(string $host, int $port=11211) {
return $this->_cache->getServerStatus($host, $port);
}
public function setCompressThreshold(int $threshold, float $min_savings=0.2) {
return $this->_cache->setCompressThreshold($threshold, $min_savings);
}
public function setServerParams(string $host, int $port=11211, int $timeout=1,
int $retry_interval=15, bool $status=true, callable $failure_callback=null ) {
return $this->_cache->setServerParams($host, $port, $timeout,
$retry_interval, $status, $failure_callback);
}
/**
* Normalized a key into a full standard key string, prepends prefix
*
* @param $key mixed: string or array; if array, values are joined using self::SEP
*/
protected function _normKey($key) {
if( is_array($key) )
$key = implode(self::SEP, $key);
return $this->_prefix . $key;
}
public function get($key, int &$flags=0) {
$key = $this->_normKey($key);
return $this->_cache->get($key, $flags);
}
public function add($key, $var, int $flag=0, int $expire=0) {
$key = $this->_normKey($key);
return $this->_cache->add($key, $var, $flag, $expire);
}
public function set($key, $var, int $flag=0, int $expire=0) {
$key = $this->_normKey($key);
return $this->_cache->set($key, $var, $flag, $expire);
}
public function replace($key, $var, int $flag=0, int $expire=0) {
$key = $this->_normKey($key);
return $this->_cache->replace($key, $var, $flag, $expire);
}
public function delete($key, int $timeout=0) {
$key = $this->_normKey($key);
return $this->_cache->delete($key, $timeout);
}
public function decrement($key, int $value=1) {
$key = $this->_normKey($key);
return $this->_cache->decrement($key, $value);
}
public function increment($key, int $value=1) {
$key = $this->_normKey($key);
return $this->_cache->increment($key, $value);
}
}