@@ -10,13 +10,23 @@ class Cache
1010 /** @var CacheElement[] */
1111 private $ _cache = [];
1212
13+ /**
14+ * Cache constructor.
15+ * @param $path
16+ * @param int $default_lifetime
17+ * @param bool $default_refresh
18+ */
1319 public function __construct ($ path , $ default_lifetime = 60 , $ default_refresh = false )
1420 {
1521 $ this ->_path = $ path ;
1622 $ this ->_default_lifetime = $ default_lifetime ;
1723 $ this ->_default_refresh = $ default_refresh ;
1824 }
1925
26+ /**
27+ * @param $key
28+ * @return bool
29+ */
2030 public function has ($ key )
2131 {
2232 if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
@@ -29,6 +39,11 @@ public function has($key)
2939 return false ;
3040 }
3141
42+ /**
43+ * @param $key
44+ * @param null $default_value
45+ * @return null|string
46+ */
3247 public function get ($ key , $ default_value = null )
3348 {
3449 if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
@@ -41,20 +56,31 @@ public function get($key, $default_value = null)
4156 return $ default_value ;
4257 }
4358
59+ /**
60+ * @param $key
61+ * @param $value
62+ * @param int|null $lifetime
63+ * @param bool|null $refresh
64+ * @return $this
65+ */
4466 public function set ($ key , $ value , $ lifetime = null , $ refresh = null )
4567 {
4668 if (is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
4769 $ this ->_cache [$ key ] = $ this ->newCacheElement ($ key , $ value , $ lifetime , $ refresh );
4870 } else {
4971 $ element ->update (
5072 $ value ,
51- $ lifetime ?: $ this -> _default_lifetime ,
73+ $ lifetime ?: $ element -> getLifetime () ,
5274 $ refresh ?: $ this ->_default_refresh
5375 );
5476 }
5577 return $ this ;
5678 }
5779
80+ /**
81+ * @param $key
82+ * @return $this
83+ */
5884 public function forget ($ key )
5985 {
6086 if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
@@ -64,6 +90,13 @@ public function forget($key)
6490 return $ this ;
6591 }
6692
93+ /**
94+ * @param $key
95+ * @param \Closure $call
96+ * @param int|null $lifetime
97+ * @param bool|null $refresh
98+ * @return $this
99+ */
67100 public function remember ($ key , \Closure $ call , $ lifetime = null , $ refresh = null )
68101 {
69102 if (!$ this ->has ($ key )) {
@@ -72,21 +105,46 @@ public function remember($key, \Closure $call, $lifetime = null, $refresh = null
72105 return $ this ;
73106 }
74107
75- public function flush ()
108+ /**
109+ * @param bool $keep_files
110+ * @return $this
111+ */
112+ public function flush ($ keep_files = false )
76113 {
77- array_map ("unlink " , glob ($ this ->_path . "/cache_*.json " ));
114+ if (!$ keep_files ) {
115+ array_map ("unlink " , glob ($ this ->path () . DIRECTORY_SEPARATOR . "cache_*.json " ));
116+ }
117+ $ this ->_cache = [];
118+ return $ this ;
78119 }
79120
121+ /**
122+ * @return $this
123+ */
80124 public function writeCache ()
81125 {
82126 foreach ($ this ->_cache as $ element ) {
83127 $ element ->writeToFs ();
84128 }
129+ return $ this ;
85130 }
86131
132+ /**
133+ * @return string
134+ */
135+ private function path ()
136+ {
137+ return rtrim ($ this ->_path , DIRECTORY_SEPARATOR );
138+ }
139+
140+ /**
141+ * @param $key
142+ * @return string
143+ */
87144 public function getPathForKey ($ key )
88145 {
89- return $ this ->_path . "/cache_ " . md5 ($ key ) . ".json " ;
146+
147+ return $ this ->path () . DIRECTORY_SEPARATOR . "cache_ " . md5 ($ key ) . ".json " ;
90148 }
91149
92150 /**
@@ -119,7 +177,7 @@ private function fetchCacheElement($key)
119177 {
120178 $ path = $ this ->getPathForKey ($ key );
121179 if (is_file ($ path )) {
122- $ cache = json_decode (file_get_contents ($ path ));
180+ $ cache = json_decode (file_get_contents ($ path ), true );
123181 $ element = new CacheElement (
124182 $ this ,
125183 $ key ,
0 commit comments