Cache
Cache is a class found under Phalcon\Cache directory. It helps in accessing frequently used data at much faster rate. Phalcon\Cache is written in C Programming language and reduce overhead.
When to implement Cache?
- When we are frequently using complex calculations that returns the same result.
- When we are using many helpers and the output generated is always the same.
- When we are accessing database data constantly and its data rarely changes.
Caching process is divided into 2 parts:
1) Frontend: Frontend checks whether a key has expired or not. Also perform additional transformations to the data before storing and after retrieving them from the backend.
2) Backend: Backend part is responsible for communicating, reading or writing the data required by the frontend.
Implementation
Below code provide basic caching process for 2 days cache by implementing frontend and backend adapters.
Frontend Adapters
Adapter |
Description |
Phalcon\Cache\Frontend\Output |
Read input data from standard PHP output. |
Phalcon\Cache\Frontend\Data |
It is used to cache any kind of PHP data (big arrays, objects, text, etc). Data is serialized before stored in the backend. |
Phalcon\Cache\Frontend\Base64 |
It's used to cache binary data. The data is serialized using base64_encode before be stored in the backend. |
Phalcon\Cache\Frontend\Json |
Data is encoded in JSON before be stored in the backend. Decoded after be retrieved. This frontend is useful to share data with other languages or frameworks. |
Phalcon\Cache\Frontend\Igbinary |
It is used to cache any kind of PHP data (big arrays, objects, text, etc). Data is serialized using Igbinary before be stored in the backend. |
Phalcon\Cache\Backend\Xcache |
Stores data in XCache. |
Phalcon\Cache\Backend\None |
It is used to cache any kind of PHP data without serializing them. |
Backend Adapters
Adapter |
Description |
Info |
Required Extension |
Phalcon\Cache\Backend\Apc |
Stores data to the Alternative PHP Cache (APC). |
APC |
APC |
Phalcon\Cache\Backend\Apcu |
Stores data to the APCu (APC without opcode caching) |
APCu |
APCu |
Phalcon\Cache\Backend\File |
Stores data to local plain files |
|
|
Phalcon\Cache\Backend\Mongo |
Stores data to Mongo Database. |
MongoDB |
MongoDB |
Phalcon\Cache\Backend\Redis |
Stores data in Redis |
Redis |
Redis |
Phalcon\Cache\Backend\Xcache |
Stores data in XCache. |
XCache |
XCache |
Phalcon\Cache\Backend\Memcache |
Stores data to a memcached server. |
Memcache |
Memcache |
|