|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Cache class |
|
4
|
|
|
* |
|
5
|
|
|
* @file Cache.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2017 Alexander Yancharuk |
|
11
|
|
|
* @date Птн Ноя 16 20:42:01 2012 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Cache; |
|
17
|
|
|
|
|
18
|
|
|
use Cache\Traits\CacheAdapterTrait; |
|
19
|
|
|
use Exception; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Cache |
|
23
|
|
|
* |
|
24
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Cache |
|
27
|
|
|
{ |
|
28
|
|
|
use CacheAdapterTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get data |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $key Key |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
* @throws Exception |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public static function get($key) |
|
39
|
|
|
{ |
|
40
|
2 |
|
return self::getAdapter()->get($key); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Save date |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $key Key |
|
47
|
|
|
* @param mixed $value Data |
|
48
|
|
|
* @param int $ttl Time to live |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
* @throws Exception |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public static function set($key, $value, $ttl = 0) |
|
54
|
|
|
{ |
|
55
|
4 |
|
return self::getAdapter()->set($key, $value, $ttl); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Save date if key not exists |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $key Key |
|
62
|
|
|
* @param mixed $value Data |
|
63
|
|
|
* @param int $ttl Time to live |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* @throws Exception |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public static function add($key, $value, $ttl = 0) |
|
69
|
|
|
{ |
|
70
|
4 |
|
return self::getAdapter()->add($key, $value, $ttl); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if data stored in cache |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key Key |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
* @throws Exception |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public static function has($key) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return self::getAdapter()->has($key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Delete data |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key Key |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
* @throws Exception |
|
93
|
|
|
*/ |
|
94
|
4 |
|
public static function del($key) |
|
95
|
|
|
{ |
|
96
|
4 |
|
return self::getAdapter()->del($key); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Method for deletion keys by template |
|
101
|
|
|
* |
|
102
|
|
|
* ATTENTION: if key contains spaces, for example 'THIS IS KEY::ID:50d98ld', |
|
103
|
|
|
* then in cache it will be saved as 'THIS_IS_KEY::ID:50d98ld'. So, template |
|
104
|
|
|
* for that key deletion must be look like - 'THIS_IS_KEY'. |
|
105
|
|
|
* Deletion can be made by substring, containing in keys. For example |
|
106
|
|
|
* '_KEY::ID'. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $tpl Substring containing in needed keys |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool |
|
111
|
|
|
* @throws Exception |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public static function delByTemplate($tpl) |
|
114
|
|
|
{ |
|
115
|
2 |
|
return self::getAdapter()->delByTemplate($tpl); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Cache cleanup |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
* @throws Exception |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public static function clear() |
|
125
|
|
|
{ |
|
126
|
2 |
|
return self::getAdapter()->clear(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Increment key value |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* @param int $offset |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
* @throws Exception |
|
137
|
|
|
*/ |
|
138
|
4 |
|
public static function increment($key, $offset = 1) |
|
139
|
|
|
{ |
|
140
|
4 |
|
return self::getAdapter()->increment($key, $offset); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Decrement key value |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $key |
|
147
|
|
|
* @param int $offset |
|
148
|
|
|
* |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
* @throws Exception |
|
151
|
|
|
*/ |
|
152
|
4 |
|
public static function decrement($key, $offset = 1) |
|
153
|
|
|
{ |
|
154
|
4 |
|
return self::getAdapter()->decrement($key, $offset); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|