从另一台计算机访问Appfabric缓存

时间:2012-12-21 13:15:23

标签: asp.net

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

例如,目前我在X1中创建了一个缓存“NamedCache1”,并使用它来存储值并从我在X1中的应用程序引用它。我在X2中创建了一个缓存“NamedCache2”,我想在我的应用程序中使用此缓存“NamedCache2”。我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

关于配置AppFabric的位置,问题有点模糊。我假设您已经完成了许多Google搜索结果中的一个步骤来配置AppFabric缓存,例如the one by 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;
    }
}