Download the PHP package talesoft/tale-cache-core without Composer
On this page you can find all versions of the php package talesoft/tale-cache-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download talesoft/tale-cache-core
More information about talesoft/tale-cache-core
Files in talesoft/tale-cache-core
Package tale-cache-core
Short Description A PSR-6 (Cache) and PSR-16 (SimpleCache) starter
License MIT
Homepage http://docs.talesoft.codes/php/tale/cache-null-cache
Informations about the package tale-cache-core
Tale Cache Core
What is Tale Cache Core?
Tale Cache Core is a basic extension of the PSR-6 and PSR-16 caching standards combined into a single library.
It acts as a base for libraries to be compatible to PSR-6 and PSR-16 caches without relying on heavy dependencies and also acts as a base for the Tale Cache library.
Furthermore, it tries to fix a single problem with the standard PSR cache specifications.
Installation
Usage
PSR-6 to PSR-16 adapter
Easily use PSR-6 cache pools in your applications or libraries
preferring PSR-16 by using Tale\Cache\PoolCache
Null Cache and Runtime Cache for library authors
Sometimes library authors want to make their libraries caching compatible, but don't want to implement a whole caching implementation with it. While interfaces work really well for that, they can only be represented as optional dependencies, when sometimes what you really want is a required dependency on a cache or test against a mocked implementation. A real implementation also avoids needing to make your properties nullable or do null-checks on optional dependencies all the time, so you avoid a lot of defensive programming with null checks
Tale Cache Core provides two lightweight, simple implementations of a PSR-6 cache pool that can be used as default values and work like normal caches, just that they don't really do anything
Imagine a service that looks like this:
If you might want to test against this or instantiate it somewhere,
you can either use the NullPool
This will basically just work like a completely disabled cache.
If you want to have some runtime caching so that cache items
are not generated over and over again, you can also use the
RuntimePool
, which caches values as long as the process is there
If you still want optional dependencies, but you want to avoid defensive null-checks all over your library code, you can just default the value with a null-coalesce operator
Easy custom Cache Pool implementation
Tale Cache Core takes some of the work you require when wanting to write PSR-6 compatible cache pools. As an example we will implement an own file cache with Tale Cache Core:
now you have a fully valid PSR-6 cache working with files
Interoperable Cache Items
Tale Cache Core extends the normal PSR-6/16 interfaces and adds a single method that provides the ability to have a single CacheItem implementation for all possible CachePools
New interfaces to code against (All are PSR-6/16 compatible):
Psr\SimpleCache\CacheInterface => Tale\CacheInterface
| No new methods
Psr\Cache\CacheItemPoolInterface => Tale\Cache\PoolInterface
| getItem($key): Tale\Cache\ItemInterface (type narrowing)
Psr\Cache\CacheItemInterface => Tale\Cache\ItemInterface
| getExpirationTime(): ?DateTimeInterface
As you can see, the Tale\Cache\ItemInterface
provides a single new
method to the interfaces, which allows us to retrieve the specified
expiration time of a cache item. This allows the Tale\Cache\ItemInterface
to be absolutely interoperable between different Item Pool implementations.
You can move items from one pool to another:
This is possible because the Cache Item can finally be a normal DTO and doesn't need its pool to set its expiration time, the cached value is stored inside the item along with its key and TTL, so it can always be moved or copied to other item pools.