1+ <?php namespace Dtkahl \FileCache ;
2+
3+ class Cache
4+ {
5+
6+ private $ _path ;
7+ private $ _default_lifetime ;
8+ private $ _default_refresh ;
9+
10+ /** @var CacheElement[] */
11+ private $ _cache = [];
12+
13+ public function __construct ($ path , $ default_lifetime = 60 , $ default_refresh = false )
14+ {
15+ $ this ->_path = $ path ;
16+ $ this ->_default_lifetime = $ default_lifetime ;
17+ $ this ->_default_refresh = $ default_refresh ;
18+ }
19+
20+ public function has ($ key )
21+ {
22+ if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
23+ if ($ element ->isAlive ()) {
24+ return true ;
25+ }else {
26+ $ this ->forget ($ key );
27+ }
28+ }
29+ return false ;
30+ }
31+
32+ public function get ($ key , $ default_value = null )
33+ {
34+ if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
35+ if ($ element ->isAlive ()) {
36+ return $ element ->getValue ();
37+ }else {
38+ $ this ->forget ($ key );
39+ }
40+ }
41+ return $ default_value ;
42+ }
43+
44+ public function set ($ key , $ value , $ lifetime = null , $ refresh = null )
45+ {
46+ if (is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
47+ $ this ->_cache [$ key ] = $ this ->newCacheElement ($ key , $ value , $ lifetime , $ refresh );
48+ } else {
49+ $ element ->update (
50+ $ value ,
51+ $ lifetime ?: $ this ->_default_lifetime ,
52+ $ refresh ?: $ this ->_default_refresh
53+ );
54+ }
55+ return $ this ;
56+ }
57+
58+ public function forget ($ key )
59+ {
60+ if (!is_null ($ element = $ this ->getOrFetchCacheElement ($ key ))) {
61+ $ element ->removeFromFs ();
62+ unset($ this ->_cache [$ key ]);
63+ }
64+ return $ this ;
65+ }
66+
67+ public function remember ($ key , \Closure $ call , $ lifetime = null , $ refresh = null )
68+ {
69+ if (!$ this ->has ($ key )) {
70+ $ this ->_cache [$ key ] = $ this ->newCacheElement ($ key , $ call (), $ lifetime , $ refresh );
71+ }
72+ return $ this ;
73+ }
74+
75+ public function flush ()
76+ {
77+ array_map ("unlink " , glob ($ this ->_path . "/cache_*.json " ));
78+ }
79+
80+ public function writeCache ()
81+ {
82+ foreach ($ this ->_cache as $ element ) {
83+ $ element ->writeToFs ();
84+ }
85+ }
86+
87+ public function getPathForKey ($ key )
88+ {
89+ return $ this ->_path . "/cache_ " . md5 ($ key ) . ".json " ;
90+ }
91+
92+ /**
93+ * @param $key
94+ * @return CacheElement|null
95+ */
96+ private function getOrFetchCacheElement ($ key )
97+ {
98+ $ element = $ this ->getCacheElement ($ key );
99+ if (is_null ($ element ) && !is_null ($ element = $ this ->fetchCacheElement ($ key ))) {
100+ $ this ->_cache [$ key ] = $ element ;
101+ }
102+ return $ element ;
103+ }
104+
105+ /**
106+ * @param $key
107+ * @return CacheElement|null
108+ */
109+ private function getCacheElement ($ key )
110+ {
111+ return array_key_exists ($ key , $ this ->_cache ) ? $ this ->_cache [$ key ] : null ;
112+ }
113+
114+ /**
115+ * @param $key
116+ * @return CacheElement|null
117+ */
118+ private function fetchCacheElement ($ key )
119+ {
120+ $ path = $ this ->getPathForKey ($ key );
121+ if (is_file ($ path )) {
122+ $ cache = json_decode (file_get_contents ($ path ));
123+ $ element = new CacheElement (
124+ $ this ,
125+ $ key ,
126+ $ cache ["value " ],
127+ $ cache ["lifetime " ],
128+ $ cache ["start " ],
129+ $ cache ["refresh " ]
130+ );
131+ $ this ->_cache [$ key ] = $ element ;
132+ return $ element ;
133+ }
134+ return null ;
135+ }
136+
137+ /**
138+ * @param $key
139+ * @param $value
140+ * @param int|null $lifetime
141+ * @param int|null $refresh
142+ * @return CacheElement
143+ */
144+ private function newCacheElement ($ key , $ value , $ lifetime = null , $ refresh = null )
145+ {
146+ return new CacheElement (
147+ $ this ,
148+ $ key ,
149+ $ value ,
150+ $ lifetime ?: $ this ->_default_lifetime ,
151+ null ,
152+ $ refresh ?: $ this ->_default_refresh
153+ );
154+ }
155+
156+ }
0 commit comments