2017 © Pedro Peláez
 

library cache

A simple caching library

image

kemist/cache

A simple caching library

  • Friday, September 15, 2017
  • by kemist80
  • Repository
  • 2 Watchers
  • 2 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Cache

Build Status Scrutinizer Code Quality Coverage Status Latest Stable Version License, (*1)

Caching library with the following features: - Extensible storage interface - Automatic expiration handling even if cache storage doesn't support it - Remembers when each cached item was read/written/accessed last time or written for the first time (created). - Counts how many times a cached item was read/written since creation - Counts cache hits (number of successful retrieving) - Counts cache misses (number of attempts to retrieve not existing or expired cache item) - Supports compressed storing - Tagged caching, (*2)

Installation

Via composer:, (*3)

{
    "require": {
        "kemist/cache": "~1.1"
    }
}

Usage

Initialization:, (*4)

<?php

require_once('vendor/autoload.php');
use Kemist\Cache\Cache as Cache;

$storage=new Kemist\Cache\Storage\FileStorage();
$cache=new Cache($storage);

Basic usage:, (*5)

// Stores a variable in cache
$cache->store('variable','test variable');

// Reads back a variable from cache with existence checking
if ($cache->has('variable')){
  echo $cache->get('variable');
}

// Deletes a variable from cache
$cache->delete('variable');

// Reads back a variable passing a default value in case it doesn't exist
echo $cache->get('variable','default value');

// Reads back a variable and deletes it from cache
$var=$cache->pull('variable');

// Deletes all cached variables
$cache->flush();

Compressed storing:, (*6)

// Stores a variable in cache compressed (in case current storage adapter supports it)
$cache->store('test.compressed','test variable',true);

// By reading back you don't need to know whether variable was compressed or not
echo $cache->get('test.compressed');

Specify cached variable expiry:, (*7)

// Stores a variable until manual deletion (default working)
$cache->store('test.manual','test variable',false,0);

// Stores a variable valid for 5 minutes (specified in seconds)
$cache->store('test.five_minutes','test variable',false,300);

// Stores a variable valid until specified date
$cache->store('test.concrete_date','test variable',false,'2015-01-01');

// Stores a variable valid for 2 weeks (you can use any valid date string)
$cache->store('test.two_weeks','test variable',false,'2weeks');

// Gets a cached item TTL (time to live: the difference between expiration and creation time in seconds)
echo $cache->getTTL('test.five_minutes');

// You can set TTL without modifying cached value
echo $cache->setTTL('test.manual',300);

// You can do the same with specifying the expiry date
echo $cache->setExpiry('test.manual','5 minutes');

Storing method:, (*8)

$object=new stdClass();
$object->property=5;
// By default every variable is stored serialized so even objects can be cached
$cache->store('test.object',$object,false,0,Cache::STORE_METHOD_SERIALIZE);
var_dump($cache->get('test.object'));

$array=array('a'=>'apple','b'=>'bike');
// You can switch storing method to JSON for arrays if you prefer
$cache->store('test.array',$array,false,0,Cache::STORE_METHOD_JSON);
// By reading back you don't need to know what was the storing method of the variable
var_dump($cache->get('test.array'));

Default value:, (*9)

// Get can return a default value if cached variable doesn't exist
echo $cache->get('test.default','default');

// You can pass even a closure
echo $cache->get('test.default',function(){return 'default';});

// Initial value setting (store default value if cached variable doesn't exist)
echo $cache->getOrStore('test.initial','initial',false,'3hours');

// The same with closure
echo $cache->getOrStore('test.initial',function(){return 'initial';},false,'3hours');

Tagged caching:, (*10)

// Sets cache value and assigns tags to it
$cache->storeTagged('test.tagged1','tagged',array('tag1','tag2'));
$cache->storeTagged('test.tagged2','tagged2','tag2');

// Retrieves cache values having the given tags
var_dump($cache->getTagged('tag2'));

// Adds more tags to the specified cache value
$cache->addTags('test.tagged2','tag3');

// Change tags without modifying cache value
$cache->setTags('test.tagged1',array('tag2','tag3','tag4'));

// Retrieves tags of a cached value
$cache->getTags('test.tagged1');

// Deletes cache variables having specified tags
var_dump($cache->deleteTagged('tag2'));

Cached variable statistics:, (*11)

// Displays cache creation time (first stored) in specified date format 
// (by default it returns a unix timestamp)
echo 'Created:'.$cache->getCreated('test.compressed','Y-m-d H:i:s');

// Displays cache expiry in specified date format
echo 'Expiry:'.$cache->getExpiry('test.two_weeks','Y-m-d H:i:s');

// Displays time when cached variable was last accessed (either read or write)
echo 'Last access:'.$cache->getLastAccess('test.array','Y-m-d H:i:s');

// Displays time when cached variable was last read
echo 'Last read:'.$cache->getLastRead('test.object','Y-m-d H:i:s');

// Displays time when cached variable was last written 
echo 'Last write:'.$cache->getLastWrite('test.array','Y-m-d H:i:s');

// Displays how many times cached value was written since creation
echo 'Write count:'.$cache->getWriteCount('variable');

// Displays how many times cached value was read since creation
echo 'Read count:'.$cache->getReadCount('test.compressed');

// Displays cache hits
echo 'Cache hits:'.$cache->getHits();

// Displays cache misses
echo 'Cache misses:'.$cache->getMisses();

// You can retrieve all of the above statistics at once
var_dump($cache->getInfo('test.object'));

// Displays all cached variable names
var_dump($cache->getKeys());

// Displays all cached variable names that were read in current runtime session
var_dump($cache->getReadKeys());

// Retrieves all tag values currently in use
var_dump($cache->getAllTags());

The Versions

15/09 2017

dev-develop

dev-develop

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

15/09 2017

dev-master

9999999-dev

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

15/09 2017

v1.1.3

1.1.3.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

15/09 2017

v1.1.2

1.1.2.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

15/09 2017

v1.1.1

1.1.1.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

15/09 2016

v1.1.0

1.1.0.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

13/01 2016

v1.0.8

1.0.8.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

09/02 2015

v1.0.7

1.0.7.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

06/02 2015

v1.0.6

1.0.6.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

17/10 2014

v1.0.5

1.0.5.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kemist

cache caching

29/09 2014

v1.0.4

1.0.4.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Kemist

cache caching

27/09 2014

v1.0.3

1.0.3.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Kemist

cache caching

27/09 2014

v1.0.2

1.0.2.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Kemist

cache caching

26/09 2014

v1.0.1

1.0.1.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Kemist

cache caching

25/09 2014

v1.0.0

1.0.0.0

A simple caching library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Kemist

cache caching