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?

  1. When we are frequently using complex calculations that returns the same result.
  2. When we are using many helpers and the output generated is always the same.
  3. 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

AdapterDescription
Phalcon\Cache\Frontend\OutputRead input data from standard PHP output.
Phalcon\Cache\Frontend\DataIt 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\Base64It's used to cache binary data. The data is serialized using base64_encode before be stored in the backend.
Phalcon\Cache\Frontend\JsonData 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\IgbinaryIt 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\XcacheStores data in XCache.
Phalcon\Cache\Backend\NoneIt is used to cache any kind of PHP data without serializing them.

Backend Adapters

AdapterDescriptionInfoRequired Extension
Phalcon\Cache\Backend\ApcStores data to the Alternative PHP Cache (APC).APCAPC
Phalcon\Cache\Backend\ApcuStores data to the APCu (APC without opcode caching)APCuAPCu
Phalcon\Cache\Backend\FileStores data to local plain files
Phalcon\Cache\Backend\MongoStores data to Mongo Database.MongoDBMongoDB
Phalcon\Cache\Backend\RedisStores data in RedisRedisRedis
Phalcon\Cache\Backend\XcacheStores data in XCache.XCacheXCache
Phalcon\Cache\Backend\MemcacheStores data to a memcached server.MemcacheMemcache





Latest Courses