├── .github └── FUNDING.yml ├── README.md ├── composer.json └── src └── SimpleCache.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [gilbitron] 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP SimpleCache 2 | 3 | The PHP SimpleCache Class is an easy way to cache 3rd party API calls. 4 | 5 | ## Install 6 | 7 | Install via [composer](https://getcomposer.org): 8 | 9 | ```javascript 10 | { 11 | "require": { 12 | "gilbitron/php-simplecache": "~1.4" 13 | } 14 | } 15 | ``` 16 | 17 | Run `composer install` then use as normal: 18 | 19 | ```php 20 | require 'vendor/autoload.php'; 21 | $cache = new Gilbitron\Util\SimpleCache(); 22 | ``` 23 | 24 | ## Usage 25 | 26 | A very basic usage example: 27 | 28 | ```php 29 | $cache = new Gilbitron\Util\SimpleCache(); 30 | $latest_tweet = $cache->get_data('tweet', 'http://search.twitter.com/search.atom?q=from:gilbitron&rpp=1'); 31 | echo $latest_tweet; 32 | ``` 33 | 34 | A more advanced example: 35 | 36 | ```php 37 | $cache = new Gilbitron\Util\SimpleCache(); 38 | $cache->cache_path = 'cache/'; 39 | $cache->cache_time = 3600; 40 | 41 | if($data = $cache->get_cache('label')){ 42 | $data = json_decode($data); 43 | } else { 44 | $data = $cache->do_curl('http://some.api.com/file.json'); 45 | $cache->set_cache('label', $data); 46 | $data = json_decode($data); 47 | } 48 | 49 | print_r($data); 50 | ``` 51 | 52 | ## Credits 53 | 54 | PHP SimpleCache was created by [Gilbert Pellegrom](https://gilbitron.me) from [Dev7studios](https://dev7studios.co). Released under the MIT license. 55 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gilbitron/php-simplecache", 3 | "type": "library", 4 | "description": "A simple script for caching 3rd party API calls in PHP.", 5 | "keywords": [ 6 | "simplecache", 7 | "cache" 8 | ], 9 | "homepage": "https://github.com/gilbitron/PHP-SimpleCache", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Gilbert Pellegrom", 14 | "email": "gilbert@pellegrom.me", 15 | "homepage": "http://gilbert.pellegrom.me" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.3.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Gilbitron\\Util\\": "src/" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/SimpleCache.php: -------------------------------------------------------------------------------- 1 | get_cache($label)){ 27 | return $data; 28 | } else { 29 | $data = $this->do_curl($url); 30 | $this->set_cache($label, $data); 31 | return $data; 32 | } 33 | } 34 | 35 | public function set_cache($label, $data) 36 | { 37 | file_put_contents($this->cache_path . $this->safe_filename($label) . $this->cache_extension, $data); 38 | } 39 | 40 | public function get_cache($label) 41 | { 42 | if($this->is_cached($label)){ 43 | $filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension; 44 | return file_get_contents($filename); 45 | } 46 | 47 | return false; 48 | } 49 | 50 | public function is_cached($label) 51 | { 52 | $filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension; 53 | 54 | if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true; 55 | 56 | return false; 57 | } 58 | 59 | //Helper function for retrieving data from url 60 | public function do_curl($url) 61 | { 62 | if(function_exists("curl_init")){ 63 | $ch = curl_init(); 64 | curl_setopt($ch, CURLOPT_URL, $url); 65 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 66 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 67 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 68 | $content = curl_exec($ch); 69 | curl_close($ch); 70 | return $content; 71 | } else { 72 | return file_get_contents($url); 73 | } 74 | } 75 | 76 | //Helper function to validate filenames 77 | private function safe_filename($filename) 78 | { 79 | return preg_replace('/[^0-9a-z\.\_\-]/i','', strtolower($filename)); 80 | } 81 | } 82 | --------------------------------------------------------------------------------