Programing

asp.net에서 캐시를 잠그는 가장 좋은 방법은 무엇입니까?

crosscheck 2020. 10. 21. 07:43
반응형

asp.net에서 캐시를 잠그는 가장 좋은 방법은 무엇입니까?


장기 실행 프로세스와 같은 특정 상황에서는 해당 리소스에 대한 다른 사용자의 후속 요청이 캐시에 도달하는 대신 장기 프로세스를 다시 실행하지 못하도록 ASP.NET 캐시를 잠그는 것이 중요하다는 것을 알고 있습니다.

ASP.NET에서 캐시 잠금을 구현하는 C #의 가장 좋은 방법은 무엇입니까?


다음은 기본 패턴입니다.

  • 캐시에서 값을 확인하고 사용 가능한 경우 반환
  • 값이 캐시에 없으면 잠금을 구현하십시오.
  • 잠금 장치 내부에서 캐시를 다시 확인하십시오. 차단되었을 수 있습니다.
  • 값 조회를 수행하고 캐시합니다.
  • 잠금 해제

코드에서는 다음과 같습니다.

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

}

완전성을 위해 전체 예제는 다음과 같습니다.

private static object ThisLock = new object();
...
object dataObject = Cache["globalData"];
if( dataObject == null )
{
    lock( ThisLock )
    {
        dataObject = Cache["globalData"];

        if( dataObject == null )
        {
            //Get Data from db
             dataObject = GlobalObj.GetData();
             Cache["globalData"] = dataObject;
        }
    }
}
return dataObject;

Pavel이 말한 것을 반영하기 위해 이것이 가장 스레드로부터 안전한 작성 방법이라고 생각합니다.

private T GetOrAddToCache<T>(string cacheKey, GenericObjectParamsDelegate<T> creator, params object[] creatorArgs) where T : class, new()
    {
        T returnValue = HttpContext.Current.Cache[cacheKey] as T;
        if (returnValue == null)
        {
            lock (this)
            {
                returnValue = HttpContext.Current.Cache[cacheKey] as T;
                if (returnValue == null)
                {
                    returnValue = creator(creatorArgs);
                    if (returnValue == null)
                    {
                        throw new Exception("Attempt to cache a null reference");
                    }
                    HttpContext.Current.Cache.Add(
                        cacheKey,
                        returnValue,
                        null,
                        System.Web.Caching.Cache.NoAbsoluteExpiration,
                        System.Web.Caching.Cache.NoSlidingExpiration,
                        CacheItemPriority.Normal,
                        null);
                }
            }
        }

        return returnValue;
    }

전체 캐시 인스턴스를 잠글 필요가 없으며 삽입하려는 특정 키만 잠그면됩니다. 즉, 남성 화장실 이용시 여성 화장실 출입을 차단할 필요가 없습니다. :)

아래 구현에서는 동시 사전을 사용하여 특정 캐시 키를 잠글 수 있습니다. 이렇게하면 두 개의 다른 키에 대해 GetOrAdd ()를 동시에 실행할 수 있지만 동시에 같은 키에 대해서는 실행할 수 없습니다.

using System;
using System.Collections.Concurrent;
using System.Web.Caching;

public static class CacheExtensions
{
    private static ConcurrentDictionary<string, object> keyLocks = new ConcurrentDictionary<string, object>();

    /// <summary>
    /// Get or Add the item to the cache using the given key. Lazily executes the value factory only if/when needed
    /// </summary>
    public static T GetOrAdd<T>(this Cache cache, string key, int durationInSeconds, Func<T> factory)
        where T : class
    {
        // Try and get value from the cache
        var value = cache.Get(key);
        if (value == null)
        {
            // If not yet cached, lock the key value and add to cache
            lock (keyLocks.GetOrAdd(key, new object()))
            {
                // Try and get from cache again in case it has been added in the meantime
                value = cache.Get(key);
                if (value == null && (value = factory()) != null)
                {
                    // TODO: Some of these parameters could be added to method signature later if required
                    cache.Insert(
                        key: key,
                        value: value,
                        dependencies: null,
                        absoluteExpiration: DateTime.Now.AddSeconds(durationInSeconds),
                        slidingExpiration: Cache.NoSlidingExpiration,
                        priority: CacheItemPriority.Default,
                        onRemoveCallback: null);
                }

                // Remove temporary key lock
                keyLocks.TryRemove(key, out object locker);
            }
        }

        return value as T;
    }
}

Craig Shoemaker는 asp.net 캐싱에 대한 훌륭한 쇼를 만들었습니다 : http://polymorphicpodcast.com/shows/webperformance/


다음과 같은 확장 방법을 생각해 냈습니다.

private static readonly object _lock = new object();

public static TResult GetOrAdd<TResult>(this Cache cache, string key, Func<TResult> action, int duration = 300) {
    TResult result;
    var data = cache[key]; // Can't cast using as operator as TResult may be an int or bool

    if (data == null) {
        lock (_lock) {
            data = cache[key];

            if (data == null) {
                result = action();

                if (result == null)
                    return result;

                if (duration > 0)
                    cache.Insert(key, result, null, DateTime.UtcNow.AddSeconds(duration), TimeSpan.Zero);
            } else
                result = (TResult)data;
        }
    } else
        result = (TResult)data;

    return result;
}

@John Owen과 @ user378380 답변을 모두 사용했습니다. 내 솔루션을 사용하면 캐시 내에 int 및 bool 값을 저장할 수도 있습니다.

오류가 있거나 조금 더 잘 쓸 수 있는지 정정하십시오.


최근에 올바른 State Bag Access Pattern이라는 패턴을 보았습니다.

스레드로부터 안전하도록 약간 수정했습니다.

http://weblogs.asp.net/craigshoemaker/archive/2008/08/28/asp-net-caching-and-performance.aspx

private static object _listLock = new object();

public List List() {
    string cacheKey = "customers";
    List myList = Cache[cacheKey] as List;
    if(myList == null) {
        lock (_listLock) {
            myList = Cache[cacheKey] as List;
            if (myList == null) {
                myList = DAL.ListCustomers();
                Cache.Insert(cacheKey, mList, null, SiteConfig.CacheDuration, TimeSpan.Zero);
            }
        }
    }
    return myList;
}

CodeGuru의이 기사에서는 다양한 캐시 잠금 시나리오와 ASP.NET 캐시 잠금에 대한 몇 가지 모범 사례를 설명합니다.

ASP.NET에서 캐시 액세스 동기화


저는 그 특정 문제를 해결하는 라이브러리 인 Rocks를 썼습니다.

또한이 문제에 대해 자세히 블로그를 작성하고 여기서 중요한 이유를 설명했습니다 .


더 많은 유연성을 위해 @ user378380의 코드를 수정했습니다. TResult를 반환하는 대신 이제 순서대로 다른 유형을 수락하기 위해 객체를 반환합니다. 또한 유연성을 위해 매개 변수를 추가합니다. 모든 아이디어는 @ user378380에 속합니다.

 private static readonly object _lock = new object();


//If getOnly is true, only get existing cache value, not updating it. If cache value is null then      set it first as running action method. So could return old value or action result value.
//If getOnly is false, update the old value with action result. If cache value is null then      set it first as running action method. So always return action result value.
//With oldValueReturned boolean we can cast returning object(if it is not null) appropriate type on main code.


 public static object GetOrAdd<TResult>(this Cache cache, string key, Func<TResult> action,
    DateTime absoluteExpireTime, TimeSpan slidingExpireTime, bool getOnly, out bool oldValueReturned)
{
    object result;
    var data = cache[key]; 

    if (data == null)
    {
        lock (_lock)
        {
            data = cache[key];

            if (data == null)
            {
                oldValueReturned = false;
                result = action();

                if (result == null)
                {                       
                    return result;
                }

                cache.Insert(key, result, null, absoluteExpireTime, slidingExpireTime);
            }
            else
            {
                if (getOnly)
                {
                    oldValueReturned = true;
                    result = data;
                }
                else
                {
                    oldValueReturned = false;
                    result = action();
                    if (result == null)
                    {                            
                        return result;
                    }

                    cache.Insert(key, result, null, absoluteExpireTime, slidingExpireTime);
                }
            }
        }
    }
    else
    {
        if(getOnly)
        {
            oldValueReturned = true;
            result = data;
        }
        else
        {
            oldValueReturned = false;
            result = action();
            if (result == null)
            {
                return result;
            }

            cache.Insert(key, result, null, absoluteExpireTime, slidingExpireTime);
        }            
    }

    return result;
}

참고URL : https://stackoverflow.com/questions/39112/what-is-the-best-way-to-lock-cache-in-asp-net

반응형