PHP 与APC:缓存中的一个对象具有多个键?
我正在使用 APC 在 PHP 应用程序中实现对象缓存。
问题是,有时我会根据不同的标准从数据库中选择一些内容。例如,当用户登录网站时,我所拥有的只是他的用户名和密码,因此我将根据用户名从数据库中进行选择。
在其他情况下,我将拥有用户 ID,并希望根据该 ID 进行选择。
每次我选择一个用户时,我都会将该对象添加到缓存中。
假设我用“User.user_id.123”键将其放入其中一次,然后用“User.user_name.JoeSmith”将其放入其中一次。
然而,这确实意味着我刚刚将同一个对象放入缓存两次,对吧?这看起来效率不太高。
有没有一种方法可以将一个对象放入具有多个键的 APC 缓存中以便以后查找它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将用户 ID 放入缓存中,例如
User.user_name.JoeSmith=123
。但我怀疑将这些值存储在缓存中的效率。它肯定会在运行时加快速度,但可能会在开发过程中导致几个问题(因为您基本上将相同的值存储两次,一次在数据库中,一次在缓存中)。我立即想到了一些事情:
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:
更新:这确实意味着将同一个对象放入缓存两次,这就是我最终要做的。只需要小心,在删除或更新对象时,使用所有可能的键从缓存中清空对象。
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.