Download the PHP package sobstel/metaphore without Composer
On this page you can find all versions of the php package sobstel/metaphore. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sobstel/metaphore
More information about sobstel/metaphore
Files in sobstel/metaphore
Package metaphore
Short Description PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).
License MIT
Homepage https://github.com/sobstel/metaphore
Informations about the package metaphore
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.
Installation
In composer.json file:
or just composer require sobstel/metaphore
Usage
Public API (methods)
-
__construct(ValueStoreInterface $valueStore, LockManager $lockManager = null)
cache($key, callable $callable, [$ttl, [$onNoStaleCacheCallable]])
- returns resultdelete($key)
getValue($key)
- returns Value objectsetResult($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.
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...
.. or use more advanced Metaphore\TTl
object, which gives you control over grace period and lock 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:
You can also affect value that is returned:
Tests
Run all tests: phpunit
.
If no memcached or/and redis installed: phpunit --exclude-group=notisolated
or phpunit --exclude-group=memcached,redis
.