1+ <?php
2+ namespace PhpMvc ;
3+
4+ /**
5+ * Represents the cache.
6+ */
7+ final class Cache {
8+
9+ /**
10+ * Gets or sets cache provider.
11+ *
12+ * @var CacheProvider
13+ */
14+ private $ provider = null ;
15+
16+ /**
17+ * Initializes a new instance of the Cache class.
18+ *
19+ * @param CacheProvider $provider The cache provider instance.
20+ */
21+ public function __construct ($ provider ) {
22+ if (!isset ($ provider ) || !$ provider instanceof CacheProvider) {
23+ throw new \Exception ('The type of $provider must not be null and must implement the "\PhpMvc\CacheProvider". ' );
24+ }
25+
26+ $ this ->provider = $ provider ;
27+ }
28+
29+ /**
30+ * Inserts a cache entry into the cache.
31+ *
32+ * @param string $key A unique identifier for the cache entry.
33+ * @param mixed|callback $value The value to insert.
34+ * @param int $duration The duration of storage the cache entry (in seconds).
35+ * @param string $regionName A named region in the cache to which the cache entry can be added.
36+ *
37+ * @return void
38+ */
39+ public function add ($ key , $ value , $ duration , $ regionName = null ) {
40+ $ this ->provider ->add ($ key , $ value , $ duration , $ regionName );
41+ }
42+
43+ /**
44+ * Removes all cache entries.
45+ *
46+ * @param string $regionName A name of region.
47+ *
48+ * @return int Number of deleted records.
49+ */
50+ public function clear ($ regionName = null ) {
51+ return $ this ->provider ->clear ($ regionName );
52+ }
53+
54+ /**
55+ * Checks whether the cache entry already exists in the cache.
56+ *
57+ * @param string $key A unique identifier for the cache entry.
58+ * @param string $regionName A name of region.
59+ *
60+ * @return bool
61+ */
62+ public function contains ($ key , $ regionName = null ) {
63+ return $ this ->provider ->contains ($ key , $ regionName );
64+ }
65+
66+ /**
67+ * Gets the specified cache entry from the cache as an object.
68+ *
69+ * @param string $key A unique identifier for the cache entry.
70+ * @param string $regionName A name of region.
71+ *
72+ * @return mixed
73+ */
74+ public function get ($ key , $ regionName = null ) {
75+ return $ this ->provider ->get ($ key , $ regionName );
76+ }
77+
78+ /**
79+ * Gets a cache entry, or adds it if there is no cache entry.
80+ *
81+ * @param string $key A unique identifier for the cache entry.
82+ * @param mixed|callback $value The value to insert.
83+ * @param int $duration The duration of storage the cache entry (in seconds).
84+ * @param string $regionName A name of region.
85+ *
86+ * @return mixed
87+ */
88+ public function getOrAdd ($ key , $ value , $ duration , $ regionName = null ) {
89+ return $ this ->provider ->getOrAdd ($ key , $ value , $ duration , $ regionName );
90+ }
91+
92+ /**
93+ * Removes the cache entry from the cache.
94+ *
95+ * @param string $key A unique identifier for the cache entry.
96+ * @param string $regionName A name of region.
97+ *
98+ * @return mixed An object that represents the value of the removed cache entry that was specified by the key,
99+ * or null if the specified entry was not found.
100+ */
101+ public function remove ($ key , $ regionName = null ) {
102+ return $ this ->provider ->remove ($ key , $ regionName );
103+ }
104+
105+ }
0 commit comments