2017 © Pedro Peláez
 

library metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

image

sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  • Wednesday, November 8, 2017
  • by sobstel
  • Repository
  • 10 Watchers
  • 83 Stars
  • 35,889 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 2 Open issues
  • 12 Versions
  • 9 % Grown

The README.md

metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampeding herd or Slashdot effect)., (*1)

Problem: too many requests hit your website at the same time while it tries to regenerate same content slamming your database, eg. when cache expired., (*2)

Solution: first request generates new content while all the subsequent requests get (stale) content from cache until it's refreshed by the first request., (*3)

Read http://www.sobstel.org/blog/preventing-dogpile-effect/ for more details., (*4)

Buy Me a Coffee at ko-fi.com, (*5)

Installation

In composer.json file:, (*6)

"require": {
  "sobstel/metaphore": "2.0.*"
}

or just composer require sobstel/metaphore, (*7)

Usage

``` php use Metaphore\Cache; use Metaphore\Store\MemcachedStore;, (*8)

// initialize $memcached object (new Memcached()), (*9)

$cache = new Cache(new MemcachedStore($memcached)); $cache->cache('key', function() { // generate content }, 30);, (*10)


Public API (methods) -------------------- - `__construct(ValueStoreInterface $valueStore, LockManager $lockManager = null)` - `cache($key, callable $callable, [$ttl, [$onNoStaleCacheCallable]])` - returns result - `delete($key)` - `getValue($key)` - returns Value object - `setResult($key, $result, Ttl $ttl)` - sets result (without anti-dogpile-effect mechanism) - `onNoStaleCache($callable)` - `getValueStore()` - `getLockManager()` Value store vs lock store ------------------------- Cache values and locks can be handled by different stores. ``` php $valueStore = new Metaphore\MemcachedStore($memcached); $lockStore = new Your\Custom\MySQLLockStore($connection); $lockManager = new Metaphore\LockManager($lockStore); $cache = new Metaphore\Cache($valueStore, $lockManager);

By default - if no 2nd argument passed to Cache constructor - value store is used as a lock store., (*11)

Sample use case might be to have custom MySQL GET_LOCK/RELEASE_LOCK for locks and still use in-built Memcached store for storing values., (*12)

Time-to-live

You can pass simple integer value..., (*13)

``` php $cache->cache('key', callback, 30); // cache for 30 secs, (*14)


.. or use more advanced `Metaphore\TTl` object, which gives you control over grace period and lock ttl. ``` php // $ttl, $grace_ttl, $lock_ttl $ttl = new Ttl(30, 60, 15); $cache->cache('key', callback, $ttl);
  • $ttl - regular cache time (in seconds)
  • $grace_ttl - grace period, how long to allow to serve stale content while new one is being generated (in seconds), similar to HTTP's stale-while-revalidate, default is 60s
  • $lock_ttl - lock time, how long to prevent other request(s) from generating same content, default is 5s

Ttl value is added to current timestamp (time() + $ttl)., (*15)

No stale cache

In rare situations, when cache gets expired and there's no stale (generated earlier) content available, all requests will start generating new content., (*16)

You can add listener to catch this:, (*17)

``` php $cache->onNoStaleCache(function (NoStaleCacheEvent $event) { Logger::log(sprintf('no stale cache detected for key %s', $event->getKey())); });, (*18)


You can also affect value that is returned: ``` php $cache->onNoStaleCache(function (NoStaleCacheEvent $event) { $event->setResult('new custom result'); });

Tests

Run all tests: phpunit., (*19)

If no memcached or/and redis installed: phpunit --exclude-group=notisolated or phpunit --exclude-group=memcached,redis., (*20)

The Versions

08/11 2017

dev-master

9999999-dev https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

08/11 2017

v1.2.5

1.2.5.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

06/11 2015

v1.2.4

1.2.4.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

25/08 2015

v1.2.3

1.2.3.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

04/03 2015

v1.2.2

1.2.2.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

20/02 2015

v1.2.1

1.2.1.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

20/01 2015

v1.2.0

1.2.0.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

11/11 2014

v1.1.0

1.1.0.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

cache

07/11 2014

v1.0.1

1.0.1.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

 

cache

08/10 2014

v1.0.0

1.0.0.0 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

 

cache

28/09 2014

v1.0.0-RC2

1.0.0.0-RC2 https://github.com/sobstel/metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

 

cache

27/09 2014

v1.0.0-BETA1

1.0.0.0-beta1 https://github.com/sobstel/metaphore

PHP cache slam defense using (memcached) semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).

  Sources   Download

MIT

The Requires

 

cache