├── .gitignore ├── LICENSE.txt ├── README.md ├── composer.json └── src └── FileCache.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor/ 3 | composer.lock 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014, Taiji Inoue 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | FileCache 2 | ========== 3 | 4 | A simple PHP class for caching 5 | 6 | 7 | Usage 8 | ------------------- 9 | 10 | ```php 11 | 'John', 20 | 'age' => 20, 21 | 'sex' => 'f', 22 | ); 23 | $lifetime = 3600; // cache lifetime (default: 3600) 24 | $cache->save($id, $data, $lifetime); 25 | 26 | $user = $cache->get($id); 27 | // return false if cache is expired or does not exist 28 | 29 | print_r($user); 30 | 31 | ``` 32 | 33 | output: 34 | 35 | ``` 36 | Array 37 | ( 38 | [name] => John 39 | [age] => 20 40 | [sex] => f 41 | ) 42 | ``` 43 | 44 | Cache files are stored in /tmp/cache directory by default. 45 | It will be saved under the folder hierarchy of 2. 46 | 47 | ``` 48 | $ tree /tmp/cache 49 | /tmp/cache 50 | └── a2 51 | └── 24 52 | └── a224b17e63b8eb3103a8c4679b7de2072b598c99.cache 53 | ``` 54 | 55 | Delete cache 56 | 57 | ``` 58 | 59 | $cache->delete($id); 60 | 61 | ``` 62 | 63 | ### Change cache directory 64 | 65 | set parameter "cache_dir" to constructor 66 | 67 | ``` 68 | __DIR__.'/cache'); 71 | 72 | $cache = new FileCache($options); 73 | $cache->save("key", "value"); 74 | $ tree ./cache 75 | ./cache 76 | └── 31 77 | └── f3 78 | └── 31f30ddbcb1bf8446576f0e64aa4c88a9f055e3c.cache 79 | 80 | 2 directories, 1 file 81 | 82 | ``` 83 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inouet/file-cache", 3 | "type": "library", 4 | "description": "A simple PHP class for caching data in the filesystem.", 5 | "keywords": ["cache", "file"], 6 | "homepage": "http://github.com/inouet/file-cache", 7 | "support": { 8 | "issues": "https://github.com/inouet/file-cache/issues", 9 | "source": "https://github.com/inouet/file-cache" 10 | }, 11 | "authors": [ 12 | { 13 | "name": "Taiji Inoue", 14 | "email": "inudog@gmail.com", 15 | "role": "Maintainer" 16 | } 17 | ], 18 | "license": [ 19 | "MIT" 20 | ], 21 | "require": { 22 | "php": ">=5.4.0" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "3.7.*" 26 | }, 27 | "autoload": { 28 | "classmap": ["src/FileCache.php"] 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/FileCache.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | 16 | class FileCache 17 | { 18 | 19 | /** 20 | * The root cache directory. 21 | * @var string 22 | */ 23 | private $cache_dir = '/tmp/cache'; 24 | 25 | /** 26 | * Creates a FileCache object 27 | * 28 | * @param array $options 29 | */ 30 | public function __construct(array $options = array()) 31 | { 32 | $available_options = array('cache_dir'); 33 | foreach ($available_options as $name) { 34 | if (isset($options[$name])) { 35 | $this->$name = $options[$name]; 36 | } 37 | } 38 | } 39 | 40 | /** 41 | * Fetches an entry from the cache. 42 | * 43 | * @param string $id 44 | */ 45 | public function get($id) 46 | { 47 | $file_name = $this->getFileName($id); 48 | 49 | if (!is_file($file_name) || !is_readable($file_name)) { 50 | return false; 51 | } 52 | 53 | $lines = file($file_name); 54 | $lifetime = array_shift($lines); 55 | $lifetime = (int) trim($lifetime); 56 | 57 | if ($lifetime !== 0 && $lifetime < time()) { 58 | @unlink($file_name); 59 | return false; 60 | } 61 | $serialized = join('', $lines); 62 | $data = unserialize($serialized); 63 | return $data; 64 | } 65 | 66 | /** 67 | * Deletes a cache entry. 68 | * 69 | * @param string $id 70 | * 71 | * @return bool 72 | */ 73 | public function delete($id) 74 | { 75 | $file_name = $this->getFileName($id); 76 | return unlink($file_name); 77 | } 78 | 79 | /** 80 | * Puts data into the cache. 81 | * 82 | * @param string $id 83 | * @param mixed $data 84 | * @param int $lifetime 85 | * 86 | * @return bool 87 | */ 88 | public function save($id, $data, $lifetime = 3600) 89 | { 90 | $dir = $this->getDirectory($id); 91 | if (!is_dir($dir)) { 92 | if (!mkdir($dir, 0755, true)) { 93 | return false; 94 | } 95 | } 96 | $file_name = $this->getFileName($id); 97 | $lifetime = time() + $lifetime; 98 | $serialized = serialize($data); 99 | $result = file_put_contents($file_name, $lifetime . PHP_EOL . $serialized); 100 | if ($result === false) { 101 | return false; 102 | } 103 | return true; 104 | } 105 | 106 | //------------------------------------------------ 107 | // PRIVATE METHODS 108 | //------------------------------------------------ 109 | 110 | /** 111 | * Fetches a directory to store the cache data 112 | * 113 | * @param string $id 114 | * 115 | * @return string 116 | */ 117 | protected function getDirectory($id) 118 | { 119 | $hash = sha1($id, false); 120 | $dirs = array( 121 | $this->getCacheDirectory(), 122 | substr($hash, 0, 2), 123 | substr($hash, 2, 2) 124 | ); 125 | return join(DIRECTORY_SEPARATOR, $dirs); 126 | } 127 | 128 | /** 129 | * Fetches a base directory to store the cache data 130 | * 131 | * @return string 132 | */ 133 | protected function getCacheDirectory() 134 | { 135 | return $this->cache_dir; 136 | } 137 | 138 | /** 139 | * Fetches a file path of the cache data 140 | * 141 | * @param string $id 142 | * 143 | * @return string 144 | */ 145 | protected function getFileName($id) 146 | { 147 | $directory = $this->getDirectory($id); 148 | $hash = sha1($id, false); 149 | $file = $directory . DIRECTORY_SEPARATOR . $hash . '.cache'; 150 | return $file; 151 | } 152 | } 153 | --------------------------------------------------------------------------------