1+ <?php
2+
3+ /**
4+ * CodeMommy CachePHP
5+ * @author Candison November <www.kandisheng.com>
6+ */
7+
8+ namespace CodeMommy \CachePHP ;
9+
10+ use Redis ;
11+
12+ /**
13+ * Class Cache
14+ * @package CodeMommy\CachePHP
15+ */
16+ class Cache
17+ {
18+ const TIMEOUT_ONE_SECOND = 1 ;
19+ const TIMEOUT_ONE_MINUTE = 1 * 60 ;
20+ const TIMEOUT_ONE_HOUR = 1 * 60 * 60 ;
21+ const TIMEOUT_ONE_DAY = 1 * 60 * 60 * 24 ;
22+ const TIMEOUT_ONE_MONTH = 1 * 60 * 60 * 24 * 30 ;
23+ const TIMEOUT_ONE_QUARTER = 1 * 60 * 60 * 24 * 90 ;
24+ const TIMEOUT_ONE_YEAR = 1 * 60 * 60 * 24 * 365 ;
25+ const TIMEOUT_ONE_CENTURY = 1 * 60 * 60 * 24 * 365 * 100 ;
26+ const TIMEOUT_ONE_LIFE = 1 * 60 * 60 * 24 * 365 * 100 ;
27+
28+ const SERVER_LOCALHOST = 'localhost ' ;
29+
30+ const DRIVER_REDIS = 'Redis ' ;
31+ const DRIVER_MEMCACHED = 'Memcached ' ;
32+ const DRIVER_APC = 'APC ' ;
33+ const DRIVER_XCACHE = 'XCache ' ;
34+
35+ const PORT_REDIS = 6379 ;
36+ const PORT_MEMCACHED = 11211 ;
37+
38+ private $ config = null ;
39+ private $ driver = null ;
40+ private $ prefix = null ;
41+
42+ /**
43+ * Cache constructor.
44+ *
45+ * @param null $config
46+ */
47+ public function __construct ($ config = null )
48+ {
49+ $ this ->config = array ();
50+ if (is_array ($ config )) {
51+ $ this ->config = $ config ;
52+ }
53+ $ this ->prefix = isset ($ this ->config ['prefix ' ]) ? strval ($ this ->config ['prefix ' ]) : '' ;
54+ }
55+
56+ /**
57+ * Get Key
58+ *
59+ * @param $key
60+ *
61+ * @return string
62+ */
63+ private function getKey ($ key )
64+ {
65+ return $ this ->prefix . $ key ;
66+ }
67+
68+ /**
69+ * Is Driver
70+ *
71+ * @param $driver
72+ *
73+ * @return bool
74+ */
75+ private function isDriver ($ driver )
76+ {
77+ if ($ this ->config ['driver ' ] == $ driver ) {
78+ return true ;
79+ }
80+ return false ;
81+ }
82+
83+ /**
84+ * Start Driver
85+ */
86+ private function startDriver ()
87+ {
88+ if ($ this ->driver == null ) {
89+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
90+ $ this ->driver = new Redis ();
91+ $ this ->driver ->connect ($ this ->config ['server ' ], $ this ->config ['port ' ]);
92+ if (isset ($ this ->config ['password ' ])) {
93+ $ this ->driver ->auth ($ this ->config ['password ' ]);
94+ }
95+ if (isset ($ this ->config ['database ' ])) {
96+ $ this ->driver ->select ($ this ->config ['database ' ]);
97+ }
98+ }
99+ }
100+ }
101+
102+ /**
103+ * Close Driver
104+ * @return null
105+ */
106+ public function closeDriver ()
107+ {
108+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
109+ return $ this ->driver ->close ();
110+ }
111+ return false ;
112+ }
113+
114+ /**
115+ * Get Driver
116+ * @return null
117+ */
118+ public function getDriver ()
119+ {
120+ $ this ->startDriver ();
121+ return $ this ->driver ;
122+ }
123+
124+ /**
125+ * Get Data
126+ *
127+ * @param $key
128+ * @param $timeout
129+ * @param $function
130+ *
131+ * @return mixed
132+ */
133+ public function getData ($ key , $ timeout , $ function )
134+ {
135+ $ key = $ this ->getKey ($ key );
136+ if ($ this ->isExist ($ key )) {
137+ return unserialize ($ this ->readValue ($ key ));
138+ }
139+ $ value = $ function ();
140+ $ timeout = intval ($ timeout );
141+ $ this ->writeValue ($ key , serialize ($ value ), $ timeout );
142+ return $ value ;
143+ }
144+
145+ /**
146+ * Is Exist
147+ *
148+ * @param $key
149+ *
150+ * @return bool
151+ */
152+ public function isExist ($ key )
153+ {
154+ $ key = $ this ->getKey ($ key );
155+ $ this ->startDriver ();
156+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
157+ return $ this ->driver ->exists ($ key );
158+ }
159+ return false ;
160+ }
161+
162+ /**
163+ * Delete
164+ *
165+ * @param $key
166+ *
167+ * @return bool
168+ */
169+ public function delete ($ key )
170+ {
171+ $ key = $ this ->getKey ($ key );
172+ $ this ->startDriver ();
173+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
174+ $ result = $ this ->driver ->delete ($ key );
175+ if ($ result > 0 ) {
176+ return true ;
177+ }
178+ return false ;
179+ }
180+ return false ;
181+ }
182+
183+ /**
184+ * Write Value
185+ *
186+ * @param $key
187+ * @param $value
188+ * @param int $timeout
189+ *
190+ * @return bool
191+ */
192+ public function writeValue ($ key , $ value , $ timeout = 0 )
193+ {
194+ $ key = $ this ->getKey ($ key );
195+ $ this ->startDriver ();
196+ $ timeout = intval ($ timeout );
197+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
198+ $ this ->driver ->set ($ key , $ value , $ timeout );
199+ return true ;
200+ }
201+ return false ;
202+ }
203+
204+ /**
205+ * Read Value
206+ *
207+ * @param $key
208+ * @param null $default
209+ *
210+ * @return null
211+ */
212+ public function readValue ($ key , $ default = null )
213+ {
214+ $ this ->startDriver ();
215+ if ($ this ->isDriver (self ::DRIVER_REDIS )) {
216+ if (!$ this ->isExist ($ key )) {
217+ return $ default ;
218+ }
219+ $ key = $ this ->getKey ($ key );
220+ return $ this ->driver ->get ($ key );
221+ }
222+ return $ default ;
223+ }
224+ }
0 commit comments