I'll most probably be using MemCache for caching some database results. As I haven't ever written and done caching I thought it would be a good idea to ask those of you who have already done it. The system I'm writing may have concurrency running scripts at some point of time. This is what I'm planning on doing:
- I'm writing a banner exchange system.
- The information about banners are stored in the database.
- There are different sites, with different traffic, loading a php script that would generate code for those banners. (so that the banners are displayed on the client's site)
- When a banner is being displayed for the first time - it get's cached with memcache.
- The banner has a cache life time for example 1 hour.
- Every hour the cache is renewed.
The potential problem I see in this task is at step 4 and 6. If we have for example 100 sites with big traffic it may happen that the script has a several instances running simultaneously. How could I guarantee that when the cache expires it'll get regenerated once and the data will be intact?