kingmaple
kingmaple

Reputation: 4310

Cache design and Memcache(d) records with timeouts?

I have a very extensive caching system in place for each and every API call. Unique fingerprint is created from every command and request parameters and a specific timeout.

When a request is made and it does not assign acceptable cache timestamp, then the request is made without cache being returned, so the program goes through everything by itself. Result of this is stored in cache with a new timestamp.

If a request is made and request defines that it is willing to accept 5 minute cache - and the system finds such - then system returns result from cache.

This means that each cache record for me includes a key (unique fingerprint), result and timestamp for when it was made.

Currently cache is stored in filesystem, timestamp is the file modification time, which causes i/o requests that are a killer on higher loads.

Having read multiple articles, I realized that Memcache and Memcached are recommended for reducing these calls.

But Memcache and Memcached only store fingerprint and the value. There is no timestamp, which technically means that I would lose on demand cache timestamp acceptance and flexibility. I would technically have to start storing two records per cache:

Fingerprint-data and Data Fingerprint-time and Timestamp

..which seems dirty. Are there any alternatives?

Upvotes: 0

Views: 383

Answers (1)

Carsten
Carsten

Reputation: 18446

If you know at the time of creation how long your cached objets should last inside the cache, then Memcached has the functionality you need. The Memcache::set function has a parameter called $expire, where you can set the lifetime of the cached object in seconds.

If you only know the lifetime when you retrieve the object from cache, this will not work. I agree that using two keys per cached entity is not feasible, because the cache could lose one of the two while keeping the other. A (still "dirty", but better) solution could be to store a timestamp with each object you put in the cache. You could do this by not caching the objects directly, but rather an array containing the timestamp and the object.

Upvotes: 1

Related Questions