|
| 1 | +# PHP Cache Lib |
| 2 | + |
| 3 | +> Original Repos: |
| 4 | +> - PHP Cache Lib: https://github.com/a19836/phpcachelib/ |
| 5 | +> - Bloxtor: https://github.com/a19836/bloxtor/ |
| 6 | +
|
| 7 | +## Overview |
| 8 | + |
| 9 | +**PHP Cache Lib** is a simple cache library designed for caching contents, objects, arrays, strings, and more. |
| 10 | +It offers flexible backend support, allowing you to cache your data using **file system, MongoDB, Memcache, or Redis**. |
| 11 | + |
| 12 | +To see a working example, open [index.php](index.php) on your server. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Handlers |
| 17 | + |
| 18 | +### User Cache Handler |
| 19 | + |
| 20 | +To be used to cache content (objects/arrays/string/etc) in a specific folder/place, regardless of the underlying storage engine (file system, MongoDB, MemCache, Redis etc.). |
| 21 | +All cached content can be **serialized** or saved as **plain** content. |
| 22 | + |
| 23 | +All methods are **based in the file name argument**, where the cached file path is created from the **root path defined before** and the correspondent file name. |
| 24 | +Note that if root path, defined before, does not exist, it will be created automatically. |
| 25 | + |
| 26 | +Also if you cache to many files, you can reach the maximum inodes limit of your OS. In this case you should use the ServiceCacheHandler instead, because it takes care this issue. |
| 27 | +Note that cache deletion based in regex is not allowed! For this case please use the ServiceCacheHandler instead. |
| 28 | + |
| 29 | +Main available methods: |
| 30 | + |
| 31 | +| Method | Description | |
| 32 | +| :--- | :--- | |
| 33 | +| `read($file_name)` | **Retrieves** the cached data associated with the given file name. | |
| 34 | +| `write($file_name, $data)` | **Writes** the provided data to the cache using the specified file name as the key. | |
| 35 | +| `isValid($file_name)` | **Checks** if the cached entry exists *and* has not expired (is still valid based on its Time-To-Live). | |
| 36 | +| `exists($file_name)` | **Checks** if a cache entry physically exists for the given file name. | |
| 37 | +| `delete($file_name)` | **Removes** the cached entry associated with the given file name. | |
| 38 | + |
| 39 | +### Serialized Cache Handler |
| 40 | + |
| 41 | +To be used to cache content (objects/arrays/string/etc) in a specific file path, regardless of the underlying storage engine (file system, MongoDB, MemCache, Redis etc.). |
| 42 | +All cached content is **serialized**. |
| 43 | + |
| 44 | +All methods are based in the **full file path argument**, where the cached file path is this path - without any kind of path treatment. |
| 45 | +Note that for the **file system engine, the parent directory of the file path must exists**! |
| 46 | + |
| 47 | +Also if you cache to many files, you can reach the maximum inodes limit of your OS. In this case you should use the ServiceCacheHandler instead, because it takes care this issue. |
| 48 | +Note that cache deletion based in regex is not allowed! For this case please use the ServiceCacheHandler instead. |
| 49 | + |
| 50 | +Main available methods: |
| 51 | + |
| 52 | +| Method | Description | |
| 53 | +| :--- | :--- | |
| 54 | +| `getCache($file_path)` | **Retrieves** the cached data stored at the given `$file_path` (which acts as the unique key). | |
| 55 | +| `setCache($file_path, $data)` | **Writes** the provided `$data` to the cache, using the `$file_path` as the storage identifier. | |
| 56 | +| `isCacheValid($file_path)` | **Checks** if the cache entry at `$file_path` exists and is still valid (not expired). | |
| 57 | +| `deleteCache($file_path)` | **Removes** the cached entry associated with the `$file_path`. | |
| 58 | + |
| 59 | +### Service Cache Handler |
| 60 | + |
| 61 | +To be used to cache content (objects/arrays/string/etc), regardless of the underlying storage engine (file system, MongoDB, MemCache, Redis etc.), that may have other objects related, and everytime that we create a new cache, it automatically disable the related cached objects. |
| 62 | +Is used for caching **services** or complex data structures, often utilizing a `$prefix` for categorization and supporting **related keys** for complex invalidation. |
| 63 | +All cached content can be **serialized** or saved as **plain** content. |
| 64 | + |
| 65 | +All methods are based in the **prefix and key arguments**, where the cached file path is created from the **root path defined before** and the prefix and key. |
| 66 | +Note that the prefix is a string that could be a relative folder path. |
| 67 | +Note that if the root path does not exist, it will be created automatically. |
| 68 | + |
| 69 | +The cached objects are saved inside of **multiple folders** (inside of the root path) to **avoid to reach the maximum inodes limit** of the OS. This handlers take care of this issue, so you don't need to worry about it. |
| 70 | +**Deletion based in regex and other conditions** are also allowed! |
| 71 | + |
| 72 | +Although, is not allowed to get cached objects based in regex or other conditions, the idea is to create this feature in the future. |
| 73 | + |
| 74 | +Main available methods: |
| 75 | + |
| 76 | +| Method | Description | |
| 77 | +| :--- | :--- | |
| 78 | +| `create($prefix, $key, $result, $type = false)` | **Creates** a new cache entry for a service using a `$prefix` and `$key`. | |
| 79 | +| `addServiceToRelatedKeysToDelete(...)` | **Adds** the current service (`$prefix`, `$key`) to a list of services to be deleted when one of the parents are invalidated. | |
| 80 | +| `deleteAll($prefix, $type = false)` | **Removes all** cache entries that fall under the specified `$prefix`. | |
| 81 | +| `delete($prefix, $key, $settings = array())` | **Removes** a specific cache entry defined by `$prefix` and `$key`. It can also be used to delete related services. See a livde example [here](examples/service_cache_with_relations.php). | |
| 82 | +| `get($prefix, $key, $type = false)` | **Retrieves** the cached data for the service specified by `$prefix` and `$key`. | |
| 83 | +| `isValid($prefix, $key, $ttl = false, $type = false)` | **Checks** if the cached service entry is currently valid (exists and has not expired). | |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Usage |
| 88 | + |
| 89 | +The library provides different handlers for various caching needs and engines. |
| 90 | + |
| 91 | +### 1. User Cache (File System) |
| 92 | + |
| 93 | +This sample demonstrates how to use the `FileSystemUserCacheHandler` for basic data caching with an unique file name. |
| 94 | + |
| 95 | +```php |
| 96 | +include_once __DIR__ . "/lib/app.php"; |
| 97 | +include_once get_lib("cache.user.filesystem.FileSystemUserCacheHandler"); |
| 98 | + |
| 99 | +$CacheHandler = new FileSystemUserCacheHandler(); |
| 100 | +$CacheHandler->setRootPath( sys_get_temp_dir() . "/cache/user/" ); |
| 101 | +$CacheHandler->config(60, true); // 60 seconds of cache ttl |
| 102 | + |
| 103 | +$file_name = "my_cached_file_name"; // Must be unique. |
| 104 | + |
| 105 | +// Get cached contents |
| 106 | +if ($CacheHandler->isValid($file_name)) |
| 107 | + $data = $CacheHandler->read($file_name); |
| 108 | +else { // Otherwise create cache for next time |
| 109 | + $data = array("foo" => "bar", "bar" => "foo"); // Contents to cache |
| 110 | + $CacheHandler->write($file_name, $data); |
| 111 | +} |
| 112 | + |
| 113 | +// Then use $data... |
| 114 | +``` |
| 115 | + |
| 116 | +### 2. Serialized Cache (File System) |
| 117 | + |
| 118 | +This sample uses `FileSystemXmlSettingsCacheHandler` and requires preparing the cache folder beforehand. |
| 119 | + |
| 120 | +```php |
| 121 | +include_once __DIR__ . "/lib/app.php"; |
| 122 | +include_once get_lib("cache.xmlsettings.filesystem.FileSystemXmlSettingsCacheHandler"); |
| 123 | + |
| 124 | +$CacheHandler = new FileSystemXmlSettingsCacheHandler(); |
| 125 | +$CacheHandler->setCacheTTL(60); // 60 seconds of cache ttl |
| 126 | + |
| 127 | +// Prepare cache folder - must create the cached folder first |
| 128 | +$root_path = sys_get_temp_dir() . "/cache/xml_settings/"; |
| 129 | + |
| 130 | +if (!is_dir($root_path)) |
| 131 | + mkdir($root_path, 0755, true); |
| 132 | + |
| 133 | +// Set file path to be cached |
| 134 | +$file_path = $root_path . "my_cached_file_name"; // Must be unique. |
| 135 | + |
| 136 | +// Get cached contents |
| 137 | +if ($CacheHandler->isCacheValid($file_path)) |
| 138 | + $cached_data = $CacheHandler->getCache($file_path); |
| 139 | +else { // Otherwise create cache for next time |
| 140 | + $data = array("foo" => "bar", "bar" => "foo"); // Contents to cache |
| 141 | + $CacheHandler->setCache($file_path, $data); // setCache($file_path, $data, $renew_data = false) |
| 142 | +} |
| 143 | + |
| 144 | +// Then use $data... |
| 145 | +``` |
| 146 | + |
| 147 | +### 3. Serialized Cache (MongoDB) |
| 148 | + |
| 149 | +This sample shows how to integrate with MongoDB using `MongoDBXmlSettingsCacheHandler`. |
| 150 | + |
| 151 | +```php |
| 152 | +include_once __DIR__ . "/lib/app.php"; |
| 153 | +include_once get_lib("mongodb.MongoDBHandler"); |
| 154 | +include_once get_lib("cache.xmlsettings.mongodb.MongoDBXmlSettingsCacheHandler"); |
| 155 | + |
| 156 | +$MongoDBHandler = new MongoDBHandler(); |
| 157 | +$MongoDBHandler->connect("192.168.1.68", "my_cache_db", "mdbu", "mdbp", 8097); |
| 158 | + |
| 159 | +$CacheHandler = new MongoDBXmlSettingsCacheHandler(); |
| 160 | +$CacheHandler->setMongoDBHandler($MongoDBHandler); |
| 161 | +$CacheHandler->setCacheTTL(60); // 60 seconds of cache ttl |
| 162 | + |
| 163 | +// Set file path to be cached |
| 164 | +$file_path = "my_cached_file_name"; // Must be unique. |
| 165 | + |
| 166 | +// Get cached contents |
| 167 | +if ($CacheHandler->isCacheValid($file_path)) |
| 168 | + $cached_data = $CacheHandler->getCache($file_path); |
| 169 | +else { // Otherwise create cache for next time |
| 170 | + $data = array("foo" => "bar", "bar" => "foo"); // Contents to cache |
| 171 | + $CacheHandler->setCache($file_path, $data); // setCache($file_path, $data, $renew_data = false) |
| 172 | +} |
| 173 | + |
| 174 | +// Then use $data... |
| 175 | +``` |
| 176 | + |
| 177 | +### 4. Service Cache (Memcache) |
| 178 | + |
| 179 | +This sample utilizes `MemcacheServiceCacheHandler` and introduces concepts like prefixes and service relations. |
| 180 | + |
| 181 | +```php |
| 182 | +include_once __DIR__ . "/lib/app.php"; |
| 183 | +include_once get_lib("cache.service.memcache.MemcacheServiceCacheHandler"); |
| 184 | + |
| 185 | +$MemcacheHandler = new MemcacheHandler(); |
| 186 | +$MemcacheHandler->connect("192.168.1.68", 8090); |
| 187 | + |
| 188 | +$CacheHandler = new MemcacheServiceCacheHandler(); |
| 189 | +$CacheHandler->setMemcacheHandler($MemcacheHandler); |
| 190 | +$CacheHandler->setRootPath("service_cache/"); // if file system handler: sys_get_temp_dir() . "/cache/service_cache/"; |
| 191 | +$CacheHandler->setDefaultTTL(60); // 60 seconds of cache ttl |
| 192 | +$CacheHandler->setDefaultType("php"); // type can be: "php" or "text" |
| 193 | + |
| 194 | +$prefix = "foo/bar/"; // optional |
| 195 | +$key = "my_data_key"; // Must be unique for each cached service. |
| 196 | + |
| 197 | +// Get cached contents |
| 198 | +if ($CacheHandler->isValid($prefix, $key)) // isValid($prefix, $key, $ttl = false, $type = false) |
| 199 | + $data = $CacheHandler->get($prefix, $key); // get($prefix, $key, $type = false) |
| 200 | +else { // Otherwise create cache for next time |
| 201 | + $data = array("foo" => "bar", "bar" => "foo"); // Contents to cache |
| 202 | + $CacheHandler->create($prefix, $key, $data); // create($prefix, $key, $data, $type = false) |
| 203 | +} |
| 204 | + |
| 205 | +// Then use $data... |
| 206 | + |
| 207 | +// Note: You can also relate services to other services and then delete those related services. |
| 208 | +// For more details, check the file examples/service_cache_with_relations.php |
| 209 | +``` |
| 210 | + |
| 211 | +### 5. Cache (Redis) |
| 212 | + |
| 213 | +A Redis usage sample is planned. |
| 214 | + |
| 215 | +```php |
| 216 | +// TODO: Redis usage sample |
| 217 | +``` |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## Further Examples |
| 222 | + |
| 223 | +The repository includes more detailed examples to guide your implementation: |
| 224 | +- [User Cache Example](examples/user_cache.php) |
| 225 | +- [Serialized Cache Example](examples/xml_settings_cache.php) |
| 226 | +- [Service Cache Example without Related Services](examples/service_cache.php) |
| 227 | +- [Service Cache Example with Related Services](examples/service_cache_with_relations.php) |
| 228 | + |
0 commit comments