ASP.NET MVC 与 WCF,缓存通知问题

标签 asp.net wcf caching

我有一个 WCF,它向我的 asp.net 应用程序提供数据。我想在应用程序中缓存这些数据,但我不知道是否有现成的机制来发出依赖项通知。

详细来说,我需要这样的东西:

  • WCF 返回数据
  • 客户端获取此数据并将其放入缓存
  • 现在客户端从缓存返回数据
  • WCF 中的数据突然发生更改
  • 客户端收到通知(?)并用新数据再次填充缓存

如果没有现成的机制,你会如何解决这个问题?

最佳答案

我认为您在这里需要的是一个自定义 CacheDependency 对象,该对象每 n 秒调用一次 WCF 服务(其中 n 是可接受的延迟量)并与之前的结果来查看数据是否更新。然后,您的自定义依赖项可以调用其“NotifyDependencyChanged”方法,该方法告诉 ASP.NET 基础数据已更改并且缓存的对象已过时。有一个关于创建自定义 CacheDependency 对象的教程 here .

认为您的自定义 CacheDependency 看起来像这样(未经测试的代码):

/// <summary>
/// DTO for encapsulating stuff needed to create the dependency
/// </summary>
public class WebServiceCacheDependencySetup
{
    public object serviceClient { get; set; }
    public string clientMethod { get; set; }
    public int pollInterval { get; set; }
}

/// <summary>
/// Generic web services cache dependency
/// </summary>
public class WebServiceCacheDependency : CacheDependency
{
    private Timer timer;
    private static string previousHash;

    private object client;
    private string clientMethod;

    /// <summary>
    /// Constructor for the cache dependency
    /// </summary>
    /// <param name="setup">Object that specifies how to create dependency and call the dependent web service method</param>
    public WebServiceCacheDependency(WebServiceCacheDependencySetup setup)
    {
        // Create a timer with a specified poll interval
        timer = new Timer(CheckDependencyCallback, this, 0, setup.pollInterval);

        client = setup.serviceClient;
        clientMethod = setup.clientMethod;

        previousHash = string.Empty;
    }

    public void CheckDependencyCallback(object sender)
    {
        // Reflect on the service's proxy to find the method we want to call
        Type clientType = client.GetType();
        MethodInfo method = clientType.GetMethod(clientMethod);

        // Programmatically invoke the method and get the return value
        object returnValue = method.Invoke(client, null);

        // Cast the return to a byte array so we can hash it
        Byte[] returnBytes = (Byte[])returnValue;

        using (SHA512Managed hashAlgorithm = new SHA512Managed())
        {
            // Hash the return value into a string
            Byte[] hashedBytes = hashAlgorithm.ComputeHash(returnBytes);
            string hashedValue = Convert.ToBase64String(hashedBytes);

            // Compare the new hash to the last hash
            if (hashedValue != previousHash)
            {
                // If the hashes don't match then the web service result has changed
                // so invalidate the cached object
                NotifyDependencyChanged(this, EventArgs.Empty);

                // Tear down this instance
                timer.Dispose();
            }
        }

    }

    protected override void DependencyDispose()
    {
        if (timer != null)
        {
            timer.Dispose();
        }
    }
}

关于ASP.NET MVC 与 WCF,缓存通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061566/

相关文章:

c# - MSChart:ChartImageHandler 不同存储设置的优缺点

C# 动态 getter 和 setter 取决于应用上下文

c# - 如何使用 DataContractSerializer 序列化 WCF 消息?

visual-studio - 如何在 Visual Studio 2017 中更新 OData WCF?

javascript - YSlow 说我的 JavaScript 文件在合并 5 个文件并打包后不再缓存

asp.net - FindControl 如果从不同的类调用则不起作用

c# - Azure 服务总线主题超时异常

java - Hibernate session.createCriteria 与 session.get 性能对比

caching - Nginx 缓存 : tag-based cache-busting like Varnish Hashtwo

ASP.Net Web 服务 : What object type to use as collection of parameters?