PHP-EasyCache
The PHP EasyCache Class is an easy & fast way to cache 3rd party API calls with lossless data compression support.
Usage
A very basic usage example:
1 2 3 |
$cache = new iProDev\Util\EasyCache(); $latest_tweet = $cache->get_data('tweet', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); echo $latest_tweet; |
A more advanced example:
1 2 3 4 5 6 7 8 9 10 11 12 |
$cache = new iProDev\Util\EasyCache(); $cache->cache_path = 'cache/'; $cache->cache_time = 3600; $data = $cache->get('identity_keyword'); if(!$data) { $data = $cache->get_contents('http://some.api.com/file.json'); $cache->set('identity_keyword', $data); } var_dump($data); |
An example with compressing data:
1 2 3 4 5 6 |
$cache = new iProDev\Util\EasyCache(); $cache->compress_level = 9; $data = $cache->get_data('identity_keyword', 'http://some.api.com/file.json'); var_dump($data); |
API Methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$data = "Some data to cache"; // Call EasyCache $cache = new iProDev\Util\EasyCache(); // SET a Data into Cache $cache->set('identity_keyword', $data); // GET a Data from Cache $data = $cache->get('identity_keyword'); // Check that the data is cached $is_cached = $cache->is_cached('identity_keyword'); // Get the Data from URL and cache it $data = $cache->get_data('identity_keyword', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); // Helper function for retrieving data from url without cache $data = $cache->get_contents('http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); // REMOVE a Cache item $cache->delete('identity_keyword'); // REMOVES all Cache expired items $cache->flush_expired(); // REMOVES all Cache items $cache->flush(); |
Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Call EasyCache $cache = new iProDev\Util\EasyCache(); // Path to cache folder (with trailing /) $cache->cache_path = 'cache/'; // Cache file extension $cache->cache_extension = '.cache'; // Length of time to cache a file (in seconds) $cache->cache_time = 3600; // Lossless data compression level. 1 - 9; 0 to disable $cache->compress_level = 0; |
License
Released under the MIT license.
Dont forget to follow us on twitter or like our page on facebook :-)
and take a look at our premium scripts too ;-)