MVC框架中的缓存策略? [英] Caching strategies in MVC Framework?

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

问题描述

我写了我自己的小PHP MVC框架,现在我在探索PHP MVC框架中的缓存策略.我正在考虑可以缓存的内容,位置和方式.

I wrote my own little PHP MVC Framework, and now Im exploring caching strategies in PHP MVC Frameworks. Im thinking about what can be cached, where and how.

我拥有的框架是简单的MVC框架.我有前端控制器,可以启动应用程序,注册类自动加载,设置php运行时指令...,最后分析URL并将请求分派给适当的控制器,方法,动作控制器,以及您想如何调用它.从控制器,我可以访问域对象和数据映射器,这些映射器可以将域对象持久存储到某些存储中,大部分时间是关系数据库. 从控制器中,我可以访问域对象和数据映射器.

The framework I have is simple MVC Framework. I have front controller, that boots up application, registers class auto loading, sets up php run time directives... and at the end analyses URL and dispatches request to appropriate controller, method, action controller, how ever you want to call it. From controller, I have access to Domain Objects, and Data Mappers that can persist Domain Objects to some storage, most of the time Relation Database. From Controllers I have access to Domain Objects and Data Mappers.

就缓存而言,这些就是我现在可以做的事情. 使用PHP,我可以使用APC缓存(即操作码缓存),但也可以使用它将变量保存到RAM中.然后,我可以将Memcache和Memcahed用作APC缓存,但是如果需要扩展的话,我可以从其他服务器访问存储的缓存.那两个不是操作码缓存.

So as far as caching goes these are the things I can things I know I can do at the moment. With PHP I can use APC Cache that is opcode cache, but I can also use it to save variables into RAM. Then I can use Memcache and Memcahed that works joust as APC cache but I can access stored cache from different servers If I have to scale. And those two are not opcode caches.

据我所知,我可以做这些事情:

As far as I know I can do these things:

  1. 在控制器中时,我可以将域对象保存到缓存中,因此,如果我已经在缓存中包含该域对象,则不必每次都打开与数据库的连接.

  1. When in controllers, I can save Domain Objects into cache, so I dont have to open connection to database every time, if I already have that Domain Object in cache.

我可以构建我的缓存系统,该系统将在引导时分析URL,然后如果存在具有该页面URL的缓存,则获取该URL的已解释页面,如果不存在,它将处理请求,然后将该页面保存到缓存中并将其与当前网址关联

I can build my cache system, that would analyze URLs at bootstrap, and then get already interpreted page for that URL if cache with that page URL exists, if not it would process request and then save that page into cache and associate it with current URL

因此,如您所见,我真的不知道如何在我的MVC中实现缓存,我应该在哪里缓存东西,如何缓存以及存在什么可能性.

So, as you can see I dont really know how to implement cache in my MVC, and where should I cache things, how, and what possibilities exists.

那么有人可以更好地解释这一点,还是将我重定向到一些我可以了解缓存的好文章?

So can someone explain this better, or redirect me to some good articles where I could learn about cacheing?

谢谢!

推荐答案

操作码缓存在那里可以抵消为每个请求解释PHP代码的成本.它与服务器基础结构有关.有点喜欢负载均衡.留给您的管理员.或使用过* NIX发行版的人,不是ubuntu.

在MVC应用程序中,有3个点可以缓存内容:

In an MVC application there are 3 points where you can cache stuff:

  • 外围设备:

与MVC模式本身并没有真正关系的应用程序部分,但涉及到MVC:路由机制和(如果使用的话)DIC.

Parts of application that are not really related to MVC pattern itself, but are involved in getting to MVC: routing mechanism and (if you use it) DIC.

可以为路由机制缓存的内容有点取决于实现.如果您使用的是从更易读的模式生成的正则表达式,则可以缓存到产生的表达式.而且还可以缓存通常由路由产生的部分参数.两者在大中型网站上都是合理的,而对于小网站来说完全没有意义.

What you can cache for routing mechanisms kinda depends on implementation. If you are using some regexp generation from more readable patterns, then you can cache to produced expressions. And also it is possible to cache part of parameters that would be normally produced by route. Both of those are reasonable in medium/large sites and completely pointless for small ones.

如果您决定使用DIC(我个人认为这是一种反模式,但是所有很酷的孩子都不同意),那么缓存几乎是强制性的,因为正确编写的DIC会利用反射.而且反射很慢.

If you decided to use DIC (personally I think that it is an antipattern, but all the cool kids disagree), then caching is almost mandatory, because correctly written DIC will utilize reflections. And reflections are slow.

响应

有时,应用程序的某些部分需要大量资源才能创建.如果您的MVC解释具有完全实现的View,则可以缓存一些用于生成输出的模板.

Sometimes there are parts of application, that require lot of resources to create. If your MVC interpretation has a fully realized View, then it is possible to cache some of the templates, which are used for generating the output.

例如,使用StackOverflow之类的网站.如果您决定在侧边栏上创建过去24小时内的顶部标签"块,则将无法为每个网页浏览量重新生成该标签.克服此限制的一种方法是仅每小时左右重新计数标签,并将生成的结果存储在HTML片段中.然后可以再次重复使用此片段.如果缓存的片段是XHR使用的JSON,您甚至可以添加客户端到期标头来进一步减少服务器负载.

For example, take site like StackOverflow. If you decided to create "top tags in past 24 hours" block on the sidebar it would be impossible to regenerate that for each page-view. One way of getting around this limitation would be to recount the tags only each hour or so and store the generated result in a HTML fragment. This fragment can then be reused over an over again. If the cached fragment is some JSON used by XHR, you can even add client-side expire header to decrease server load even more.

您可以仅缓存页面片段或整个页面.基本上,这是在具有MVC架构的站点中处理具有静态内容的部分的方法.

You can cache either only fragments of page or the whole thing. This is basically how in a site with MVC-imbued architecture one deals with parts that have static content.

模型层

这很棘手.

首先,您必须了解缓存只是另一种存储形式,这意味着缓存由特殊的映射器处理.

First of all you have to understand that cache is just another form of storage, which means that caching is handled by special mappers.

$user = new User;
$cache = new UserCacheMapper;

$user->setId( 42 );
if ( ! $cache->fetch( $user ) )
{
    $storage = new UserDbMapper( $pdo );
    $storage->fetch( $user );

    $cache->store( $user );
}

// the $user object has been initialized

缓存映射器也是发生缓存失效的地方,这样您就可以在项目的任何阶段添加缓存机制.

The cache mapper is also the place where invalidation of cache would happen and this way you can add the caching mechanism at any stage of project.

但是,我建议停止使用控制器来处理域对象和存储之间的交互.您基本上是在表示层中泄漏域业务逻辑.我建议创建包含应用程序此部分的服务,并让您将控制器与域逻辑隔离.

Though, I would recommend to stop using controllers for handling the interaction between domain objects and storage. You are basically leaking the domain business logic in the presentation layer. I would recommend to create services that contain this part of application and let you isolate controllers from the domain logic.

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

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