2014-11-27 5 views
0

AppFabric 캐시를 4 시간마다 8000 개의 개체로 업데이트하는 서비스가 있습니다. 그것은 테이블을 읽고, 모든 레코드로부터 오브젝트를 생성하여 캐시에 저장합니다.AppFabric에서 잠금을 사용하는 올바른 방법은 무엇입니까

소비자가 여전히 캐시에서 개체를 요청할 수있을 때 캐시가 업데이트되는 동안. 이 이것에 대해 가능한 많은 정보는 없습니다, 그래서 나는 다음과 같은 코드가 다른 스레드가 캐시를 조회 할 수 있습니다 동안 appfabric에 저장하는 캐시 개체를 잠그고하는 올바른 방법 인 경우 알고 싶어

// caching part 
private static void CacheDealers(Dictionary<Key<string>, Dealer> dealers) 
{ 
    DataCache cache = Cache.Instance.DealerCache; 

    foreach (Key<string> key in dealers.Keys) { 
    CacheObject(cache, key.ToString(), dealers[key], mDealerRegionName); 
    } 
} 

    private static void CacheObject(DataCache cache, string key, object obj, string region) 
    { 
     DataCacheLockHandle lockHandle; 

     if (cache.GetAndLock(key, TimeSpan.FromSeconds(10), out lockHandle) == null) 
     { 
     cache.Put(key, obj, TimeSpan.MaxValue, region); 
     } 
     else 
     { 
     cache.PutAndUnlock(key, obj, lockHandle, TimeSpan.MaxValue, region); 
     } 
    } 

    // retrieve part 
    public Dealer GetDealer(string lab, string number) { 
     Dealer dealer = (Dealer)Cache.Instance.DealerCache.Get(new Key<string>(lab, number).ToString(), mDealerRegionName); 
     return dealer; 
    } 

답변

1

잠긴 항목은 아직 할 수있다 캐시에서 읽으면 (잠긴 상태에서 업데이트 된 객체가 아닌 원래 객체가 반환되지만) 캐시가 업데이트되는 동안 GetDealer 메소드가 계속 작동해야합니다. MSDN에서 :

정기 Get 메서드 호출이 차단되지 않고 항상 캐시 된 개체의 최신 버전에 액세스