1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Cache; |
9
|
|
|
use |
10
|
|
|
cs\Cache; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for simplified work with cache, when using common prefix |
14
|
|
|
*/ |
15
|
|
|
class Prefix { |
16
|
|
|
protected $prefix; |
17
|
|
|
/** |
18
|
|
|
* Initialization with some prefix |
19
|
|
|
* |
20
|
|
|
* @param string $prefix |
21
|
|
|
*/ |
22
|
16 |
|
function __construct ($prefix) { |
23
|
16 |
|
$this->prefix = $prefix; |
24
|
16 |
|
} |
25
|
|
|
/** |
26
|
|
|
* Get item from cache |
27
|
|
|
* |
28
|
|
|
* If item not found and $callback parameter specified - closure must return value for item. This value will be set for current item, and returned. |
29
|
|
|
* |
30
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
31
|
|
|
* @param callable|null $callback |
32
|
|
|
* |
33
|
|
|
* @return false|mixed Returns item on success of <b>false</b> on failure |
34
|
|
|
*/ |
35
|
16 |
|
function get ($item, $callback = null) { |
36
|
16 |
|
return Cache::instance()->get("$this->prefix/$item", $callback); |
37
|
|
|
} |
38
|
|
|
/** |
39
|
|
|
* Put or change data of cache item |
40
|
|
|
* |
41
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
42
|
|
|
* @param mixed $data |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
2 |
|
function set ($item, $data) { |
47
|
2 |
|
return Cache::instance()->set("$this->prefix/$item", $data); |
48
|
|
|
} |
49
|
|
|
/** |
50
|
|
|
* Delete item from cache |
51
|
|
|
* |
52
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
10 |
|
function del ($item) { |
57
|
10 |
|
return Cache::instance()->del("$this->prefix/$item"); |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* Get item from cache |
61
|
|
|
* |
62
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
63
|
|
|
* |
64
|
|
|
* @return false|mixed Returns item on success of <b>false</b> on failure |
65
|
|
|
*/ |
66
|
2 |
|
function __get ($item) { |
67
|
2 |
|
return $this->get($item); |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* Put or change data of cache item |
71
|
|
|
* |
72
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
73
|
|
|
* @param mixed $data |
74
|
|
|
*/ |
75
|
2 |
|
function __set ($item, $data) { |
76
|
2 |
|
$this->set($item, $data); |
77
|
2 |
|
} |
78
|
|
|
/** |
79
|
|
|
* Delete item from cache |
80
|
|
|
* |
81
|
|
|
* @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
82
|
|
|
*/ |
83
|
10 |
|
function __unset ($item) { |
84
|
10 |
|
$this->del($item); |
85
|
10 |
|
} |
86
|
|
|
} |
87
|
|
|
|