Welcome Guest, Not a member yet? Register   Sign In
How to use redis instead of file system as a mysql cache
#1

(This post was last modified: 06-13-2016, 06:28 AM by dangyuluo.)

Seems that Codeigniter use Redis separately as a key-value cache. But in many situations we need more that that.

I know that Codeigniter offers database cache, which is based on file storage. Yet compared with Redis, it has two drawbacks,

  1. Redis runs in memory and can be accessed via socket or TCP/IP. File storage system, well, as its name, is based on hard drive. Though my sever has an SSD hard drive, files on it cannot be read as fast as from Redis. So using Redis as DB cache will beat file storage in terms of speed. 
  2. Redis has an intrinsic support of timeout control of key. Thus we don`t need to manually delete database cache files for some data changing infrequently. It can be updated automatically by Redis hourly, for example. 
Another point, which I think is not the optimal scheme of database cache in 3.0.6. Codeigniter creates cache directories for each combination of controller and its method. Like the cache files of /post/view/1and /post/edit/1 is stored in different dir. But these two pages do share the same database query:

SELECT * FROM `post` WHERE `id`=1 LIMIT 1

And this SQL query will be executed twice or even more times in different methods, only to get the same data from database and waste the server's CPU. So I think, the optimal way to cache data from database, is to recognize the specific SQL query sentence, MD5 it, and store the result in to whether file system or Redis, selected by user. Thus different page can share the same cache result and enhance the hit ratio of cache.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB