├── LICENSE ├── README.md ├── composer.json └── src └── SuperCache ├── State.php └── SuperCache.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 PHP Framework Interoperability Group 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-super-cache 2 | Simple PHP cache mechanism which provides 200X Faster Caching than other PHP cache mechanisms like Redis/Memcache/APC in PHP & HHVM. SuperCache uses the normal file system to store values. This method is faster than Redis/Memcache because all those serialize and unserialize objects. 3 | 4 | [![GitHub tag](https://img.shields.io/github/tag/shabeer-ali-m/php-super-cache.svg?style=flat-square)](https://github.com/shabeer-ali-m/php-super-cache/releases) 5 | 6 | ## Installation 7 | Via Composer 8 | ``` bash 9 | composer require smart-php/super-cache 10 | ``` 11 | 12 | ### Basic Usage 13 | 14 | ```php 15 | ')->set(''); 21 | sCache::cache('myKey')->set('Key_value'); 22 | 23 | //Retrieving cache value with a key 24 | echo sCache::cache('myKey')->get(); 25 | ?> 26 | ``` 27 | 28 | ### Cache Folder 29 | By default the cache will save in the `tmp` folder. Please make sure that the `tmp` folder has write access. 30 | You can set a custom folder for the cache: 31 | ```php 32 | sCache::setPath('youfolder/tempfolder/'); 33 | ``` 34 | or 35 | ```php 36 | define('SuperCache_PATH','youfolder/tempfolder/'); 37 | ``` 38 | 39 | 40 | #### Advanced Options 41 | ##### Locking 42 | 43 | Lock your data to readonly so that the data won't be overwritten. 44 | ```php 45 | sCache::cache('myKey')->set('my_value')->lock(); 46 | //setting new value 47 | sCache::cache('myKey')->set('new_value'); 48 | echo sCache::cache('myKey')->get(); //output : my_value 49 | //unlocking 50 | sCache::cache('myKey')->unlock()->set('new_value'); 51 | echo sCache::cache('myKey')->get(); //output : new_value 52 | ``` 53 | 54 | ##### Options 55 | ```php 56 | //options 57 | sCache::cache('myKey')->set('my_value')->options([ 58 | 'expiry' => time()+600, //time to expire 59 | 'lock' => true //alternative method to lock or unlock 60 | 'custom' => 'your customer attribute value' 61 | ]); 62 | 63 | //isValid (To check for a valid key or to check whether it has expired or not) 64 | sCache::cache('myKey')->isValid(); //true or false 65 | 66 | //To get all option values 67 | print_r(sCache::cache('myKey')->getOptions()); //array 68 | 69 | //destroy 70 | sCache::cache('myKey')->destroy(); 71 | 72 | //clearAll (Clear all cache values) 73 | sCache::cache('myKey')->clearAll(); 74 | ``` 75 | 76 | ### Cache Your Class 77 | 78 | You can cache any class to super cache. To bind data you need to use the trait class `State` by adding `use State`, or you can do it via your custom set_state. 79 | 80 | ```php 81 | use SuperCache\SuperCache as sCache; 82 | use SuperCache\State as State; 83 | 84 | sCache::setPath('youCacheLocation'); 85 | 86 | Class MyClass 87 | { 88 | /** 89 | * This trait class will bind data from cache to your class 90 | */ 91 | use State; 92 | 93 | private $firstName; 94 | 95 | private $lastName; 96 | 97 | public function setName($firstName, $lastName) 98 | { 99 | $this->firstName = $firstName; 100 | $this->lastName = $lastName; 101 | } 102 | 103 | public function getName() 104 | { 105 | return $this->firstName . " ". $this->lastName; 106 | } 107 | } 108 | 109 | // Creating Object of your class 110 | $myObject = new MyClass; 111 | $myObject->setName("John", "Doe"); 112 | 113 | // Saving your object in SuperCache 114 | sCache::cache('myObject')->set($myObject); 115 | 116 | // Retrieving your object from Cache 117 | $cacheObject = sCache::cache('myObject')->get(); 118 | echo $myObject->getName(); 119 | ``` 120 | 121 | ## License 122 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 123 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smart-php/super-cache", 3 | "description": "Simple PHP cache mechanism which is 500X Faster Caching than Redis/Memcache/APC in PHP & HHVM", 4 | "keywords": ["php", "php-super-cache", "super-cache", "cache", "php-cache", "lightweight", "caching", "powerful"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Shabeer Ali M", 9 | "email": "shabeer.ali.m@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "version": "0.0.9", 14 | "require": { 15 | "php": ">=5.3.0" 16 | }, 17 | "autoload": { 18 | "psr-0": { 19 | "SuperCache": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/SuperCache/State.php: -------------------------------------------------------------------------------- 1 | $v) { 24 | $this->{$k} = $v; 25 | } 26 | } 27 | 28 | /** 29 | * __set_state 30 | * @param [array] $data 31 | */ 32 | public static function __set_state($data) 33 | { 34 | $self = new self(); 35 | $self->setState($data); 36 | return $self; 37 | } 38 | } -------------------------------------------------------------------------------- /src/SuperCache/SuperCache.php: -------------------------------------------------------------------------------- 1 | path = self::$static_path; 76 | $this->key = $key; 77 | $this->options = [ 78 | 'expiry' => -1, 79 | 'lock' => false, 80 | ]; 81 | if ($is_super) { 82 | $this->attr_instance = new self($this->key . SuperCache::OPTIONS, false); 83 | if ($this->attr_instance->isExists()) { 84 | $this->options = $this->attr_instance->get(); 85 | } 86 | 87 | } 88 | } 89 | 90 | /** 91 | * attrSave Save attributes 92 | */ 93 | private function attrSave() 94 | { 95 | $this->attr_instance->set($this->options); 96 | } 97 | 98 | /** 99 | * cache 100 | * @param [sting] $key cache key 101 | * @return SuperCache 102 | */ 103 | public static function cache($key) 104 | { 105 | if (!isset(self::$instance[$key])) { 106 | self::$instance[$key] = new self($key); 107 | } 108 | return self::$instance[$key]; 109 | } 110 | 111 | /** 112 | * Set File Path 113 | * @param [sting] $path file path 114 | */ 115 | public static function setPath($path) 116 | { 117 | self::$static_path = $path; 118 | } 119 | 120 | /** 121 | * Get File Path 122 | * @return [sting] $path file path 123 | */ 124 | public static function getPath() 125 | { 126 | return self::$static_path; 127 | } 128 | 129 | /** 130 | * 131 | * @param [bolean] $flag 132 | */ 133 | public static function setPretty($val) 134 | { 135 | self::$is_pretty = (boolean) $val; 136 | } 137 | 138 | /** 139 | * Saving cache 140 | * @param [mixed] $val Cache Value 141 | * @return SuperCache 142 | */ 143 | public function set($val) 144 | { 145 | $key = $this->key; 146 | if ($this->options['lock']) { 147 | return $this; 148 | } 149 | 150 | $val = var_export($val, true); 151 | 152 | //is_pretty 153 | if (!self::$is_pretty) { 154 | $val = str_replace(["\n",", '"," => "],["",",'","=>"], $val); 155 | } 156 | 157 | 158 | // HHVM fails at __set_state, so just use object cast for now 159 | $val = str_replace('stdClass::__set_state', '(object)', $val); 160 | // Write to temp file first to ensure atomicity 161 | $tmp = $this->path . "$key." . uniqid('', true) . SuperCache::EXT; 162 | $file = fopen($tmp, 'x'); 163 | fwrite ($file, 'path . $key); 166 | return $this; 167 | } 168 | 169 | /** 170 | * Retrieving cache value 171 | * @return [mixed] 172 | */ 173 | public function get() 174 | { 175 | if (!$this->isValid()) { 176 | return; 177 | } 178 | 179 | @include $this->path . "$this->key"; 180 | return isset($val) ? $val : false; 181 | } 182 | 183 | /** 184 | * getOptions Get Super Cache Options 185 | * @return array 186 | */ 187 | public function getOptions() 188 | { 189 | return $this->options; 190 | } 191 | 192 | /** 193 | * isExists 194 | * @return boolean 195 | */ 196 | private function isExists() 197 | { 198 | if (file_exists($this->path . "$this->key")) { 199 | return true; 200 | } else { 201 | return false; 202 | } 203 | 204 | } 205 | 206 | /** 207 | * isValid To check for valid key 208 | * @return boolean 209 | */ 210 | public function isValid() 211 | { 212 | if ($this->options['expiry'] != -1 && $this->options['expiry'] < time()) { 213 | return false; 214 | } 215 | 216 | if (!$this->isExists()) { 217 | return false; 218 | } 219 | 220 | return true; 221 | } 222 | 223 | /** 224 | * clearAll Removing all cache keys and values 225 | */ 226 | public static function clearAll() 227 | { 228 | $files = glob(self::$static_path . '*'); 229 | foreach ($files as $file) { 230 | if (is_file($file)) { 231 | unlink($file); 232 | } 233 | 234 | } 235 | } 236 | 237 | /** 238 | * lock specific cache 239 | * @return SuperCache 240 | */ 241 | public function lock() 242 | { 243 | $this->options['lock'] = true; 244 | $this->attrSave(); 245 | return $this; 246 | } 247 | 248 | /** 249 | * unlock specific cache 250 | * @return SuperCache 251 | */ 252 | public function unlock() 253 | { 254 | $this->options['lock'] = false; 255 | $this->attrSave(); 256 | return $this; 257 | } 258 | 259 | /** 260 | * options 261 | * @param array $options 262 | * @return SuperCache 263 | */ 264 | public function options($options) 265 | { 266 | $this->options = array_merge($this->options, $options); 267 | $this->attrSave(); 268 | return $this; 269 | } 270 | 271 | /** 272 | * destroy Destroy a specific key 273 | */ 274 | public function destroy() 275 | { 276 | @unlink($this->path . "$this->key"); 277 | @unlink($this->path . "$this->key" . SuperCache::OPTIONS); 278 | } 279 | } 280 | --------------------------------------------------------------------------------