Whats the best way to manage keys (in memcache ) to prevent stale cached values?

前端 未结 6 1492
忘了有多久
忘了有多久 2021-01-30 15:01

Ive recently implemented memcache on my site which has been under heavy mysql load (mysql was as optimized as I could make it). It solved all my load issues, and site is running

6条回答
  •  再見小時候
    2021-01-30 15:51

    What you could do to make sure that your cache is always up to date without doing lots of changes to your code is work with a "version cache". This does increase the number of memcache requests you will make, but this might be a solution for you.

    Another good thing about this solution is that you can set expiration time to never expire.

    The idea is to basically have a version number stored in memcache for in your case a certain keyword (per keywork, not combination). How to use this?

    When someone submits a new item:

    • for every word in the title, do if(!Memcache:increment("version_" + keyword)) {Memcache:set("version_" + keyword);}

    When someone executes a query:

    • the md5 thing that your are doing is already ok. Additionally you need to add the version of every keyword in your search string to the memcache key.

    This ensures that as soon as a keyword has new results (or less when deleting), the version will be bumped and as such all related memcache queries.

    Cache always up to date and queries can potentially stay longer than 1 hour in the cache.

提交回复
热议问题