0

我有一台计算机(X1),其中运行 VS 2010 中构建的 Web 应用程序,并在此应用程序中安装和使用 Appfabric。我有另一台计算机(X2)也安装了 Appfabric。两台计算机都连接在一个工作组中。

例如,目前我在 X1 中创建了一个缓存“NamedCache1”,并使用它来存储值并从 X1 中的应用程序中引用它。我在 X2 中创建了一个缓存“NamedCache2”,我想在我的应用程序中使用这个缓存“NamedCache2”。我怎样才能实现这一点?

4

1 回答 1

0

关于您在哪里配置 AppFabric,这个问题有点模糊。我假设您已经完成了配置 AppFabric 缓存的众多 Google 结果之一中的步骤,例如Scott Hanselman的步骤。如果您在该链接中查看他的 CacheUtil 类,您可以修改它以访问两个缓存。

using Microsoft.ApplicationServer.Caching;
using System.Collections.Generic;

public class CacheUtil
{
    private static DataCacheFactory _factory = null;
    private static DataCache _NamedCache1 = null;
    private static DataCache _NamedCache2 = null;

    public static DataCache GetNamedCache1()
    {
        if (_NamedCache1 != null)
            return _NamedCache1;

        //Define Array for 2 Cache Hosts
        List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(2);

        //Specify Cache Host Details 
        //  Parameter 1 = host name
        //  Parameter 2 = cache port number
        servers.Add(new DataCacheServerEndpoint("X1", 22233));
        servers.Add(new DataCacheServerEndpoint("X2", 22233));

        //Create cache configuration
        DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();

        //Set the cache host(s)
        configuration.Servers = servers;

        //Set default properties for local cache (local cache disabled)
        configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();

        //Disable tracing to avoid informational/verbose messages on the web page
        DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

        //Pass configuration settings to cacheFactory constructor
        _factory = new DataCacheFactory(configuration);

        //Get reference to named cache called "default"
        _NamedCache1 = _factory.GetCache("NamedCache1");

        return _NamedCache1;
    }

    public static DataCache GetNamedCache2()
    {
        if (_NamedCache2 != null)
            return _NamedCache2;

        //Define Array for 2 Cache Hosts
        List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(2);

        //Specify Cache Host Details 
        //  Parameter 1 = host name
        //  Parameter 2 = cache port number
        servers.Add(new DataCacheServerEndpoint("X1", 22233));
        servers.Add(new DataCacheServerEndpoint("X2", 22233));

        //Create cache configuration
        DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();

        //Set the cache host(s)
        configuration.Servers = servers;

        //Set default properties for local cache (local cache disabled)
        configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();

        //Disable tracing to avoid informational/verbose messages on the web page
        DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

        //Pass configuration settings to cacheFactory constructor
        _factory = new DataCacheFactory(configuration);

        //Get reference to named cache called "default"
        _NamedCache2 = _factory.GetCache("NamedCache2");

        return _NamedCache2;
    }
}
于 2012-12-21T15:38:48.807 回答