PHP 缓存库,包含适配器,例如 Memcached、Redis、Couchbase、APC(u)、SQL 以及基于这些的其他功能(例如事务、防堵塞保护)
PHP 缓存库,包含适配器,例如 Memcached、Redis、Couchbase、APC(u)、SQL 以及基于这些的其他功能(例如事务、防堵塞保护)
Documentation: https://www.scrapbook.cash - API reference: https://docs.scrapbook.cash
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/scrapbookThe exact bootstrapping will depend on which adapter, features and interface you will want to use, all of which are detailed below.
This library is built in layers that are all KeyValueStore implementations that you can wrap inside one another if you want to add more features.
Here's a simple example: a Memcached-backed psr/cache with stampede protection.
…Just take a look at this "build your cache" section to generate the exact configuration you'd like to use (adapter, interface, features) and some example code.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
The PECL Memcached extension is used
to interface with the Memcached server. Just provide a valid \Memcached object
to the Memcached adapter:
// create \Memcached object pointing to your Memcached server
$client = new \Memcached();
$client->addServer('localhost', 11211);
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\Memcached($client);Redis is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.
The PECL Redis extension is used
to interface with the Redis server. Just provide a valid \Redis object
to the Redis adapter:
// create \Redis object pointing to your Redis server
$client = new \Redis();
$client->connect('127.0.0.1');
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\Redis($client);Engineered to meet the elastic scalability, consistent high performance, always-on availability, and data mobility requirements of mission critical applications.
The PECL Couchbase extension and
couchbase/couchbase package
are used to interface with the Couchbase server. Just provide valid
\Couchbase\Collection, \Couchbase\Management\BucketManager and
\Couchbase\Bucket objects to the Couchbase adapter:
// create \Couchbase\Bucket object pointing to your Couchbase server
$options = new \Couchbase\ClusterOptions();
$options->credentials('username', 'password');
$cluster = new \Couchbase\Cluster('couchbase://localhost', $options);
$bucket = $cluster->bucket('default');
$collection = $bucket->defaultCollection();
$bucketManager = $cluster->buckets();
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\Couchbase($collection, $bucketManager, $bucket);APC is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
With APC, there is no "cache server", the data is just cached on the executing machine and available to all PHP processes on that machine. The PECL APCu extension can be used.
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\Apc();MySQL is the world's most popular open source database. MySQL can cost-effectively help you deliver high performance, scalable database applications.
While a database is not a genuine cache, it can also serve as key-value store. Just don't expect the same kind of performance you'd expect from a dedicated cache server.
But there could be a good reason to use a database-based cache: it's convenient if you already use a database, and it may have other benefits (like persistent storage, replication.)
// create \PDO object pointing to your MySQL server
$client = new PDO('mysql:dbname=cache;host=127.0.0.1', 'root', '');
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\MySQL($client);PostgreSQL has a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.
While a database is not a genuine cache, it can also serve as key-value store. Just don't expect the same kind of performance you'd expect from a dedicated cache server.
But there could be a good reason to use a database-based cache: it's convenient if you already use a database, and it may have other benefits (like persistent storage, replication.)
// create \PDO object pointing to your PostgreSQL server
$client = new PDO('pgsql:user=postgres dbname=cache password=');
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\PostgreSQL($client);SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
While a database is not a genuine cache, it can also serve as key-value store. Just don't expect the same kind of performance you'd expect from a dedicated cache server.
// create \PDO object pointing to your SQLite server
$client = new PDO('sqlite:cache.db');
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\SQLite($client);While it's not the fastest kind of cache in terms of I/O access times, opening a file usually still beats redoing expensive computations.
The filesystem-based adapter uses league\flysystem to abstract away the file
operations, and will work with all kinds of storage that league\filesystem
provides.
// create Flysystem object
$adapter = new \League\Flysystem\Local\LocalFilesystemAdapter('/path/to/cache', null, LOCK_EX);
$filesystem = new \League\Flysystem\Filesystem($adapter);
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\Flysystem($filesystem);PHP can keep data in memory, too! PHP arrays as storage are particularly useful to run tests against, since you don't have to install any other service.
Stuffing values in memory is mostly useless: they'll drop out at the end of the request. Unless you're using these cached values more than once in the same request, cache will always be empty and there's little reason to use this cache.
However, it is extremely useful when unittesting: you can run your entire test suite on this memory-based store, instead of setting up cache services and making sure they're in a pristine state...
// create Scrapbook KeyValueStore object
$cache = new \MatthiasMullie\Scrapbook\Adapters\MemoryStore();In addition to the default cache functionality (like get & set), Scrapbook
comes with a few neat little features. These are all implemented in their own
little object that implements KeyValueStore, and wraps around a KeyValueStore.
In other words, any feature can just be wrapped inside another one or on top
of any adapter.
BufferedStore helps reduce requests to your real cache. If you need to request the same value more than once (from various places in your code), it can be a pain to keep that value around. Requesting it again from cache would be easier, but then you get some latency from the connection to the cache server.
BufferedStore will keep known values (items that you've already requested or written yourself) in memory. Every time you need that value in the same request, it'll just get it from memory instead of going out to the cache server.
Just wrap the BufferedStore layer around your adapter (or other features):
// create buffered cache layer over our real cache
$cache = new \MatthiasMullie\Scrapbook\Buffered\BufferedStore($cache);TransactionalStore makes it possible to defer writes to a later point in time. Similar to transactions in databases, all deferred writes can be rolled back or committed all at once to ensure the data that is stored is reliable and complete. All of it will be stored, or nothing at all.
You may want to process code throughout your codebase, but not commit any changes until everything has successfully been validated & written to permanent storage.
While inside a transaction, you don't have to worry about data consistency. Inside a transaction, even if it has not yet been committed, you'll always be served the value you intend to store. In other words, when you write a new value to cache but have not yet committed it, you'll still get that value when you query for it. Should you rollback, or fail to commit (because data stored by another process caused your commit to fail), then you'll get the original value from cache instead of the one your intended to commit.
Just wrap the TransactionalStore layer around your adapter (or other features):
// create transactional cache layer over our real cache
$cache = new \MatthiasMullie\Scrapbook\Buffered\TransactionalStore($cache);And TA-DA, you can use transactions!
// begin a transaction
$cache->begin();
// set a value
// it won't be stored in real cache until commit() is called
$cache->set('key', 'value'); // returns true
// get a value
// it won't get it from the real cache (where it is not yet set), it'll be read
// from PHP memory
$cache->get('key'); // returns 'value'
// now commit write operations, this will effectively propagate the update to
// 'key' to the real cache
$cache->commit();
// ... or rollback, to discard uncommitted changes!
$cache->rollback();A cache stampede happens when there are a lot of requests for data that is not currently in cache, causing a lot of concurrent complex operations. For example:
In those cases, this huge amount of requests for data that is not in cache at that time causes that expensive operation to be executed a lot of times, all at once.
StampedeProtector is designed counteract that. If a value can't be found in cache, a placeholder will be stored to indicate it was requested but didn't exist. Every follow-up request for a short period of time will find that indication and know another process is already generating that result, so those will just wait until it becomes available, instead of crippling the servers.
Just wrap the StampedeProtector layer around your adapter (or other features):
// create stampede protector layer over our real cache
$cache = new \MatthiasMullie\Scrapbook\Scale\StampedeProtector($cache);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
暂无开放 Issues,或尚未同步最近议题。