这是基本模式:
- 检查缓存中的值,如果可用则返回
- 如果该值不在高速缓存中,则实施锁定
- 在锁内,再次检查缓存,您可能已被阻止
- 执行值查找并将其缓存
- 释放锁
在代码中,它看起来像这样:
private static object ThisLock = new object();public string GetFoo(){ // try to pull from cache here lock (ThisLock) { // cache was empty before we got the lock, check again inside the lock // cache is still empty, so retreive the value here // store the value in the cache here } // return the cached value here}


