|
1 | | -# php-caching |
| 1 | +# PHP Cache Class |
2 | 2 | A light, simple and powerful PHP Cache Class that uses the filesystem for caching. |
| 3 | + Your feedback is always welcome. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- PHP 7.4.0 or higher (**cache.class.php**) |
| 8 | +- PHP 5.1.6 or higher (**cache.class-old.php**) |
| 9 | + |
| 10 | +## Introduction |
| 11 | +Basically, the caching class stores its data in files in text format. Files will be created for each cache you will create, it will use less memory (RAM) in comparison to using a single file for multiple caches. |
| 12 | + |
| 13 | +## Quick Start ## |
| 14 | + |
| 15 | +#### Setup Cache class: |
| 16 | + |
| 17 | +First include the Cache class: |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | + require_once 'cache.class.php'; |
| 22 | + // require_once 'cache.class-old.php'; // for older versions of php |
| 23 | + |
| 24 | + // Create an instance; |
| 25 | + $cache = new Cache('cache'); // We have to pass cache directory (folder) path |
| 26 | +?> |
| 27 | +``` |
| 28 | + |
| 29 | +#### Create cache: |
| 30 | +`$classInstance->write(string $cacheName, string $content)` <br /> |
| 31 | +Params |
| 32 | +- **$cacheName** (Required): Any string that will be used to access the cache in future |
| 33 | +- **$content** (Optional): Content (as string); |
| 34 | + |
| 35 | +```php |
| 36 | +<?php |
| 37 | + $cache->write('cache-name', 'This is the content'); |
| 38 | +?> |
| 39 | +``` |
| 40 | + |
| 41 | +#### Get cached data: |
| 42 | +`$classInstance->read(string $cacheName, int $maxAge = 0, bool $deleteExpired = TRUE)` <br /> |
| 43 | +Params |
| 44 | +- **$cacheName** (Required): String that was used while creating cache |
| 45 | +- **$maxAge** (Optional): Seconds; Return NULL if file older then these seconds. Default: 0, No limit |
| 46 | +- **$deleteExpired** (Optional): TRUE OR FALSE; Delete cache if file age is more then maxAge. Default: TRUE |
| 47 | + |
| 48 | +```php |
| 49 | +<?php |
| 50 | + $cache->read('cache-name', 200, TRUE); |
| 51 | +?> |
| 52 | +``` |
| 53 | + |
| 54 | +#### Cache Subfolder: |
| 55 | +The cache files will be stored in a subfolder in the cache directory <br /> |
| 56 | +`$classInstance->setSubFolder(string $subFolder)` <br /> |
| 57 | +Params |
| 58 | +- **$subFolder** (Required): subfolder name |
| 59 | + |
| 60 | +```php |
| 61 | +<?php |
| 62 | + $cache->setSubFolder('ip-files'); |
| 63 | + $cache->write('134.201.250.155', '{"type": "ipv4", "continent": "NA", "country": "US", "region": "CA", }'); |
| 64 | + $ipdate = $cache->read('134.201.250.155', 200, TRUE); |
| 65 | +?> |
| 66 | +``` |
| 67 | + |
| 68 | +#### Delete Expired Cache files: |
| 69 | +`$classInstance->clear(int $maxAge = 0)` <br /> |
| 70 | +Params |
| 71 | +- **$maxAge** (Optional): Seconds; Return NULL if file older then these seconds. Default: 0, delete all |
| 72 | + |
| 73 | +```php |
| 74 | +<?php |
| 75 | + $cache->clear(200); |
| 76 | +?> |
| 77 | +``` |
| 78 | + |
| 79 | +#### Delete all Cache files: |
| 80 | +`$classInstance->clearAll()` <br /> |
| 81 | +It will delete every thing from cache directory |
| 82 | + |
| 83 | +```php |
| 84 | +<?php |
| 85 | + $cache->clearAll(); |
| 86 | +?> |
| 87 | +``` |
| 88 | + |
| 89 | +## Example: |
| 90 | +Please check test folder for examples |
| 91 | +```php |
| 92 | +require_once("cache.class.php"); |
| 93 | + |
| 94 | +$cache = new Cache('cache'); |
| 95 | + |
| 96 | +$page = "home.php"; |
| 97 | +$cacheMaxAge = 86400; // One Day |
| 98 | +$cachedData = $cache->read($page, $cacheMaxAge); |
| 99 | + |
| 100 | +if($cachedData != NULL){ |
| 101 | + echo $cachedData; |
| 102 | + die; |
| 103 | +} |
| 104 | +else{ |
| 105 | + ob_start(); |
| 106 | + include($page); |
| 107 | + |
| 108 | + $page_content = ob_get_contents(); |
| 109 | + |
| 110 | + $cache->write($page, $page_content); |
| 111 | + |
| 112 | + ob_end_flush(); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +Thanks; |
0 commit comments