dtvnnhh8992 2012-03-20 17:38
浏览 66
已采纳

在zend框架中的内存中脚本缓存后端

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条回答 默认 最新

  • douzi2749 2012-04-03 22:45
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀