Download the PHP package parshikovpavel/array-cache without Composer
On this page you can find all versions of the php package parshikovpavel/array-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download parshikovpavel/array-cache
More information about parshikovpavel/array-cache
Files in parshikovpavel/array-cache
Package array-cache
Short Description Non-persistent PSR-6/PSR-16 compatible cache using an array. Useful for mocking the original cache in tests
License MIT
Homepage https://github.com/parshikovpavel/array-cache
Informations about the package array-cache
The package for caching data in memory using a PHP array.
As cached data is only available in the current request the package should be used for mocking the original cache in tests.
The cache implementation is compatible with PSR-6/PSR-16.
In addition, CounterInterface
(excluded from PSR-16) is included in the library and used for implementation.
The table below shows the correspondence between the library classes and the implemented interfaces.
PSR | Class | Implements |
---|---|---|
PSR-6 | CacheItemPool |
CacheItemPoolInterface |
PSR-6 | CacheItem |
CacheItemInterface |
PSR-16 | Cache |
CacheInterface |
PSR-16 + excluded CounterInterface |
CountingCache |
CacheInterface + CounterInterface |
Installation
The recommended method of installing is via Composer.
Run the following command from the project root:
Usage
A detailed description of the interfaces implemented in the library can be found in PSR-6 and PSR-16.
Here are some common usage patterns of the library for testing your application using PHPUnit.
PSR-6
According to PSR-6, the package provides \ppCache\CacheItemPool
class (implementing the \PSR\Cache\CacheItemPoolInterface
interface) and
\ppCache\CacheItem
class (implementing the \PSR\Cache\CacheItemInterface
interface).
Cache fixture
Use \ppCache\CacheItemPool
instance as a fixture.
Put the creating of the cache fixture into the setUp() method.
Dependency injection
If your application based on the dependency inversion principle and the dependency injection technique, it's very easy to replace a real cache with a mock cache.
Consider the case of constructor injection. Let's assume Client
class provide a parameter in a constructor to inject a cache instance.
Get value from cache
The most common cache use case is a getting value and computing in case of cache miss.
A cache client must first try to get value from cache by $key
.
-
If trying is successful, return
$value
- otherwise compute
$value
by calling the time-consumingcompute()
function and cache the value for a while
PSR-16
According to PSR-16, the package provides \ppCache\Cache
class (implementing the \Psr\SimpleCache\CacheInterface
interface).
Cache fixture
Use \ppCache\Cache
instance as a fixture.
Put the creating of the cache fixture into the setUp() method.
Dependency injection
Similarly, inject a cache instance into the client constructor:
Get value from cache
The algorithm is the same as the one for \ppCache\CacheItemPool
but the implementation is a bit simpler.
PSR-16 + CounterInterface
The package provides the \ppCache\CountingCache
class which implements both the \Psr\SimpleCache\CacheInterface
(introduced PSR-16) and the CounterInterface
(excluded from PSR-16).
CounterInterface
definition is taken from PGP-FIG repository
and is in this package.
The \ppCache\CountingCache
implementation decorates a \ppCache\Cache
instance and supplements its implementation with
atomic increment-decrement methods.
Cache fixture
Dependency injection
Value increment and decrement
Unit testing
There are unit tests in the ./tests
directory. You can run all tests with the following command:
All versions of array-cache with dependencies
psr/cache Version ^1.0
psr/simple-cache Version ^1.0
parshikovpavel/psr-cache-adapter Version *