PHP & APC: One object in cache with multiple keys?
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 开发者_如何学Cwith multiple keys for finding it later?
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?
Update: It does mean putting the same object into the cache twice, and it's what I wound up doing. Just have to be careful to empty an object from the cache with all possible keys when it's deleted or updated.
精彩评论