Php template caching design

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-03-04T12:35:59Z Indexed on 2010/04/21 22:03 UTC
Read the original article Hit count: 295

Filed under:
|

Hello to all,

I want to include caching in my app design. Caching templates for starters.

The design I have used so far is very modular. I have created an ORM implementation for all

my tables and each table is represented by the corresponding class.

All the requests are handled by one controller which routes them to the appropriate webmethod functions.

I am using a template class for handling UI parts.

What I have in mind for caching includes the implementation of a separate Cache class for handling caching with the

flexibility to either store in files, apc or memcache. Right now I am testing with file caching.

Some thoughts

  1. Should I include the logic of checking for cached versions in the Template class or

in the webmethods which handle the incoming requests and which eventually call the Template class.

In the first case, things are pretty simple as I will not have to change anything more than pass the template class

an extra argument (whether to load from cache or not).

In the second case however, I am thinking of checking for a cached version immediately in the webmethod and if found return it. This will save all the processing done until the logic reaches the template (first case senario).

Both senarios however, rely on an accurate mechanism of invalidating caches, which brings as to

  1. Invalidating caches

    As I see it (and you can add your input freely) a template cached file, becomes invalidate if:

    a. the expiration set, is reached.

    b. the template file itself is updated (ie by the developer when adding a new line)

    c. the webmethod that handles the request changes (ie the developer adds/deletes something in the code)

    d. content coming from the db and ending in the template file is modified

    I am thinking of storing a json encoded array inside the cached file. The first value will be the expiration timestamp of the cache. The second value will be the modification time of the php file with the code handling the request (to cope with option c above) The third will be the content itself

    The validation process I am considering, according to the above senarios, is:

    a. If the expiration of the cached file (stored in the array) is reached, delete the cache file

    b. if the cached file's mod time is smaller than the template's skeleton file mod time, delete the cached file

    c. if the mod time of the php file is greated than the one stored in the cache, delete the cached file.

    d. This is tricky. In the ORM implementation I ahve added event handlers (which fire when adding, updating, deleting objects). I could delete the cache file every time an object thatprovides content to the template, is modified.

    The problem is how to keep track which cached files correpond to each schema object.

    Take this example, a user has his shortprofile page and a full profile page (2 templates)

    These templates can be cached. Now, every time the user modifies his profile, the event handler would need to know which

    templates or cached files correspond to the User, so that these files can be deleted. I could store them in the db but I am

    looking for a beter approach

© Stack Overflow or respective owner

Related posts about php

Related posts about caching