andrew
andrew

Reputation: 5176

Smarty Caching vs roll your own?

As far as I understand it, when you turn on caching in smarty, smarty caches compiled templates. These compiled templates can then used to speed up rendering of the page. Wouldn't it be a good idea to run you own level of caching on top of your smarty application that goes like this.

if(a cache for this page exists){
    -Don't run my application, don't include my files don't instantiate my classes.
    -Send the cached version of this page to the user
    -end the script here
}else{//if the cache for this page does not exist or is not current
  - run my application as usual
  -save all the output to a file for next time
}

The whenever somthing happens on my site that would update the content of the this page, eg the admin makes changes to the content of the site delete the cache file. I feel like I must be missing something here. This method would allow me to store an all html version of every page and send that when it is valid. It seems like this would drastically improve the speed of my site.

Edit: Ok so I have discovered that smarty does infact store a html version of my site. How do I prevent my application from running if the rest of my application from running if the cache is current. Do I just include and instantiate smarty first and do something like

if($smarty->usingcache())[
exit;
}

Upvotes: 0

Views: 497

Answers (2)

SingleNegationElimination
SingleNegationElimination

Reputation: 156238

How can you know what the page will look like until you do some request specific processing. Until you've checked your client's authentication status, performed some database queries ( or fetched cached results) to fetch recent information, you can't know if the most recently rendered page is the same as what would be rendered this time. Smarty solves this with this strategy:

  • Your app does all of it's domain/business logic in response to the request.
  • Your app populates the smarty template instance with template variables
  • Smarty generates a hash of the template and the template variables
    • If the hash is not in the cache, Smarty renders the template and caches it
    • If the hash is in the cache, Smarty returns the cached template instead of rendering
    • If the cache is full, smarty evicts an old, cached page to make room.

By default, Smarty uses the filesystem for the Cache, but it the caching strategy is compatible with any key-value store. In fact, plugins exist for other stores, such as this one for memcached.

Upvotes: 1

Jon
Jon

Reputation: 437554

If your site were static, this would work. But in that case, you wouldn't need Smarty...

Suppose you update a record in your database. Then all pages in your site which contain output directly or indirectly affected by the update would have to be invalidated. How would you know which pages to invalidate?

Upvotes: 1

Related Questions