One of these days, I'd really like to add some memcache support to HTML Purifier. The large data-structures it deals with really lend themselves well to caching, and although I'm currently getting this sort of functionality using unserialize(file_get_contents()), retrieving objects from memory is bound to be a bit faster. Plus, it gives me a chance to make use of a real performance-winner used on such sites like Slashdot and Wikipedia.
However, because this is a library, memcached support will likely only get turned on when the container application uses it too. As such, it is vital that my usage of memcached coexists peacefully with their usage.
I'm not too worried about keys: HTML Purifier can easily get its own pseudo-namespace so there's very little chance of collision. I am, however, worried about cache invalidation. The data I would cache doesn't have any logical expiration time (it should, barring any changes in the code, last forever), but when the code changes, it is virtually guaranteed that every object will change. In my filesystem based cache system, I use a flush() function to clean out the cache so it can be cold-started. Since the average application uses only one or two configurations, this isn't too bad.
However, as far as I can see, the flush() equivalent on memcache will delete every object in the cache. Which probably would be quite irritating to a developer and possibly dangerous for performance intensive sites. I don't see any alternative though. Is there a way to selectively delete items from memcache (knowing only a common trait of these items and not the actual ones), or is there some other technique to deal with this problem?
Memcache etiquette: flush()
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
The only method I can suggest is to maintain the list of keys used by HTMLPurifier in a distinct item under well-known key. Something like this:I don't see any alternative though. Is there a way to selectively delete items from memcache (knowing only a common trait of these items and not the actual ones),
Code: Select all
public function flush() {
$keys = $this->_memcached->get(self::LIST_ITEM_NAME);
if (is_array($keys)) {
foreach ($keys as $key) {
$this->_memcached->delete($key);
}
}
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US