Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rapidwebltd/caching-object-wrapper
🎁 Wraps up any PHP object so all its methods are cached.
https://github.com/rapidwebltd/caching-object-wrapper
cache caching php php-library psr-6 wrapper
Last synced: about 1 month ago
JSON representation
🎁 Wraps up any PHP object so all its methods are cached.
- Host: GitHub
- URL: https://github.com/rapidwebltd/caching-object-wrapper
- Owner: rapidwebltd
- License: lgpl-3.0
- Created: 2018-03-21T13:19:23.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-22T11:41:00.000Z (almost 7 years ago)
- Last Synced: 2024-04-13T12:59:20.447Z (8 months ago)
- Topics: cache, caching, php, php-library, psr-6, wrapper
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🎁 Caching Object Wrapper
[![Build Status](https://travis-ci.org/rapidwebltd/caching-object-wrapper.svg?branch=master)](https://travis-ci.org/rapidwebltd/caching-object-wrapper)
[![Coverage Status](https://coveralls.io/repos/github/rapidwebltd/caching-object-wrapper/badge.svg?branch=master)](https://coveralls.io/github/rapidwebltd/caching-object-wrapper?branch=master)
[![StyleCI](https://styleci.io/repos/126181707/shield?branch=master)](https://styleci.io/repos/126181707)Wraps up any PHP object so all its methods are cached.
## Installation
You can install this package easily via Composer. Just run the following command from the root of your project.
```
composer require rapidwebltd/caching-object-wrapper
```## Usage
Here is a very simple usage example, showing how we can easily cache a simple object.
```php
use RapidWeb\CachingObjectWrapper\CachingObjectWrapper;
use rapidweb\RWFileCachePSR6\CacheItemPool;// This is an example class that we are going to use. It just generates random numbers.
class RandomNumberGenerator
{
public function generate()
{
return rand();
}
}// First, we need to install and setup a PSR6 cache item pool.
// As an example, we'll use the PSR-6 adapter for RW File Cache.// $ composer require rapidwebltd/rw-file-cache-psr-6
$cache = new CacheItemPool();
$cache->changeConfig(
[
'cacheDirectory' => '/tmp/cow-tests/',
'gzipCompression' => true,
]
);// Next, we can wrap up a new RandomNumberGenerator.
// We also pass in the $cache object we just created, and the desired cache expiry time in seconds.$randomNumberGenerator = new CachingObjectWrapper(new RandomNumberGenerator(), $cache, 60 * 60);
// That's it!
// To test, we can tell the wrapped object to generate us two random numbers.$randomNumber1 = $randomNumberGenerator->generate();
$randomNumber2 = $randomNumberGenerator->generate();// Due to our caching, $randomNumber1 and $randomNumber2 should be identical.
```