| 1 |  |  | <?php /** MicroApcDriver */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Micro\Cache\Drivers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Micro\Base\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class ApcDriver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author Oleg Lunegov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link https://github.com/linpax/microphp-framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @copyright Copyright (c) 2013 Oleg Lunegov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package Micro | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @subpackage Cache\Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @version 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @since 1.0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class ApcDriver extends CacheDriver | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Constructor | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @access public | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @param array $config config array | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @result void | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @throws Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct(array $config = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         parent::__construct($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (!$this->check()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             throw new Exception('APC cache not installed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function check() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (extension_loaded('apc') && ini_get('apc.enabled')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $values = apc_fetch($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return is_array($values) ? $values : []; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function set($name, $value, $duration = 300, $new = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($new === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return apc_add($name, $value, $duration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return apc_store($name, $value, $duration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function delete($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return apc_delete($name); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function clean() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (extension_loaded('apc')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return apc_clear_cache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return apc_clear_cache('user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function info($type = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return apc_cache_info($type); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function getMeta($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $success = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $stored = apc_fetch($id, $success); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($success === false || count($stored) !== 3) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         list($data, $time, $ttl) = $stored; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return ['expire' => $time + $ttl, 'mtime' => $time, 'data' => unserialize($data)]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function increment($name, $offset = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return apc_inc($name, $offset); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function decrement($name, $offset = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return apc_dec($name, $offset); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  | }  | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  |