|
1 | | -# PHP EasyCache |
| 1 | +# PHP EasyCache 🚀 |
2 | 2 |
|
3 | | -The PHP EasyCache Class is an easy & fast way to cache 3rd party API calls with lossless data compression support. |
| 3 | +A lightweight, secure, and extendable caching library for PHP — now safer, faster, and more flexible. |
4 | 4 |
|
5 | | -## Install |
| 5 | +--- |
6 | 6 |
|
7 | | -Install via [composer](https://getcomposer.org): |
| 7 | +## Features |
| 8 | +✅ File locking for thread-safe writes |
| 9 | +✅ Automatic expired cache cleanup |
| 10 | +✅ Per-key TTL |
| 11 | +✅ Key sanitization (security) |
| 12 | +✅ Optional compression |
| 13 | +✅ PHPUnit test support |
8 | 14 |
|
9 | | -```javascript |
10 | | -{ |
11 | | - "require": { |
12 | | - "iprodev/php-easycache": "~1.2" |
13 | | - } |
14 | | -} |
15 | | -``` |
16 | | - |
17 | | -Run `composer install` then use as normal: |
| 15 | +--- |
18 | 16 |
|
19 | | -```php |
20 | | -require 'vendor/autoload.php'; |
21 | | -$cache = new iProDev\Util\EasyCache(); |
| 17 | +## Installation |
| 18 | +```bash |
| 19 | +composer require iprodev/php-easycache |
22 | 20 | ``` |
23 | 21 |
|
24 | | -## Usage |
25 | | - |
26 | | -A very basic usage example: |
| 22 | +--- |
27 | 23 |
|
| 24 | +## Usage |
28 | 25 | ```php |
29 | | -$cache = new iProDev\Util\EasyCache(); |
30 | | -$latest_tweet = $cache->get_data('tweet', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); |
31 | | -var_dump($latest_tweet); |
32 | | -``` |
33 | | - |
34 | | -A more advanced example: |
| 26 | +use Iprodev\EasyCache\EasyCache; |
35 | 27 |
|
36 | | -```php |
37 | | -$cache = new iProDev\Util\EasyCache(); |
38 | | -$cache->cache_path = 'cache/'; |
39 | | -$cache->cache_time = 3600; |
| 28 | +$cache = new EasyCache([ |
| 29 | + 'cache_path' => __DIR__ . '/tmp/', |
| 30 | + 'cache_time' => 600, |
| 31 | + 'compression_level' => 3 |
| 32 | +]); |
40 | 33 |
|
41 | | -$data = $cache->get('identity_keyword'); |
| 34 | +$data = $cache->get('github_data'); |
42 | 35 |
|
43 | | -if(!$data) { |
44 | | - $data = $cache->get_contents('http://some.api.com/file.json'); |
45 | | - $cache->set('identity_keyword', $data); |
| 36 | +if (!$data) { |
| 37 | + $data = file_get_contents('https://api.github.com/repos/iprodev/PHP-EasyCache'); |
| 38 | + $cache->set('github_data', $data, 300); |
46 | 39 | } |
47 | 40 |
|
48 | | -var_dump($data); |
| 41 | +echo $data; |
49 | 42 | ``` |
50 | 43 |
|
51 | | -An example with compressing data: |
52 | | - |
53 | | -```php |
54 | | -$cache = new iProDev\Util\EasyCache(); |
55 | | -$cache->compress_level = 9; |
56 | | - |
57 | | -$data = $cache->get_data('identity_keyword', 'http://some.api.com/file.json'); |
58 | | - |
59 | | -var_dump($data); |
60 | | -``` |
61 | | - |
62 | | -## API Methods |
63 | | - |
64 | | -```php |
65 | | -$data = "Some data to cache"; |
66 | | - |
67 | | -// Call EasyCache |
68 | | -$cache = new iProDev\Util\EasyCache(); |
69 | | - |
70 | | -// SET a Data into Cache |
71 | | -$cache->set('identity_keyword', $data); |
72 | | - |
73 | | -// GET a Data from Cache |
74 | | -$data = $cache->get('identity_keyword'); |
75 | | - |
76 | | -// Check that the data is cached |
77 | | -$is_cached = $cache->is_cached('identity_keyword'); |
78 | | - |
79 | | -// Get the Data from URL and cache it |
80 | | -$data = $cache->get_data('identity_keyword', 'http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); |
81 | | - |
82 | | -// Helper function for retrieving data from url without cache |
83 | | -$data = $cache->get_contents('http://search.twitter.com/search.atom?q=from:chawroka&rpp=1'); |
84 | | - |
85 | | -// REMOVE a Cache item |
86 | | -$cache->delete('identity_keyword'); |
87 | | - |
88 | | -// REMOVES all Cache expired items |
89 | | -$cache->flush_expired(); |
90 | | - |
91 | | -// REMOVES all Cache items |
92 | | -$cache->flush(); |
93 | | -``` |
94 | | - |
95 | | - |
96 | | -## Configuration |
97 | | - |
98 | | -```php |
99 | | -// Call EasyCache |
100 | | -$cache = new iProDev\Util\EasyCache(); |
101 | | - |
102 | | -// Path to cache folder (with trailing /) |
103 | | -$cache->cache_path = 'cache/'; |
104 | | - |
105 | | -// Cache file extension |
106 | | -$cache->cache_extension = '.cache'; |
107 | | - |
108 | | -// Length of time to cache a file (in seconds) |
109 | | -$cache->cache_time = 3600; |
| 44 | +--- |
110 | 45 |
|
111 | | -// Lossless data compression level. 1 - 9; 0 to disable |
112 | | -$cache->compress_level = 0; |
| 46 | +## Running Tests |
| 47 | +```bash |
| 48 | +composer install |
| 49 | +composer test |
113 | 50 | ``` |
114 | 51 |
|
115 | | -## Credits |
| 52 | +--- |
116 | 53 |
|
117 | | -PHP EasyCache was created by [Hemn Chawroka](http://iprodev.com) from [iProDev](http://iprodev.com). Released under the MIT license. |
| 54 | +## License |
| 55 | +MIT © [iprodev](https://github.com/iprodev) |
0 commit comments