CacheItemRemovedCallback CacheItemRemovedCallback CacheItemRemovedCallback CacheItemRemovedCallback Delegate

Definición

Define un método de devolución de llamada para notificar a las aplicaciones cuándo se quita un elemento almacenado en caché del objeto Cache.Defines a callback method for notifying applications when a cached item is removed from the Cache.

public delegate void CacheItemRemovedCallback(System::String ^ key, System::Object ^ value, CacheItemRemovedReason reason);
public delegate void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason);
type CacheItemRemovedCallback = delegate of string * obj * CacheItemRemovedReason -> unit
Public Delegate Sub CacheItemRemovedCallback(key As String, value As Object, reason As CacheItemRemovedReason)

Parámetros

key
String String String String

Clave que se elimina de la caché.The key that is removed from the cache.

value
Object Object Object Object

Elemento Object asociado a la clave eliminada de la caché.The Object item associated with the key removed from the cache.

reason
CacheItemRemovedReason CacheItemRemovedReason CacheItemRemovedReason CacheItemRemovedReason

Razón por la que se quitó el elemento de la memoria caché, tal y como especifica la enumeración de CacheItemRemovedReason.The reason the item was removed from the cache, as specified by the CacheItemRemovedReason enumeration.

Herencia
CacheItemRemovedCallbackCacheItemRemovedCallbackCacheItemRemovedCallbackCacheItemRemovedCallback

Ejemplos

En el ejemplo de código siguiente se muestra una página que muestra a los usuarios el valor asignado a un elemento en la memoria caché y, a continuación, los notifica cuando el elemento se quita de la memoria caché.The following code example demonstrates a page that shows users the value assigned to an item in the cache and then notifies them when the item is removed from the cache. Crea un RemovedCallback método, que usa la firma CacheItemRemovedCallback del delegado, para notificar a los usuarios cuando se quita el elemento de la memoria CacheItemRemovedReason caché y utiliza la enumeración para indicarles por qué se quitó.It creates a RemovedCallback method, which uses the signature of the CacheItemRemovedCallback delegate, to notify users when the cache item is removed and uses the CacheItemRemovedReason enumeration to tell them why it was removed. Además, utiliza la Cache.Item[String] propiedad para agregar objetos a la memoria caché y recuperar el valor de esos objetos.In addition, it uses the Cache.Item[String] property to add objects to the cache and retrieve the value of those objects. En el AddItemToCache método, usa el Cache.Add método para agregar un elemento a la memoria caché.In the AddItemToCache method, it uses the Cache.Add method to add an item to the cache. Para usar el CacheItemRemovedCallback delegado, debe agregar un elemento a la memoria caché con este método o el Cache.Insert método para que ASP.net pueda realizar automáticamente las llamadas de método apropiadas cuando se quite el elemento.To use the CacheItemRemovedCallback delegate, you must add an item to the cache with this method or the Cache.Insert method so that ASP.NET can automatically make the proper method calls when the item is removed. El método RemoveItemFromCache personalizado usa el Cache.Remove método para eliminar explícitamente el elemento de la memoria caché, RemovedCallback lo que hace que se invoque el método.The custom RemoveItemFromCache method uses the Cache.Remove method to explicitly delete the item from the cache, causing the RemovedCallback method to be invoked.

<html>
 <Script runat=server language="C#">
// <snippet2>
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
    }
// </snippet2>

// <snippet3>
    public void AddItemToCache(Object sender, EventArgs e) {
        itemRemoved = false;

        onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

        if (Cache["Key1"] == null)
          Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
    }
// </snippet3>

// <snippet4>
    public void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
// </snippet4>
 </Script>
 <body>
  <Form runat="server">
   <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
   <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
  <% if (itemRemoved) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("<BR>");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     }
     else {
// <snippet5>
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
// </snippet5>
     }
  %>
 </body>
</html>
<%@ Page Language="VB" %>

<html>
 <Script runat=server>
' <snippet2>
    Shared itemRemoved As boolean = false
    Shared reason As CacheItemRemovedReason
    Dim onRemove As CacheItemRemovedCallback

    Public Sub RemovedCallback(k As String, v As Object, r As CacheItemRemovedReason)
      itemRemoved = true
      reason = r
    End Sub
' </snippet2>

' <snippet3>
    Public Sub AddItemToCache(sender As Object, e As EventArgs)
        itemRemoved = false

        onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

        If (IsNothing(Cache("Key1"))) Then
          Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
        End If
    End Sub
' </snippet3>

' <snippet4>
    Public Sub RemoveItemFromCache(sender As Object, e As EventArgs)
        If (Not IsNothing(Cache("Key1"))) Then
          Cache.Remove("Key1")
        End If
    End Sub
' </snippet4>
 </Script>

 <body>
  <Form runat="server">
    <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
    <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
<%
If (itemRemoved) Then
    Response.Write("RemovedCallback event raised.")
    Response.Write("<BR>")
    Response.Write("Reason: <B>" + reason.ToString() + "</B>")
Else
' <snippet5>
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
' </snippet5>
End If
%>
 </body>
</html>

Métodos de extensión

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

Obtiene un objeto que representa el método representado por el delegado especificado.Gets an object that represents the method represented by the specified delegate.

Se aplica a

Consulte también: