therealsix
therealsix

Reputation: 654

Duplicate memcached objects

I am using Memcached to store some PHP objects- lets say my object is a can of soda. Right now my memcached key for Pepsi is 4. So when I get 'soda_4' I get the Pepsi object.

The problem is that sometimes I know I need Pepsi, but I don't have the ID for it. So I need to make a DB call to get the ID before I can get the memcached object. This seems inefficient.

What would be perfect is if I could reference the same object by 2 keys (every time I save the object I have both the ID and Name).

At this point I'm considering saving 2 copies each time.

Any suggestions?

Upvotes: 1

Views: 475

Answers (1)

ajreal
ajreal

Reputation: 47331

Method A:-

  1. using two caches
  2. key A = "name" => "id" (pepsi => 4)
  3. key B = "id (details of pepsi)

So, you can have reference to key B when you have object name.
The drawback is you have to make two calls.

Method B

You have already mentioned, create two cache using name and id as key.
The drawback is you are duplicating the data.
So, the capacity of memcache will reduce 50%.

I would prefer your suggestion, however

Upvotes: 2

Related Questions