简体   繁体   English

PHP和APC:缓存中有多个键的一个对象?

[英]PHP & APC: One object in cache with multiple keys?

I'm implementing object caching in a PHP application using APC. 我正在使用APC在PHP应用程序中实现对象缓存。

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. 在其他情况下,我将拥有用户ID,并希望基于该ID进行选择。

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". 因此,假设我一次将其放入键“ User.user_id.123”,一次将其放入“ 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? 有没有一种方法可以使用多个密钥将一个对象放入APC缓存中以便以后查找?

You could put the users ID into the cache, like User.user_name.JoeSmith=123 . 您可以将用户ID放入缓存中,例如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. 只需小心删除或更新对象时,使用所有可能的键将其从缓存中清空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM