Download the PHP package phpixie/cache without Composer
On this page you can find all versions of the php package phpixie/cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpixie/cache
More information about phpixie/cache
Files in phpixie/cache
Informations about the package cache
Cache
PHPixie Cache library
The PHPixie Cache component supports both PSR-6 and PSR-16 standards and also adds a few useful features.
Initializing
If you are using the PHPixie framework the component is accessible via your builder and configured in /assets/config/cache.php
.
Like all the other PHPixie components you can use it without the framework, in that case initialize it like so:
Configuration
Similarly to how you configure database connections, you can define multiple storage configurations using the available drivers: void, memory, phpfile, memcached and redis:
Usage
As mentioned PHPixie Cache supports both PSR-6 and the new simplified PSR-16. You will mostly use instances of
the PHPixie\Cache\Pool
interface:
Some examples:
There's no point to rewrite here all the usage examples for these PSRs which already have great documentation.
The additional methods like delete
, clear
, etc. are very intuitive easy to find in PSR docs and by using IDE hints.
Let's instead focus on some unique features.
Prefixed pools
When multiple parts of the application use the same cash storage a common practice is to prefix keys. How often have you seen code like this:
If these entities are cached in different parts of the application you have to make sure you are always using the same prefix or even abstract this logic into a separate service. PHPixie Cache solves this problem using a prefixed pools that proxy requests to the storage automatically prefixing the keys:
As you probably guessed such pools also implement the same PHPixie\Cache\Pool
interface as the storages and can themselves
be prefixed thus creating a hierarchy. They can be used to define different pools for different entities and also
make it easy to later switch some of them to actual separate storages if needed. For example you can start out with
one cache storage and multiple prefixed pools and expand easily when your application grows.
Simple usage without storages
The PHPixie\Cache
class itself also implements the Pool
interface and will proxy requests to the default cache storage.
This makes the component easier to use in application with only one cache storage:
Not hashing the keys
Most filesystem caches hash the key to generate the appropriate file name, this is done to protect you from using keys that contain characters not supported by the filesystem. In reality most people use alphanumeric cache keys anyway and hashing these provides no real value but makes cache files harder to inspect and makes a slight impact on the application performance. PHPixie Cache uses raw keys for filenames, although hashing can easily be introduced if required.
Optimized file cache
Most cache libraries serialize the value together with the expiry timestamp to store it in a file cache. This approach has two drawbacks: everytime you read the value you have to deserialize it which impacts performance when used frequently, also to just check if the cache has not yet expired you have to deserialize the entire file, which is wasteful if the actual value is not used afterwards. So how does PHPixie Cache solve these? Let's look at an example of a cached file:
The first line contains a comment with the expiry timestamp, which can be checked simply by reading this one line
ignoring the rest of the file. Also the cache is stored as PHP code which is retrieved using the include
operator.
This means that your opcache will cache it and not actually read the file from disk every time it is requested. This
approach is especially great for usecases with singular writes and frequent reads like configuration cache etc. and
can actually outperform other storages.
Contributing and adding drivers
Since PHPixie's social component got so much help from the community with adding new providers I decided to add a small guide on how to contribute and add your own storage driver to PHPixie Cache.
- Add a class
PHPixie\Cache\Drivers\Type\YourDriver
extending fromPHPixie\Cache\Drivers\Driver
. - Register it in
PHPixie\Cache\Builder::$driverMap
. - Create a test class
PHPixie\Tests\Cache\Driver\YourDriverTest
extending fromPHPixie\Tests\Cache\DriverTest
- Edit
.travis.yml
andcomposer.json
to add any necessary dependencies and packages so that your tests runs on Travis CI. - Send in the Pull Request ;)
And of course our chatroom will be happy to help with any problems you encounter.