3

BACKSTORY

I maintain a spectrum of (web-)applications that all use a large homegrown PHP library. Some of these applications are traditional desktop applications that are used by employees, but others (which are also more relevant to this question), are PHP websites for which performance is becoming a more important issue as the popularity continues to grow.

CURRENT PHP CACHING METHODS

To speed up one of our websites (it's a shop, think of it as thinkgeek.com), i employ memcached to cache certain segments of the website that don't require constantly being dynamicly build (such as the product listing for a certain category).

We also use a pretty much factory-default installation of APC as an OPCode cache.

Both of these methods bring significant improvements to the website's performance, but i'm very much looking to go further down the road of optimisation.

Function Volatility in PHP

Coming from a Database background myself, i'm very fond of how PostgreSQL for example, uses function volatility to get massive performance gains while maintaining reliable and accurate results.

My question is, is there any extension to PHP that allows the developer to mark certain functions (or class methods) as IMMUTABLE? (meaning the result of that function is always the same, when given the same input arguments). This caching extension could then cache the result of that function which should result in massive performance gains when using big libraries of code.

A simple example would be a method such as SomeClass::getWebsiteFooter(); which returns some HTML code that's always the same, unless the website has been altered (in which case the cache would be flushed).

Does something like this exist ? I haven't been able to find anything remotely similar on the market. Are there any other methods of performance improvement that might benefit my situation ?

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
Staex
  • 31
  • 1
  • 1
  • The technique name you're looking for is memoize (it's not a typo, it's without an R). You can search for "php memoize" but it's far better to implement your own version of the caching (because that's what it essentially is) since you'll have to somehow invalidate the cache, and as Denis Ermolin said - you can cache it to APC. – N.B. Nov 19 '12 at 13:23
  • @N.B. Thanks! Wasn't aware of the term memoize. You probably have a point about doing it myself, however since it's a fairly large framework it would clutter the codebase quite a bit to have to wrap everything with a few lines of memcache lookup/store code (even if it's only 3 lines every time). The main advantage in being able to mark methods as immutable (as in: `public immutable function foo();`), i think, is that all the magic can be handled automaticly by the extension, while the codebase doesn't get cluttered. – Staex Nov 19 '12 at 13:32

1 Answers1

1

I would say you have look at php application as a web application and implement several levels of caching.

IMMUTABLE methods - I don't think is good approach. Usage of caching on db level, application level (memcached) is good start.

Then I would suggest caching on view level Smarty caching and caching proxy like Squid or Varnish

Zdenek Machek
  • 1,758
  • 1
  • 20
  • 30
  • Ok, thanks I've changed it, editor wasn't doing exactly what I wanted. – Zdenek Machek Nov 19 '12 at 13:11
  • I've already implemented caching on the data- and viewlayers with memcached (and additionally an APC opcode-cache). But as the application is using a large library of code that is shared between multiple applications, i'm trying to improve that one. Memcached is perfectly capable of this ofcourse, however i would have to introduce a bit of memcached code into a hundred different methods in order to apply the effect across the board. If it was possible to indicate the volatility of a method instead then the code itself wouldn't need modification and the backend (PHP ext) can do all the work. – Staex Nov 19 '12 at 13:54