2017 © Pedro Peláez
 

library json-disk-cache

Cache class intended to keep serialized data in JSON format files

image

spiechu/json-disk-cache

Cache class intended to keep serialized data in JSON format files

  • Sunday, December 3, 2017
  • by spiechu
  • Repository
  • 1 Watchers
  • 7 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 55 % Grown

The README.md

JSON Disk Cache

Build Status Code Coverage Scrutinizer Code Quality, (*1)

Cache class intended to keep serialized data in JSON format files., (*2)

There is one cache directory per all domains. hashtable.cache keeps cache hashes of all domains. domain.cache keeps hashes with serialized data., (*3)

Features

Class caches data on disk using JSON format. Uses typical methods set(), get(), clear()., (*4)

Caches are grouped in domains. Each domain is contained in one file. Hashtable with pairs: name and (optional) params are stashed in file., (*5)

What is diffrent is method getSet which accepts array with object, method name and optionable params to execute when cache is not present., (*6)

Additionally any set cache has its own valid time, which can be set globally or per set()., (*7)

Requirements

You need at least PHP 5.4 since library uses shorthand array creation [] and (new Object)->method() construction. If You want to run unit tests, PHPUnit 3.6 is needed., (*8)

Installation

Library needs to be registered for autoload. It uses standard SplClassLoader, for example:, (*9)

<?php
require_once 'SplClassLoader.php';
$classLoader = new SplClassLoader('Spiechu\JSONDiskCache' , 'src');
$classLoader->register();

Usage

At creation of JSONDiskCache object You can set cache directory in first param, otherwise jsoncache in JSONDiskCache.php directory will be set. Second param is default domain. Then You can set default cache valid time. Example below:, (*10)

<?php
$JSONDiskCache = new JSONDiskCache(__DIR__ . DIRECTORY_SEPARATOR . 'mycachedir', 'my_domain');

// set global cache expiration time in seconds
$JSONDiskCache->setValidTime(10);

There are a few ways to set cache entry:, (*11)

<?php
$JSONDiskCache->set('valueName', 'value to cache');

// additionally You can set value expiration time in seconds
// global cache expiration is suppressed
$JSONDiskCache->set('valueName', 'value to cache for 5 seconds', 5);

// the same cache name can be distinguished by used params
// in this case You can pass an array to set()
$JSONDiskCache->set(['valueName', 'myParam'], 'value to cache for 5 seconds', 5);

Now You can retrieve value from cache, notice that null value is treated as no cache at all:, (*12)

<?php
$cachedValue = $JSONDiskCache->get('valueName');

// when array has been passed to set(), the same construction is needed in get()
$cachedValue = $JSONDiskCache->get(['valueName', 'myParam']);

You can also clear cache, true means cache was found and deleted:, (*13)

<?php
$isCleared = $JSONDiskCache->clear('valueName');

// and again with param
$isCleared = $JSONDiskCache->clear(['valueName', 'myParam']);

Suppose You have $db object that retrieves data from database with $db->fetchData(1) method. When valid cache for pair array('dataName', 1) is found then cache is used, otherwise $db->fetchData(1):, (*14)

<?php
$value = JSONDiskCache->getSet(['dataName', 1], [$db, 'fetchData'], [1]);

To save cache to a file just do nothing. Just before object is being destroyed, its destructor will save cache to cache files. To force file write You can do:, (*15)

<?php

// this will work, but You lost object too
unset($JSONDiskCache);

// save only hashes
$JSONDiskCache->saveHashTableToFile();

// save cache to file
$JSONDiskCache->saveCacheToFile();

Before cache is written to file, some maintenance is being performed automatically when cache entries numbers are above threshold. You can always do a function call:, (*16)

<?php

// iterates over all domains and tries to eliminate old cache entries
$JSONDiskCache->cleanUpCache();

// clean only certain domain
$JSONDiskCache->removeOldCacheEntries('domain to clean');

The Versions

03/12 2017

dev-master

9999999-dev https://github.com/spiechu/JSON-Disk-Cache

Cache class intended to keep serialized data in JSON format files

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

cache json caching

03/12 2017

0.2.1

0.2.1.0 https://github.com/spiechu/JSON-Disk-Cache

Cache class intended to keep serialized data in JSON format files

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

cache json caching

15/07 2012

0.2.0

0.2.0.0 https://github.com/spiechu/JSON-Disk-Cache

Cache class intended to keep serialized data in JSON format files

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache json caching

09/07 2012

0.1.1

0.1.1.0 https://github.com/spiechu/JSON-Disk-Cache

Cache class intended to keep serialized data in JSON format files

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache json

08/07 2012

0.1.0

0.1.0.0 https://github.com/spiechu/JSON-Disk-Cache

Cache class intended to keep serialized data in JSON format files

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache json