tags:

views:

71

answers:

1

I'm implementing object caching in a PHP application using APC.

The issue is, that sometimes I will select something from the database based on different criteria. For instance, when a user logs into the web site, all I have is his username and password, so I'm going to select from the database based on the username.

In other situations, I'll have the user ID, and want to select based off of that.

Each time I select a user, I'd like to add the object to the cache.

So let's say I put it in there once with the key "User.user_id.123" and once with "User.user_name.JoeSmith".

This does, however, mean that I've just put the same object into my cache twice, right? That doesn't seem so efficient.

Is there a way to put one object into an APC cache with multiple keys for finding it later?

A: 

You could put the users ID into the cache, like User.user_name.JoeSmith=123.

But I doubt the efficiency of storing these values in the cache. It will certainly speed things up at run-time but may lead to several issues during development (because you are basically storing the same values twice, once in the database and once in the cache). Some things that jump into my mind immediately:

  • Are you sure you invalidating the cache if a user value changes?
  • Is the object referencing other objects? Is the cache invalidated once a referenced object changes?
soulmerge