Caching strategies in MVC Framework?

£可爱£侵袭症+ 提交于 2019-12-03 17:31:46

The opcode cache is there to offset the cost of interpreting your PHP code for each request. It's related to server infrastructure. Kinda like load balancing. Leave it to your admin(s). Or someone who has used *NIX distro, that is not ubuntu.

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

  • periphery:

    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.

    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.

  • response

    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.

    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.

    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.

  • model layer

    This is the tricky bit.

    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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!