¿Hay una manera de utilizar el uso de un perfil de caché al añadir manualmente elementos a la caché?

StackOverflow https://stackoverflow.com/questions/4761657

Pregunta

Tengo memoria caché de salida en mi aplicación configurada utilizando perfiles de memoria caché de resultados en el web.config. Es muy conveniente para poder almacenamiento en caché de configuración en todos los elementos de salida que lo necesitan y luego ser capaz de ajustar todos los valores de caché en un solo lugar.

Sin embargo, también estoy implementando el almacenamiento en caché en mis datos y capas lógicas para ciertos artículos. Sería conveniente si yo también podría hacer referencia a un perfil en vez del disco de codificación de los parámetros de almacenamiento en caché para datos y elementos lógicos que quiero caché, pero no parece ser una manera de hacer referencia a un perfil en el método Insert () en el memoria caché de objetos.

Alternativly, pude construir mi propia sección de configuración de los perfiles de la lista de caché para los artículos añadidos manualmente.

¿Fue útil?

Solución

Se puede obtener una lista de perfiles de caché de resultados que hacen esto:

private Dictionary<string, OutputCacheProfile> _outputCacheProfiles;
/// <summary>
/// Initializes <see cref="OutputCacheProfiles"/> using the settings found in
/// "system.web\caching\outputCacheSettings"
/// </summary>
void InitializeOutputCacheProfiles(
            System.Configuration.Configuration appConfig,
            NameValueCollection providerConfig)
{
    _outputCacheProfiles = new Dictionary<string, OutputCacheProfile>();

    OutputCacheSettingsSection outputCacheSettings = 
          (OutputCacheSettingsSection)appConfig.GetSection("system.web/caching/outputCacheSettings");

    if(outputCacheSettings != null)
    {
        foreach(OutputCacheProfile profile in outputCacheSettings.OutputCacheProfiles)
        {
            _outputCacheProfiles[profile.Name] = profile;
        }
    }
}

Y luego utilizarlo en su inserción:

/// <summary>
/// Gets the output cache profile with the specified name
/// </summary>
public OutputCacheProfile GetOutputCacheProfile(string name)
{
    if(!_outputCacheProfiles.ContainsKey(name))
    {
        throw new ArgumentException(String.Format("The output cache profile '{0}' is not registered", name));
    }
    return _outputCacheProfiles[name];
}

  /// <summary>
    /// Inserts the key/value pair using the specifications of the output cache profile
    /// </summary>
    public void InsertItemUsing(string outputCacheProfileName, string key, object value)
    {
        OutputCacheProfile profile = GetOutputCacheProfile(outputCacheProfileName);
        //Get settings from profile to use on your insert instead of hard coding them
    }

Otros consejos

Si usted se refiere a C # 's objeto Cache.Insert puede anexar un GUID a la clave para que cada perfil tiene un GUID correspondiente que se puede extraer de la memoria caché cuando se quiere recuperar el perfil más adelante.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top