Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis with PHP - implementing data caching

I have installed redis on my server and implemented object caching for data returned within a PHP based web application. The php model essentially executes a reasonably complex query and returns a detailed array of data. I have tested the caching and found everything to be working as expected. I first check to see if the key exists in redis. If it does, redis returns the data, the model unserializes and returns the previously cached data. If the cache has expired, the model executes the sql query, returns the data and sets the key and serialized value in redis.

So here are my questions.

  1. I'm not sure how to really benchmark this as it is all browser based. What tools are there out there that would allow me to get a reasonable benchmark to compare caching and not caching. I'm thinking of perhaps a php script that calls the api 1000 times via curl.

  2. I implemented this in redis because I once read that caching with redis will work across multiple sessions or ip addresses accessing the site. For example, if the api is accessed 1000 time an hour by multiple ip addresses/users, I am assuming this approach will reduce the load on the mysql server and let redis do the work of returning the cached data instead. Can anyone shed some light on this? Are my assumptions valid?

All comments are welcome!

Thanks!

Dave

like image 463
dnpage Avatar asked Jan 28 '12 03:01

dnpage


People also ask

What is Redis cache in PHP?

June 3, 2022 March 3, 2021 by Jeff Wilson. Redis is an open-source and in-memory data structure store that can be used for caching, real-time analytics, searching, and machine learning.

Is Redis good for caching?

Caching. Redis is a great choice for implementing a highly available in-memory cache to decrease data access latency, increase throughput, and ease the load off your relational or NoSQL database and application.


1 Answers

To benchmark the web site, I would use something like Siege rather than writing a specific PHP script.

Regarding Redis usage, caching things in in-memory stores like memcached or Redis is now very common. Both memcached and Redis are suitable for this purpose, although for pure caching, memcached is arguably easier to setup. 1000 times an hour represents only 3.6 TPS - any data store (including MySQL) will support such traffic without any issue. Now, multiply this traffic by 100 or 1000, and the caching layer (memcached or Redis) will become mandatory to protect your database.

To use Redis for caching, you may want to check the EXPIRE command and have a look at the maxmemory-policy parameter in the configuration file.

like image 61
Didier Spezia Avatar answered Sep 29 '22 02:09

Didier Spezia