Coder Social home page Coder Social logo

seldaek / scrapbook Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matthiasmullie/scrapbook

0.0 3.0 1.0 2.96 MB

PHP caching environment, with adapters for e.g. Memcached, Redis, Couchbase, APC, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

Home Page: http://www.scrapbook.cash

License: MIT License

Makefile 0.08% PHP 98.70% Shell 1.22%

scrapbook's Introduction

Scrapbook PHP cache

Build status Code coverage Code quality Latest version Downloads total License

Documentation: http://www.scrapbook.cash - API reference: http://docs.scrapbook.cash

Adapters

Memcached, Redis, Couchbase, APC, MySQL, SQLite, PostgreSQL, Flysystem, MemoryStore

Interfaces

3 interfaces are available & they work with all adapters.

KeyValueStore

KeyValueStore is inspired by the Memcached API (driver model). It'll let you do the most advanced cache operations & is easiest to use.

Here's an example:

// create \Memcached object pointing to your Memcached server
$client = new \Memcached();
$client->addServer('localhost', 11211);
// create Scrapbook cache object
// (example with Memcached, but any adapter works the same)
$cache = new \MatthiasMullie\Scrapbook\Adapters\Memcached($client);

// set a value
$cache->set('key', 'value'); // returns true

// get a value
$cache->get('key'); // returns 'value'

A detailed list of the KeyValueStore interface & it's methods can be found in the documentation.

PSR-6 CacheItemPoolInterface & CacheItemInterface

PSR-6 (a PHP-FIG standard) is a different approach (pool model): there's 1 class to interact with the cache backend (Pool) & one to represent the cache value (Item). This interface is supported by multiple cache implementations, so there won't be any vendor lock-in

However, it doesn't offer much more than basic get, set & delete functionality (which is quite often more than enough!)

// boilerplate code example with Memcached, but any
// MatthiasMullie\Scrapbook\KeyValueStore adapter will work
$client = new \Memcached();
$client->addServer('localhost', 11211);
$cache = new \MatthiasMullie\Scrapbook\Adapters\Memcached($client);

// create Pool object from cache engine
$pool = new \MatthiasMullie\Scrapbook\Psr6\Pool($cache);

// get item from Pool
$item = $pool->getItem('key');

// get item value
$value = $item->get();

// ... or change the value & store it to cache
$item->set('updated-value');
$pool->save($item);

A detailed list of the PSR-6 interface & it's methods can be found in the documentation.

PSR-16 CacheInterface & CounterInterface

PSR-16 is a second PHP-FIG cache standard. It's a driver model just like KeyValueStore, and it works very much in the same way. This interface is supported by multiple cache implementations, so there won't be any vendor lock-in

However, it also doesn't offer much more than basic get, set & delete functionality (which is quite often more than enough!)

// boilerplate code example with Memcached, but any
// MatthiasMullie\Scrapbook\KeyValueStore adapter will work
$client = new \Memcached();
$client->addServer('localhost', 11211);
$cache = new \MatthiasMullie\Scrapbook\Adapters\Memcached($client);

// create Simplecache object from cache engine
$simplecache = new \MatthiasMullie\Scrapbook\Psr16\SimpleCache($cache);

// get value from cache
$value = $simplecache->get('key');

// ... or store a new value to cache
$simplecache->set('key', 'updated-value');

A detailed list of the PSR-16 interface & it's methods can be found in the documentation.

Extras

Local buffer

BufferedStore helps avoid repeat requests to your real cache by keeping the value in memory. That way, you don't have to do that in your application - just keep querying that cache!

Transactions

Just like database transactions, TransactionalStore lets you defer cache writes to a later point in time, until you're ready to commit all of it (or rollback.) You can even nest multiple transactions!

Stampede protection

Cache stampedes can happen when you get a sudden surge of traffic but the data is not yet in cache. StampedeProtector will make sure that only 1 request will generate the result & the others just wait until it pops up in cache, instead of cripling your servers.

Sharding

When you have too much data for (or requests to) 1 little server, this'll let you shard it over multiple cache servers. All data will automatically be distributed evenly across your server pool, so all the individual cache servers only get a fraction of the data & traffic.

Installation

Simply add a dependency on matthiasmullie/scrapbook to your composer.json file if you use Composer to manage the dependencies of your project:

composer require matthiasmullie/scrapbook

Although it's recommended to use Composer, you can actually include these files anyway you want.

Just take a look at this "build your cache" section to generate the exact configuration you'd like to use (adapter, interface, extras) and some example code.

License

Scrapbook is MIT licensed.

scrapbook's People

Contributors

curry684 avatar hansott avatar matthiasmullie avatar mindplay-dk avatar nyholm avatar

Watchers

 avatar  avatar  avatar

Forkers

enterstudio

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.