Zend 框架中的内存中脚本缓存后端 [英] In-Memory script cache backend in zend framework

查看:29
本文介绍了Zend 框架中的内存中脚本缓存后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用 Zend_Cache 和 zend 框架的内存脚本缓存解决方案.

我们的应用程序中有几个案例,为了显示一个包含 100 个元素的表格,逻辑如下:

  • 从内容表中获取 100 个项目(单个查询)
  • 对于每个项目,从 db/memcache 后端获取创建者用户和批准内容的用户(这通常通过 for 循环完成)

在大多数情况下,批准内容的用户是 2 或 3 个管理员用户,但是脚本会多次调用数据库/memecache.情况变得更糟,因为用户模型必须查询 3 个不同的表才能知道用户属于哪个角色,以及他还属于哪个社区.

总而言之,为了显示 100 个项目的表,我们查询数据库(或内存缓存服务器)600 次 :-)

我在这里考虑了 2 个解决方案:

  • 不要查询循环内的用户表.循环遍历 100 个项目,获取所有用户 ID 并在单个查询中获取所有用户.我不确定这是否是一个好的解决方案,因为这意味着我必须编写 sql 连接,导致模型函数返回一个我们大量使用的非 Zend_Db_Table_Row.

  • 另一种解决方案是在第一次加载时将用户对象保存到内存缓存数组中,下次循环尝试读取用户 ID 时,它将首先在内存缓存中的这个特殊"中查找.我正在考虑使用 Zend_Registry 作为缓存后端,类似于这里的解决方案:http://sameerparwani.com/posts/using-zend_registry-as-a-zend_cache-backend

我们确实使用了内存缓存服务器,但脚本只是查询内存缓存服务器几百次,而不是查询 mysql 数据库.查询内存缓存服务器会更快,但是,由于脚本已经加载了这些数据,我想使用脚本内存"而不是查询外部服务器.

解决方案

看一看 FaZend,有它的实现.在查看代码时,它的工作方式应该略有不同(例如,没有 TTL,test() 返回一个布尔值),但这对您来说已经足够了.未测试!

从 Zend Framework 2 开始,就有了它的实现:Zend\Cache\Storage\Adapter\Memory

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.

解决方案

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

这篇关于Zend 框架中的内存中脚本缓存后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆