PHPackages                             juhara/zzzcache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. juhara/zzzcache

ActiveLibrary[Caching](/categories/caching)

juhara/zzzcache
===============

A minimalist cache system

v1.0.15(7y ago)1763Apache-2.0PHPPHP &gt;=5.4

Since Apr 12Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/zamronypj/zzzcache)[ Packagist](https://packagist.org/packages/juhara/zzzcache)[ RSS](/packages/juhara-zzzcache/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (8)Used By (3)

About ZzzCache
==============

[](#about-zzzcache)

A minimalist and simple PHP cache library implementation. Visit [Documentation](https://v3.juhara.com/zzzcache.html) for more information.

Requirement
===========

[](#requirement)

- [PHP &gt;= 7.2](https://php.net)
- [Composer](https://getcomposer.org)

Installation
============

[](#installation)

Run through composer

```
$ composer require juhara/zzzcache

```

How to use
==========

[](#how-to-use)

### Implement Cacheable interface

[](#implement-cacheable-interface)

Any class that can be stored in cache manager needs to implements `Juhara\ZzzCache\Contracts\Cacheable` interface, which is `data()` and `ttl()` method.

- `data()` method should return class data.
- `ttl()` should return integer value of time to live in millisecond. This value determine how long data will be kept in cache until considered expired.

When reading data from cache, cache manager relies on cache storage interface implementation to provide proper serialization/unserialization when read or write data.

There is one `Juhara\ZzzCache\Contracts\Cacheable` implementation provided, `Juhara\ZzzCache\Helpers\ClosureCacheable` class, which implements data as closure.

```
$ttl = 60 * 60 * 1; //cache item for 1 hour
$cacheableItem = new Juhara\ZzzCache\Helpers\ClosureCacheable(function () {
    return [
        'dummyData' => 'dummy data'
    ];
}, $ttl);

```

When `$cacheableItem->data()` is called, it calls closure function pass in constructor and return data that defined in closure.

`Juhara\ZzzCache\Helpers\ClosureCacheableFactory` class implements `Juhara\ZzzCache\Contracts\CacheableFactoryInterface` and acts as factory for `Juhara\ZzzCache\Helpers\ClosureCacheable` class.

```
$cacheableFactory = new \Juhara\ZzzCache\Helpers\ClosureCacheableFactory();
$cacheableItem = $cacheableFactory->build($data, $ttl);

```

Of course, you are free to implement your own.

### Initialize Cache instance

[](#initialize-cache-instance)

`Juhara\ZzzCache\Cache` class is default `Juhara\ZzzCache\Contracts\CacheInterface`implementation provided with this library.

To use it, you need to provide `Juhara\ZzzCache\Contracts\CacheStorageInterface`and `Juhara\ZzzCache\Contracts\ExpiryCalculatorInterface` implementation.

See [Storage Implementation](#storage-implementation) for available `CacheStorageInterface` implementation.

There is `Juhara\ZzzCache\Helpers\ExpiryCalculator` class which is default `ExpiryCalculatorInterface` implementation for this library. See [Example](#example)

```
