Zend如何使用缓存组件
假设您有这样的场景:一个简单的博客主页,加载静态内容和动态内容。 静态内容由很少变化的图像组成。我还有数据库驱动的动态内容。动态内容包括您的所有博客文章(文本和图像)和相关用户评论。动态内容从每小时到每小时定期变化每天。
您将如何进行缓存?特别是假设用户正在留下评论或管理员正在添加/编辑帖子,您会想要手动触发缓存清除以获得此博客主页的更新版本吗?
感谢您的耐心等待。
卢卡
再次感谢
Let's say you have this scenario:a simple blog home-page that loads both static content as well as dynamic content.
The static content is composed of images that rarely changes.I also have database-driven,dynamic content.The dynamic content consists in all of your blog posts (text and image) and related users comments.The dynamic content changes periodically from every hour to every day.
How would you go with caching?And in particular supposing a user is leaving a comment or the admin is adding/editing a post to you would want to manually trigger the cache clearing to have the update version of this blog home-page?
thanks for your patience.
Luca
thanks again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,一个链接: http://framework.zend.com/manual/1.11/en/zend .cache.html
基本上,您需要做的是设置一个缓存机制,然后当您想要从缓存中检索某些内容时手动调用它。
在引导程序中,我可能有这样的代码:
然后,您可以使用 load() 和 save() 函数来操作缓存。我的控制器的一个示例:
在这里,我检查键“main_menu”是否被缓存。如果缓存未命中,则生成并缓存主菜单。
如果我编辑主菜单,我也想重新生成缓存。我简单地称之为:
这很简单,只需阅读文档即可。写得很好。
First, a link: http://framework.zend.com/manual/1.11/en/zend.cache.html
Basically, what you need to do is set up a cache mechanism and then manually call it when you want to retrieve something from the cache.
In the bootstrap, I might have this code:
Then, you may use the load() and save() functions to manipulate the cache. An example from my controller:
Here, I check whether the key "main_menu" is cached. If a cache miss is scored, the main menu is generated and cached.
If I edit the main menu, I'll want to regenerate the cache as well. I simply call this:
It's pretty simple, just read the documentation. It's well written.
Zend 缓存提供了一种非常简单的方法来在缓存中存储数据并提高速度。 Zend 使用前端和后端进行缓存。前端对于访问或操作缓存很有用。后端可用于将数据存储在 File、memcache、Sqlite 等中。
首先,通过在 bootstrap 文件中创建 on 函数来初始化 bootstrap 文件中的前端和后端。
然后使用zend缓存工厂来定义缓存对象。参数 core 定义通用类型的 zend 缓存核心装置 File 参数是定义缓存存储装置,用于存储缓存记录,然后是前端和后端。
现在使用 zend 注册表注册该缓存数组,以便您可以在任何控制器、模型等中使用它们。
在您想要使用数据缓存的任何控制器或任何模型中定义以下代码。
首先在上面的代码中我们得到了缓存数组。现在,如果未设置结果一,则缓存完成意味着该文件是在您在后端数组中定义的路径处生成的
。下次页面加载时,将从缓存数据存储的文件中检索数据。
您可以根据定义的路径检查文件。
该文件中的数据为 json 格式。
Zend cache provide a very simple way to store data in cache and to increase the speed. Zend uses frontend and back end to caching. Front end is useful to access or operate the cache. Back end is useful to store data in File , memcache, Sqlite etc.
First of all initialize the fronted and backed in bootstrap file by creating on function in bootstrap file.
Then use the zend cache factory to define the cache object. The parameter core define the zend cache core means of generic type File parameter is to define the cache storage means where to store the records of cache then second and forth is for frontend and backend.
Now register that cache array using zend registry so that you can use that are in any controller , model etc.
Define Below code in any controller or any model where you want to use caching of data.
First of all in above code we get the cache array. Now if result one is not set then caching done means the file is generated at the path that you define in back-end array
For the Next time page load that data is retrieve from the file where the caching data store.
You can check the file as per defined path.
In that file data is in json format.
所以@mingos展示了缓存的基本用法。他谈到了通用缓存,这很好。然而,ZF 有几种不同的缓存机制可供您用于不同的用途。您无需限制自己只使用一种类型的缓存。您可以混合使用它们。例如,用于缓存静态内容 Zend_Cache_Frontend_Page 值得考虑,因为它会生成静态页面的完整 html 文件。如果您有很多配置文件,例如长的routes.ini 或其他文件,您可以使用
Zend_Cache_Frontend_File
缓存它们。这样您就可以节省为每个请求解析 ini 文件的时间。视图的重要部分可以使用Zend_Cache_Frontend_Output
等进行缓存。缓存什么以及何时更新缓存是一个棘手的问题。这完全取决于您的内容变化的速度和频率。例如,如果每秒有 100 条新评论,则每秒清理评论缓存 100 次(即针对每条新评论)是没有意义的。最好将每个帖子的评论与其他帖子的评论分开缓存。然后,您将清理/刷新仅与此帖子关联的缓存。
So the basic usage of cache is shown by @mingos. He talks about generic cache, which is good. However, ZF has few different cache mechanisms that you can use for different things. You don't need to limit yourself for one type of cache. You can use a mixture of them. For example, for caching your static content Zend_Cache_Frontend_Page would be worth considering as it would generate a full html file of your static pages. If you have lots of config files, e.g. long routes.ini or whatever, you can cache them using
Zend_Cache_Frontend_File
. With this you would save time parsing the ini files for every request. Significant parts of your views could be cached usingZend_Cache_Frontend_Output
, etc.What to cache and when to update a cache is a tricky question. It all depends on how fast and how often your content is changing. For example, if you have like 100 new comments per second, there is no point in cleaning your comment cache 100 times per second (i.e. for each new comment). It would be better maybe to have comments for each post cached separately from the comments of other posts. Then you would clean/refresh a cache associated with only this post.
简单的缓存是在定义的时间段之后超时的缓存。这使得缓存层简单且易于实现。 Zend 手册提供了有关缓存基础知识的更多信息。
然而,实时信息和缓存信息是两个世界。如果您需要实时,请不要缓存。
如果缓存层过于复杂,则可能会毁掉整个应用程序。
A simple cache is one that times out after the defined period of time. This keeps the caching layer simple and easy to implement. The Zend Manual has more information on caching basics.
However real-time information and cached information are two worlds. If you need real-time, don't cache.
If you make the caching layer too complex, you can destroy your whole application.