Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/micropackage/cache
WordPress cache wrapper
https://github.com/micropackage/cache
cache-driver composer-library micropackage wordpress wordpress-cache
Last synced: about 2 months ago
JSON representation
WordPress cache wrapper
- Host: GitHub
- URL: https://github.com/micropackage/cache
- Owner: micropackage
- License: mit
- Created: 2020-01-06T13:00:18.000Z (about 5 years ago)
- Default Branch: develop
- Last Pushed: 2023-06-26T20:24:05.000Z (over 1 year ago)
- Last Synced: 2024-11-24T09:38:48.740Z (2 months ago)
- Topics: cache-driver, composer-library, micropackage, wordpress, wordpress-cache
- Language: PHP
- Size: 54.7 KB
- Stars: 14
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache
[![BracketSpace Micropackage](https://img.shields.io/badge/BracketSpace-Micropackage-brightgreen)](https://bracketspace.com)
[![Latest Stable Version](https://poser.pugx.org/micropackage/cache/v/stable)](https://packagist.org/packages/micropackage/cache)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/micropackage/cache.svg)](https://packagist.org/packages/micropackage/cache)
[![Total Downloads](https://poser.pugx.org/micropackage/cache/downloads)](https://packagist.org/packages/micropackage/cache)
[![License](https://poser.pugx.org/micropackage/cache/license)](https://packagist.org/packages/micropackage/cache)
## 🧬 About Cache
This micropackage is a wrapper for WordPress cache with two drivers implemented:
- [WP Object Cache API](https://codex.wordpress.org/Class_Reference/WP_Object_Cache)
- [Transients API](https://codex.wordpress.org/Transients_API)It provides a unified, object-oriented way to manipulate WordPress Cache, witch the Cache manipulator object for even easier setting and getting the cache.
## 💾 Installation
``` bash
composer require micropackage/cache
```## 🕹 Usage
### Object Cache
Constructing the Object Cache driver:
```php
use Micropackage\Cache\Driver\ObjectCache;$object_cache = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS );
$object_cache->set_key( 'cache_key' );
```Group parameter allows you to store the cache under the same key across multiple groups. Default is empty string.
By default the expiration is set to `0` which means the transient never expires.
#### Available methods
| Method | Description | Returns |
| ----------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| ```set_key( (string) $key )``` | Sets the cache key | $this |
| ```get_key()``` | Gets the cache key | (string) Cache key |
| ```set_group( (string) $group )``` | Sets the cache group | $this |
| ```get_group``` | Gets the cache group | (string) Default empty string |
| ```set_expiration( (int) $expiration )``` | Sets the cache expiration in seconds | $this |
| ```get_expiration()``` | Gets the cache expiration | (int) Expiration seconds
Default 0 which means the cache
doesn't expire |
| ```set( (mixed) $value )``` | Sets the cache | void |
| ```add( (mixed) $value )``` | Sets the cache
only if it wasn't set before | void |
| ```get()``` | Gets the cached value | mixed\|false
False if not set |
| ```force_get()``` | Gets the cached value
and updates the local cache
from persistent cache | mixed\|false
False if not set |
| ```delete()``` | Deletes the cache | void |### Transient Cache
Constructing the Transient Cache driver:
```php
use Micropackage\Cache\Driver\Transient;$transient_cache = new Transient( $expiration = DAY_IN_SECONDS );
$transient_cache->set_key( 'cache_key' );
```By default the expiration is set to `0` which means the transient never expires.
#### Available methods
| Method | Description | Returns |
| ----------------------------------------- | -------------------------------------------- | ------------------------------------------------------------ |
| ```set_key( (string) $key )``` | Sets the cache key | $this |
| ```get_key()``` | Gets the cache key | (string) Cache key |
| ```set_expiration( (int) $expiration )``` | Sets the cache expiration in seconds | $this |
| ```get_expiration()``` | Gets the cache expiration | (int) Expiration seconds
Default 0 which means the cache
doesn't expire |
| ```set( (mixed) $value )``` | Sets the cache | void |
| ```add( (mixed) $value )``` | Sets the cache only if it wasn't set before | void |
| ```get()``` | Gets the cached value | mixed\|false
False if not set |
| ```delete()``` | Deletes the cache | void |### Cache manipulator
The Cache manipulator object allows you to use the `collect` method to easily get/store the cache value.
See the below example with Object Cache (you can pass the Transient Driver as well).
```php
use Micropackage\Cache\Cache;
use Micropackage\Cache\Driver\ObjectCache;$driver = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS );
$cache = new Cache( $driver, $cache_key = 'extremaly_important_thing' );$the_thing = $cache->collect( function() {
return 'The value was not set apparently';
} );
```The `collect` method takes a callable function as an argument. If the cache wasn't set for the key provided in cache construtor, the callable is called which should return the value for cache. The value is stored and returned.
Using variables from outside the callable:
```php
$some_var = 'I am awesome!';$the_thing = $cache->collect( function() use ( $some_var ) {
return $some_var;
} );
```## 📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by [BracketSpace](https://bracketspace.com).
## 📖 Changelog
[See the changelog file](./CHANGELOG.md).
## 📃 License
This software is released under MIT license. See the [LICENSE](./LICENSE) file for more information.