In-Memory script cache backend in zend framework

2019-08-06 04:31发布

问题:

I'm looking for solution for a in memory script cache, using Zend_Cache and zend framework.

We have a few cases in our application, that in order to display a table with, let's say, 100 elements, the logic is as the following:

  • fetch 100 items from the content table (a single query)
  • for every item, get the creator user and user who approved the content from the db / memcache backend (this is usually done from a for loop)

In most cases, the user who approved the content is a 2 or 3 admin users, however the script calls the database / memecache multiple times. It gets worse, as the user model has to query 3 different tables to know which role the user belong to, and which community he also belongs to.

So all in all, to display a table of 100 items, we query the database (or memcache server) 600 times :-)

I was thinking about 2 solutions here:

  • Not to query the users table inside the loop. Loop over the 100 items, get all the user IDs and get all the users in a single query. I'm not that sure it would be a good solution, as it mean I'll have to write sql joins causing the model functions to return a non Zend_Db_Table_Row, which we use heavily.

  • another solution is to save the user object into a in memory cache array on first load, and next time the loop tries to read the user id, it will first look up in this "special" in memory cache. I was thinking about using Zend_Registry as the cache backend, similar to the solution here: http://sameerparwani.com/posts/using-zend_registry-as-a-zend_cache-backend

We do use memcache server, but the script just queries the memcache server few hundred times instead of query the mysql database. It is faster to query the memcache server, however, as the script already loads this data, i want to use the script "memory" and not query an external server.

回答1:

Take a look at FaZend, there is an implementation of it. On taking a look into the code it should work a little bit different (e.g no TTL, test() returns a boolean) but it could be enough for you. Not tested !

Edit:

Since Zend Framework 2 there is an implementation for it: Zend\Cache\Storage\Adapter\Memory