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

前端 未结 6 1491
忘了有多久
忘了有多久 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:35

    Couple simple things you can do:

    First, if you really want to use the query string as a cache key, make it more deterministic and predictable. I'd do this by sorting the query string, e.g, : ?zed=7&alpha=1 is transformed to ?alpha=1&zed=7. Also strip out variables that aren't relevant to the caching key.

    To handle the problem of the ?page parameter, and items not showing up because the cache hasn't refreshed, I've got a couple ideas:

    Folke's idea of adding a 'version' to the cache key would work well. The same trick is used to easily make links like unvisited.

    Another approach would be to store the number of pages in the cache value, and then, when the database is updated, iterate through the cache keys.

    cache.put("keyword,page=3", array(num_pages=7, value=...))
    
    ...later...
    update_entry()
    num_pages, value = cache.get("keyword,page=3")
    for i in num_pages:
      cache.flush("keyword,page="+i)
    

    Whether this is a good idea or not depends on how many pages there are, and the chance of updates coming in while the loop is running.

    A third idea is to cache the entire result set instead of just that page of results. This may or may not be an option depending up on the size of the result set. When that result set is updated, you just flush the cache for that keyword.

    cache.put("keyword", array(0="bla", 1=foo", ...)
    ...later...
    cache.get("keyword")[page_num]
    

    A fourth idea is to change your caching backend and use something built to handle this situation. I dunno what other cache servers are out there, so you'll have to look around.

    Finally, to supplement all this, you can try and be smarter about the expire time on cache entries. e.g., use the mean time between updates, or the number of queries per second for the keyword, etc.

提交回复
热议问题