What is Memcache & How to use in PHP

CodeyMaze
3 min readMar 5, 2022

Memcache is object storing mechanism which is used to store the results of database queries helping websites to serve pages faster. Memcaching process stores data as key-value pair in memory so that it can be accessed later. It will be useful in those applications which relies heavily on database queries. Memcaching is going to improve performance of the application significantly.

Caching with Memcache works like this:

  1. Browser requests page, PHP runs on the server to execute the code
  2. PHP will ask Memcache extension to get the page data
  3. If data is cached, it is sent to PHP
  4. If it is not cached, Memcache sends query to the database, returns it to PHP and store the data

One question may come when Memcache knows when is the time to refresh the data in case of updates in the database. It is known as cache invalidation means knowing when to delete the current data from cache and force to re-query from the database.

Memcache is a caching mechanism, it does not know when to get the updated data. You have to write the code for all conditions. One strategy that we can follow is update the Memcache when database is updated. In this way you will be always having newly refreshed data in your Memcache.

Other way is to store data in Memcache with an expiration time. After the expiration time, delete the Memcached data and re-query the database to get the updated data and then store in Memcache.

To enable the PHP Memcache extension, build PHP using -enable-memcache option. Now you can connect to Memcache server instance.

$cache = new Memcache(); $cache->connect('localhost',11211);

Memcache runs as a daemon and it is called Memcached .

Store data in Memcache

We can store object in Memcache using the $cache object. $cache object is having set method to store the object in cache. It takes key-value pair and time.

Parameters for set method are :

key — key of the item to be stored

value — value of the item to be stored

flag — use MEMCACHED_COMPRESSED to store the item compressed.

time — expiration time of the item

$cache->set('key','value', 0, $time)

Time will be in seconds, if it is set to 0, then item will never expires. Flag value set to 0 means no compression.

Return value of this function will be true or false. True on success & False on failure.

Get data from Memcache

$cache object has get() method which can be used to get value from the given key.

$value = $cache->get($key);

In case of no value it will return null.

Delete data from Memcache

Memcache provides delete() method to delete data from Memcache.

$cache->delete($key);

Why should not use Memcache

  • If you have data more than 1 MB then do not use Memcache, It is not for large data.
  • If you have long keys more than 250 characters then do not use Memcache.
  • If your application is running on shared hosting then do not use Memcache . Anyone can use Telnet to access Memcached server.

Subscribe to our Youtube channel for more videos :

https://www.youtube.com/channel/UCEXWe9R6-ppk4zhNo2JbtmQ

--

--

CodeyMaze

I am here to provide programming related topics, running website also named http://codeymaze.com. Feel free to follow me :)