• Stars
    star
    101
  • Rank 336,947 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 11 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cache slam defense using a semaphore to prevent dogpile effect.

metaphore

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

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.

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

Read http://www.sobstel.org/blog/preventing-dogpile-effect/ for more details.

Buy Me a Coffee at ko-fi.com

Installation

In composer.json file:

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

or just composer require sobstel/metaphore

Usage

use Metaphore\Cache;
use Metaphore\Store\MemcachedStore;

// initialize $memcached object (new Memcached())

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

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.

$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.

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.

Time-to-live

You can pass simple integer value...

$cache->cache('key', callback, 30); // cache for 30 secs

.. or use more advanced Metaphore\TTl object, which gives you control over grace period and lock ttl.

// $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).

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.

You can add listener to catch this:

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

You can also affect value that is returned:

$cache->onNoStaleCache(function (NoStaleCacheEvent $event) {
    $event->setResult('new custom result');
});

Tests

Run all tests: phpunit.

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

More Repositories

1

sequent

Short-term memory training game (mobile). Built with React Native and Redux.
JavaScript
89
star
2

sesshin

PHP secure advanced session manager.
PHP
66
star
3

jsonp.js

Lightweight JSONP library for cross-domain ajax calls.
JavaScript
36
star
4

golazon-legacy

Football data mnmlist way. Built with Next.js and Ruby.
TypeScript
33
star
5

hydropuzzle

Surreal tech adventure game for iOS and Android. Built with React Native.
JavaScript
19
star
6

react-native-animatable-unmountable

Declarative transitions and animations for React Native (with unmount effect).
JavaScript
15
star
7

AsyncHTTP

PHP asynchronous non-blocking HTTP requests.
PHP
4
star
8

albicelestes

¡Vamos Argentina! 🇦🇷⚽
TypeScript
3
star
9

calmbook

Facebook pages without distractions
TypeScript
3
star
10

Execution

PHP class to handle unrecoverable fatal errors. (use Monolog instead)
PHP
2
star
11

Imaged

Image mainpulation lib. Object-oriented wrapper for PHP GD2 library.
PHP
2
star
12

emolRTE

Unbloated Rich Text Editor for jQuery.
JavaScript
2
star
13

agnostic

not-really-ORM [ON HOLD]
PHP
2
star
14

shed

PHP Dependency Injection container
PHP
1
star
15

voltpuzzle

Sequel to Hydropuzzle. Built with React Native and Expo. (unfinished :/)
JavaScript
1
star
16

jsondocr

DSL for documenting JSON structure.
Ruby
1
star
17

XReflect

Object Oriented PHP Reflection to XML (generator).
PHP
1
star
18

locals

Order form for local stores grounded by COVID-19 (managed from plain simple google spreadsheet).
TypeScript
1
star
19

sobstel

Ruby
1
star
20

sobstel.github.com

HTML
1
star
21

scru.js

Lightweight execution dependency manager (lazy loading with dependencies).
JavaScript
1
star