All Projects → sobstel → Metaphore

sobstel / Metaphore

Licence: mit
Cache slam defense using a semaphore to prevent dogpile effect.

Labels

Projects that are alternatives of or similar to Metaphore

Rememberable
Query caching for Laravel
Stars: ✭ 960 (+900%)
Mutual labels:  caching
Cucache
Fast PUT/GET/DELETE in-memory key-value store for lookaside caching
Stars: ✭ 63 (-34.37%)
Mutual labels:  caching
Typeorm Loader
A database-aware data-loader for use with GraphQL and TypeORM.
Stars: ✭ 86 (-10.42%)
Mutual labels:  caching
Cachex
A powerful caching library for Elixir with support for transactions, fallbacks and expirations
Stars: ✭ 985 (+926.04%)
Mutual labels:  caching
Ansible Role Memcached
Ansible Role - Memcached
Stars: ✭ 54 (-43.75%)
Mutual labels:  caching
Hazelcast Cpp Client
Hazelcast IMDG C++ Client
Stars: ✭ 67 (-30.21%)
Mutual labels:  caching
Stl.fusion
Get real-time UI updates in Blazor apps and 10-1000x faster API responses with a novel approach to distributed reactive computing. Fusion brings computed observables and automatic dependency tracking from Knockout.js/MobX/Vue to the next level by enabling a single dependency graph span multiple servers and clients, including Blazor apps running in browser.
Stars: ✭ 858 (+793.75%)
Mutual labels:  caching
Serverless Api Gateway Caching
A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
Stars: ✭ 92 (-4.17%)
Mutual labels:  caching
Ymcache
YMCache is a lightweight object caching solution for iOS and Mac OS X that is designed for highly parallel access scenarios.
Stars: ✭ 58 (-39.58%)
Mutual labels:  caching
Minicache
📦 Python memory caching utilities
Stars: ✭ 79 (-17.71%)
Mutual labels:  caching
Synchrotron
Caching layer load balancer.
Stars: ✭ 42 (-56.25%)
Mutual labels:  caching
Fastcache
Fast thread-safe inmemory cache for big number of entries in Go. Minimizes GC overhead
Stars: ✭ 1,051 (+994.79%)
Mutual labels:  caching
Cashew
A simple and elegant yet powerful HTTP client cache for .NET
Stars: ✭ 70 (-27.08%)
Mutual labels:  caching
Clusteredbigcache
golang bigcache with clustering as a library.
Stars: ✭ 37 (-61.46%)
Mutual labels:  caching
Quell
Quell is an easy-to-use, lightweight JavaScript library providing a client- and server-side caching solution for GraphQL. Use Quell to prevent redundant client-side API requests and to minimize costly server-side response latency.
Stars: ✭ 90 (-6.25%)
Mutual labels:  caching
Goodies
Useful stuff missing from .NET for example duck typing, CSP channels, caching, money, typed ids...
Stars: ✭ 11 (-88.54%)
Mutual labels:  caching
Efsecondlevelcache
Entity Framework 6.x Second Level Caching Library.
Stars: ✭ 63 (-34.37%)
Mutual labels:  caching
Hazelcast Python Client
Hazelcast IMDG Python Client
Stars: ✭ 92 (-4.17%)
Mutual labels:  caching
Trickster
Open Source HTTP Reverse Proxy Cache and Time Series Dashboard Accelerator
Stars: ✭ 1,306 (+1260.42%)
Mutual labels:  caching
Tortilla
Wrapping web APIs made easy.
Stars: ✭ 1,215 (+1165.63%)
Mutual labels:  caching

metaphore

PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampending 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.

Scrutinizer Code Quality Build Status

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].