Get expiration time of a memcache item in php?

后端 未结 3 666
花落未央
花落未央 2021-01-19 14:18

I\'m caching tweets on my site (with 30 min expiration time). When the cache is empty, the first user to find out will repopulate it.

However, at that time the Twitt

相关标签:
3条回答
  • 2021-01-19 14:34

    don't store critical data in memcached. it guarantees nothing.

    if you always need to get "latest good" cache - you need to store data at any persistent storage, such as database or flat file.

    in this case if nothing found in cache - you do twitter api request. if it fails - you read data from persistent. and on another http request you will make same iteration one more time.

    or you can put data from persistent into memcache with pretty shor lifetime. few minutes for example (1-5) to let twitter servers time to get healthy. and after it expired - repeat the request.

    0 讨论(0)
  • 2021-01-19 14:46

    In that case, isn't this the better logic?

    • If the cache is older than 30 minutes, attempt to pull from Twitter
    • If new data was successfully retrieved, overwrite the cache
    • Cache data for an indefinite amount of time (or much longer than you intend to cache anyway)
    • Note the last time the cache was updated (current time) in a separate key
    • Rinse, repeat

    The point being, only replace the data with something new if you have it, don't let the old data be thrown away automatically.

    0 讨论(0)
  • 2021-01-19 14:50

    When you are putting your data into memcache - you are setting also how long the cache is valid. So theoretically you could also put the time when cache was created and/or when cache will expire. Later after fetching from cache you can always validate how much time left till cache will expire and decide what you want to do.

    But letting cache to be repopulated on user visit can be still risky at some point - lets say if you would like to repopulate cache when it reaches ~5 min before expiration time - and suddenly there would be no visitors coming in last 6 minutes before cache expires - then cache will still expire and no one will cause it to be repopulated. If you want to be always sure that cache entry exists - you need to do checks periodically - for example - making a cronjob which does cache checks and fill-ups.

    0 讨论(0)
提交回复
热议问题