Pregunta

Sé que en determinadas circunstancias, como procesos de ejecución prolongada, es importante bloquear el caché de ASP.NET para evitar que solicitudes posteriores de otro usuario para ese recurso ejecuten el proceso largo nuevamente en lugar de acceder al caché.

¿Cuál es la mejor manera en C# de implementar el bloqueo de caché en ASP.NET?

¿Fue útil?

Solución

Aquí está el patrón básico:

  • Verifique el valor en la caché, devuélvalo si está disponible
  • Si el valor no está en el caché, implemente un bloqueo
  • Dentro de la cerradura, revisa el caché nuevamente, es posible que hayas sido bloqueado
  • Realice la búsqueda de valores y guárdelo en caché
  • Liberar el bloqueo

En código, se ve así:

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

}

Otros consejos

Para completar, un ejemplo completo se vería así.

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;

Solo para hacerme eco de lo que dijo Pavel, creo que esta es la forma más segura de escribirlo.

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;
    }

No es necesario bloquear toda la instancia de caché, sino que solo necesitamos bloquear la clave específica que está insertando.Es decir.No es necesario bloquear el acceso al baño femenino mientras usas el baño masculino :)

La siguiente implementación permite bloquear claves de caché específicas utilizando un diccionario concurrente.De esta manera puedes ejecutar GetOrAdd() para dos claves diferentes al mismo tiempo, pero no para la misma clave al mismo tiempo.

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 ha hecho un excelente espectáculo sobre el almacenamiento en caché de asp.net:http://polymorphicpodcast.com/shows/webperformance/

Se me ocurrió el siguiente método de extensión:

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;
}

He utilizado las respuestas de @John Owen y @user378380.Mi solución también le permite almacenar valores int y bool dentro del caché.

Corríjame si hay algún error o si se puede escribir un poco mejor.

Recientemente vi un patrón llamado Patrón de acceso a la bolsa de estado correcto, que parecía tocar esto.

Lo modifiqué un poco para que fuera seguro para subprocesos.

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;
}

Este artículo de CodeGuru explica varios escenarios de bloqueo de caché, así como algunas prácticas recomendadas para el bloqueo de caché de ASP.NET:

Sincronización del acceso a la caché en ASP.NET

Escribí una biblioteca que resuelve ese problema en particular: Rocas.Almacenamiento en caché

También escribí un blog sobre este problema en detalle y expliqué por qué es importante. aquí.

Modifiqué el código de @user378380 para mayor flexibilidad.En lugar de devolver TResult, ahora devuelve un objeto para aceptar diferentes tipos en orden.También se agregan algunos parámetros para mayor flexibilidad.Toda la idea pertenece a @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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top