Re: How does caching work?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/6/9 Paul M Foster <paulf@xxxxxxxxxxxxxxxxx>:
> This question may be too complex for a reasonable answer on this list,
> but I'll ask anyway.
>
> If a page has static content (no PHP variables, etc.), I can
> understand how caching works. The engine just checks to see if it's got
> a copy of the requested page in the cache, and displays it if so, or
> fetches it if not.
>
> But if a page is populated with variables from a database (for example)
> which could change from time to time, how could a caching engine
> possibly cache it? How would it determine whether to re-fetch the page
> or use the cached version?

Assuming you're not talking about client-side caching which Dan has
already covered...

Generally speaking there's two ways to approach server-side caching...
update-on-expiry and update-on-change.

For update-on-expiry you simply tell the caching system how long the
cache should last and when the cache expires that chunk the code will
generate it again from the database.

In update-on-change you take a more pro-active approach and have any
code that changes cached data update/expire any cache items that use
it.

Which approach is best depends on a number of factors including the
type of data being cached, how often it will change and how fresh you
need the site to be. It's also worth nothing that update-on-expiry is
fine as it is for low traffic sites but if you have a high traffic
site you'll need to look at locking the cache item while it's
generated otherwise you could have a large number of requests
simultaneously updating the same cache items.

On the main site I maintain I use update-on-change and just store the
raw data in the cache - most pages are generated from this data
on-the-fly. There are a few pages that are hit a lot that I cache as
an HTML chunk but for the most part it's just data in the cache. My
reasoning behind this is that DB's are painful and expensive to scale
whereas HTTP/PHP servers are simple and cheap.

-Stuart

-- 
http://stut.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux