Q:

How can you update a fragment cache while allowing reads from the cache during the update?

Is there a way to refresh a fragment cache in a way that allows reads from the cache while the update is taking place?
I'm caching a part of an html.erb view in Rails with the cache do .. end block in erb.
I'm expiring the same cache in the controller, with a call to expire_fragment(:controller => 'controllername')
I'm using memcached as the fragment cache store.
I could be wrong, but it looks like the default behavior is that the moment you call expire_fragment the fragment is deleted from the cached, so another request a split second after that for the same fragment will miss the cache.
What I would really like is for reads from the cache to keep taking place right up until the new fragment is computed and saved in the cached, at which point all subsequent requests get that new cached version.
This particular fragment is expensive to calculate. It take about 7 seconds.

A:

I would assume memcached has some mechanism for handling read and write conflicts. 
This issue will only matter on a very high traffic site. 
Edit:
Found a question "Is_memcached_atomic?" in the memcached FAQ:

All individual commands sent to
  memcached are absolutely atomic. If
  you send a set and a get in parallel,
  against the same object, they will not
  clober each other. They will be
  serialized and one will be executed
  before the other. Even in threaded
  mode, all commands are atomic. If they
  are not, it's a bug :)

I don't think the issue you describe will be much of a problem. Without going into the Rails internals, it is still possible in a multiple-vm Rails environment that there may be a cache miss, but the worst case scenario is simply that the fragment is generated by requests that happen to have the timing exactly right. Unless you fragment is hugely expensive (seconds rather than milliseconds) and your traffic and infrastructure is massive (multiple rails instances, hundreds of requests a second), I doubt it will be an issue. 

