1515 */
1616class Cache
1717{
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 ;
18+ const TIMEOUT_ONE_SECOND = 1 ; // 1
19+ const TIMEOUT_ONE_MINUTE = 60 ; // 1 * 60
20+ const TIMEOUT_ONE_HOUR = 3600 ; // 1 * 60 * 60
21+ const TIMEOUT_ONE_DAY = 86400 ; // 1 * 60 * 60 * 24
22+ const TIMEOUT_ONE_MONTH = 2592000 ; // 1 * 60 * 60 * 24 * 30
23+ const TIMEOUT_ONE_QUARTER = 7776000 ; // 1 * 60 * 60 * 24 * 90
24+ const TIMEOUT_ONE_YEAR = 31536000 ; // 1 * 60 * 60 * 24 * 365
25+ const TIMEOUT_ONE_CENTURY = 3153600000 ; // 1 * 60 * 60 * 24 * 365 * 100
26+ const TIMEOUT_ONE_LIFE = 3153600000 ; // 1 * 60 * 60 * 24 * 365 * 100
2727
2828 const SERVER_LOCALHOST = 'localhost ' ;
2929
@@ -35,23 +35,9 @@ class Cache
3535 const PORT_REDIS = 6379 ;
3636 const PORT_MEMCACHED = 11211 ;
3737
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- }
38+ private static $ config = null ;
39+ private static $ driver = null ;
40+ private static $ prefix = null ;
5541
5642 /**
5743 * Get Key
@@ -60,9 +46,9 @@ public function __construct($config = null)
6046 *
6147 * @return string
6248 */
63- private function getKey ($ key )
49+ private static function getKey ($ key )
6450 {
65- return $ this -> prefix . $ key ;
51+ return self :: $ prefix . $ key ;
6652 }
6753
6854 /**
@@ -72,9 +58,9 @@ private function getKey($key)
7258 *
7359 * @return bool
7460 */
75- private function isDriver ($ driver )
61+ private static function isDriver ($ driver )
7662 {
77- if ($ this -> config ['driver ' ] == $ driver ) {
63+ if (self :: $ config ['driver ' ] == $ driver ) {
7864 return true ;
7965 }
8066 return false ;
@@ -83,30 +69,42 @@ private function isDriver($driver)
8369 /**
8470 * Start Driver
8571 */
86- private function startDriver ()
72+ private static function startDriver ()
8773 {
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 ' ]);
74+ if (self :: $ driver == null ) {
75+ if (self :: isDriver (self ::DRIVER_REDIS )) {
76+ self :: $ driver = new Redis ();
77+ self :: $ driver ->connect (self :: $ config ['server ' ], self :: $ config ['port ' ]);
78+ if (isset (self :: $ config ['password ' ])) {
79+ self :: $ driver ->auth (self :: $ config ['password ' ]);
9480 }
95- if (isset ($ this -> config ['database ' ])) {
96- $ this -> driver ->select ($ this -> config ['database ' ]);
81+ if (isset (self :: $ config ['database ' ])) {
82+ self :: $ driver ->select (self :: $ config ['database ' ]);
9783 }
9884 }
9985 }
10086 }
10187
88+ /**
89+ * Set Config
90+ *
91+ * @param null $config
92+ */
93+ public static function setConfig ($ config = null )
94+ {
95+ self ::$ config = is_array ($ config ) ? $ config : array ();
96+ self ::$ prefix = isset (self ::$ config ['prefix ' ]) ? strval (self ::$ config ['prefix ' ]) : '' ;
97+ self ::$ driver = null ;
98+ }
99+
102100 /**
103101 * Close Driver
104102 * @return null
105103 */
106- public function closeDriver ()
104+ public static function closeDriver ()
107105 {
108- if ($ this -> isDriver (self ::DRIVER_REDIS )) {
109- return $ this -> driver ->close ();
106+ if (self :: isDriver (self ::DRIVER_REDIS )) {
107+ return self :: $ driver ->close ();
110108 }
111109 return false ;
112110 }
@@ -115,10 +113,10 @@ public function closeDriver()
115113 * Get Driver
116114 * @return null
117115 */
118- public function getDriver ()
116+ public static function getDriver ()
119117 {
120- $ this -> startDriver ();
121- return $ this -> driver ;
118+ self :: startDriver ();
119+ return self :: $ driver ;
122120 }
123121
124122 /**
@@ -130,15 +128,15 @@ public function getDriver()
130128 *
131129 * @return mixed
132130 */
133- public function getData ($ key , $ timeout , $ function )
131+ public static function getData ($ key , $ timeout , $ function )
134132 {
135- $ key = $ this -> getKey ($ key );
136- if ($ this -> isExist ($ key )) {
137- return unserialize ($ this -> readValue ($ key ));
133+ $ key = self :: getKey ($ key );
134+ if (self :: isExist ($ key )) {
135+ return unserialize (self :: readValue ($ key ));
138136 }
139137 $ value = $ function ();
140138 $ timeout = intval ($ timeout );
141- $ this -> writeValue ($ key , serialize ($ value ), $ timeout );
139+ self :: writeValue ($ key , serialize ($ value ), $ timeout );
142140 return $ value ;
143141 }
144142
@@ -149,12 +147,12 @@ public function getData($key, $timeout, $function)
149147 *
150148 * @return bool
151149 */
152- public function isExist ($ key )
150+ public static function isExist ($ key )
153151 {
154- $ key = $ this -> getKey ($ key );
155- $ this -> startDriver ();
156- if ($ this -> isDriver (self ::DRIVER_REDIS )) {
157- return $ this -> driver ->exists ($ key );
152+ $ key = self :: getKey ($ key );
153+ self :: startDriver ();
154+ if (self :: isDriver (self ::DRIVER_REDIS )) {
155+ return self :: $ driver ->exists ($ key );
158156 }
159157 return false ;
160158 }
@@ -166,12 +164,12 @@ public function isExist($key)
166164 *
167165 * @return bool
168166 */
169- public function delete ($ key )
167+ public static function delete ($ key )
170168 {
171- $ key = $ this -> getKey ($ key );
172- $ this -> startDriver ();
173- if ($ this -> isDriver (self ::DRIVER_REDIS )) {
174- $ result = $ this -> driver ->delete ($ key );
169+ $ key = self :: getKey ($ key );
170+ self :: startDriver ();
171+ if (self :: isDriver (self ::DRIVER_REDIS )) {
172+ $ result = self :: $ driver ->delete ($ key );
175173 if ($ result > 0 ) {
176174 return true ;
177175 }
@@ -189,13 +187,13 @@ public function delete($key)
189187 *
190188 * @return bool
191189 */
192- public function writeValue ($ key , $ value , $ timeout = 0 )
190+ public static function writeValue ($ key , $ value , $ timeout = 0 )
193191 {
194- $ key = $ this -> getKey ($ key );
195- $ this -> startDriver ();
192+ $ key = self :: getKey ($ key );
193+ self :: startDriver ();
196194 $ timeout = intval ($ timeout );
197- if ($ this -> isDriver (self ::DRIVER_REDIS )) {
198- $ this -> driver ->set ($ key , $ value , $ timeout );
195+ if (self :: isDriver (self ::DRIVER_REDIS )) {
196+ self :: $ driver ->set ($ key , $ value , $ timeout );
199197 return true ;
200198 }
201199 return false ;
@@ -209,15 +207,15 @@ public function writeValue($key, $value, $timeout = 0)
209207 *
210208 * @return null
211209 */
212- public function readValue ($ key , $ default = null )
210+ public static function readValue ($ key , $ default = null )
213211 {
214- $ this -> startDriver ();
215- if ($ this -> isDriver (self ::DRIVER_REDIS )) {
216- if (!$ this -> isExist ($ key )) {
212+ self :: startDriver ();
213+ if (self :: isDriver (self ::DRIVER_REDIS )) {
214+ if (!self :: isExist ($ key )) {
217215 return $ default ;
218216 }
219- $ key = $ this -> getKey ($ key );
220- return $ this -> driver ->get ($ key );
217+ $ key = self :: getKey ($ key );
218+ return self :: $ driver ->get ($ key );
221219 }
222220 return $ default ;
223221 }
0 commit comments