2017 © Pedro Peláez
 

library cache

SugiPHP Cache Component

image

sugiphp/cache

SugiPHP Cache Component

  • Tuesday, August 9, 2016
  • by tzappa
  • Repository
  • 2 Watchers
  • 2 Stars
  • 465 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

Cache

Build Status Scrutinizer Code Quality, (*1)

SugiPHP cache component provides a simple and unified API for several caching systems like APC, Memcached and file based cache., (*2)

One of the most important features of the caching systems is invalidating items after a specified period of time. This will give you the ability to cache some time consuming queries like the total number of adverts in your site, and not worry about if some advert is deleted or some adverts are posted, but instead set a time after which that number is invalidated and needs to be refreshed., (*3)

One other feature is that no matter if the store is actually running (caching items) or not it will not produce any errors or exceptions. Instead it will return NULL on any get requests. In the above example this means that your code will be fooled to count adverts every time, maybe slowing down performance, but still working. Your code will still work on some development or testing environments where no cache is available nor is needed., (*4)

Installation

composer require sugiphp/cache ~1.0

Settings

  • Using APC (APCu) as a cache:
add("foo", "bar");
?>
  • Using file-based cache:

  • Using Memcached:
addServer("127.0.0.1", 11211);

// make a store
$mcStore = new MemcachedStore($memcached);
$cache = new Cache($mcStore);
?>
  • NullStore and ArrayStore

Those two classes are not real cache storages, but they are usefull on development environments and for unit tests. ArrayStore caches values only for a lifetime of the script, and thus the expiration time is not implemented. All store values will be flushed after the script is over., (*5)


NullStore is actually a fake store. It is used to check your code is operational even if there is no cache storages available or if there is a problem with existing ones, e.g. no space left on the server or there is no connection with Memcached. Other use is when you wish your code to work without any caching for a while. Especially usefull on development when any caching mechanism can be a disadvantage., (*6)

set("foo", "bar");
$cache->get("foo"); // will return NULL
?>

Usage

Caching is done by setting a key-value pairs in a store., (*7)

Store a value in a cache

<?php
$cache->set("foo", "bar");    // store a value for a maximum allowed time
$cache->set("foo", "foobar"); // store a new value with the same key
?>

Add a value if it is not already been stored

<?php
$cache->add("key", "foo");    // this will store a value
$cache->add("key", "foobar"); // this will fail
?>

Retrieve value from the cache

<?php
$cache->set("key", "value", 60); // store a value for 60 seconds
$cache->get("key"); // this will return "value" if 60 seconds are not passed and NULL after that
$cache->get("baz"); // will return NULL
?>

Delete a value

<?php
$cache->delete("key");
?>

Check value is set

<?php
$cache->has("key"); // will return TRUE if the "key" was set and the expiration time was not passed, and FALSE otherwise
?>

Increment / decrement value

<?php
$cache->set("num", 1); // store a numeric value
$cache->inc("num"); // will return 2
$cache->inc("num", 10); // will return 12
$cache->dec("num"); // will return 11
?>

The Versions

09/08 2016

dev-master

9999999-dev

SugiPHP Cache Component

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Plamen Popov

cache memcache apc memcached

09/08 2016

1.1.0

1.1.0.0

SugiPHP Cache Component

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Plamen Popov

cache memcache apc memcached

16/12 2014

1.0.0

1.0.0.0

SugiPHP Cache Component

  Sources   Download

MIT

The Requires

  • php >=5.3

 

by Plamen Popov

cache memcache apc sugiphp