-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tag.php
91 lines (80 loc) · 1.36 KB
/
Tag.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
<?php
namespace TaggedCache;
/**
* Class Tag
* @package TaggedCache
*/
class Tag
{
private $_tag;
private static $_values = array();
public function __construct($tag = null)
{
$this->setTag($tag);
}
/**
* Set name of tag
* @param mixed $this
*/
public function setTag($tag = null)
{
if (empty($tag))
{
$tag = '@' . get_class($this);
}
if ('@' !== $tag{0})
{
$tag = '@' . get_class($this) . '::' . $tag;
}
$this->_tag = $tag;
return $this;
}
/**
* Return name of tag
* @return mixed
*/
public function getTag()
{
return $this->_tag;
}
/**
* Return current version of tag
* @return mixed
*/
public function get()
{
if (!isset(self::$_values[$this->getTag()]))
return false;
return self::$_values[$this->getTag()];
}
/**
* Set current tag version
* @param mixed $value
* @return $this
*/
public function set($value)
{
self::$_values[$this->getTag()] = $value;
return $this;
}
/**
* Save current tag version into cache
* @return $this
*/
public function save()
{
if (false !== ($value = $this->get()))
\Yii::app()->cache->set($this->getTag(), $value, /* forever */ 0);
return $this;
}
/**
* Remove tag value from cache
* @return $this
*/
public function delete()
{
unset(self::$_values[$this->getTag()]);
\Yii::app()->cache->delete($this->getTag());
return $this;
}
}