zoukankan      html  css  js  c++  java
  • 微软高性能缓存AppFabric(二)使用

    原文链接:http://www.cnblogs.com/Qbit/p/6102614.html 

    从AppFabric 的安装目录中选择两个dll添加到项目中,

    默认安装位置:C:Program Files用于 Windows Server 的 AppFabric 1.1

    方法一: 使用代码指定配置:

    创建缓存对象,在最后 指定的缓存名称处参考:

    微软高性能缓存AppFabric (一) 安装文章里PowerShell 创建的缓存名称

    "使用 New-Cache 命令创建所有必要的命名缓存。"

    private DataCache GetCurrentCache()
    
    {
    
     
    
    DataCache dCache;
    
    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
    
    servers[0] = new DataCacheServerEndpoint
    
    (this.txtAppFabricServerHost.Text,//主机名或IP地址
    
    (int)this.numAppFabricPortNumber.Value);//端口号默认:22233 这里总让我想到网易的表情。。#23 !.. 
    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
    
    factoryConfig.Servers = servers;
    
    var factory = new DataCacheFactory(factoryConfig);
    
    dCache = factory.GetCache(this.txtAppFabricCacheName.Text);//指定缓存名称这里参考
    
     
    
    return dCache;
    
     
    
    }

    方法二:使用配置文件配置缓存

    这种配置方法十分灵活,而且程序代码也比较简洁,只需要在配置文件中添加相应的配置即可。

    private DataCache GetDefaultCache()
    
    {
    
        var factory = new DataCacheFactory();
    
        var dCache = factory.GetCache(this.txtAppFabricCacheName.Text);//缓存名称 
    
        return dCache;
    
    }
    

      

    Config文件:

    首先需要在configuration 添加一个Section,用来识别我们的配置

       <configSections>
          <!-- required to read the <dataCacheClient> element -->
          <section name="dataCacheClient"
             type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
                Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, 
                Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             allowLocation="true"
             allowDefinition="Everywhere"/>
       </configSections>

    然后添加缓存配置:

        <dataCacheClient>
          <hosts>
             <host
                name="127.0.0.1"
                cachePort="22233"/>
             <!--<host
                name="CacheServer2"
                cachePort="22233"/>-->
          </hosts>
       </dataCacheClient>

    使用AppFabric 进行操作缓存:

    //参考文档:https://msdn.microsoft.com/zh-cn/library/ee790846.aspx

    // Declare array for cache host(s).

    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];

    servers[0] = new DataCacheServerEndpoint("127.0.0.1", 22233);

    // Setup the DataCacheFactory configuration.

    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();

    factoryConfig.Servers = servers;

    // Create a configured DataCacheFactory object.

    DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig);

    // Get a cache client for the cache "NamedCache1".

    DataCache myCache = mycacheFactory.GetCache("NamedCache1");

    //add string object to cache with key "Key0"

    #region 添加或更新

    try

    {

    myCache.Add("Key0", "object added with Key0");

    }

    catch (Exception ex)

    {

    throw;

    }

    //add or replace string object in cache using key "Key0"

    myCache.Put("Key0", "object replaced or added using Key0");

    //add or replace object in cache using array notation

    myCache["Key0"] = "object replaced or added using Key0";

    #endregion

    #region 获取

    //get string from cache using key "Key0"

    string myString1 = (string)myCache.Get("Key0");

    //get string from cache using array notation

    string myString2 = (string)myCache["Key0"];

    #endregion

    #region 删除

    //remove object in cache using key "Key0"

    myCache.Remove("Key0");

    //remove object in cache using array notation

    myCache["Key0"] = null;

    #endregion

    return View();

    补充在最后:

    看到其他兄弟写的一些Velocity的代码,数据是存储在一些Region 中的。这样在需要删除一部分Key的时候确实很方便。但是今天配置Session过程中注意到一条官方提醒

     存储在region中的对象将不会在缓存主机之间负载平衡,但位于创建region的缓存主机中。因此,它通常不是推荐的配置。

    仅当特殊要求查找单个主机上的所有会话对象时,才应该使用region

  • 相关阅读:
    双击导航栏自动滑动ListView到顶部
    及时取消代码中的AsyncTask
    Nubia Z9 mini使用体验
    特殊情况特殊处理
    SharePreferences的DB实现
    时下手机和p2p理财的共同点
    小米空气净化器体验
    消息框架的一种实现
    刷了MIUI的手机在OSX下连接USB调试的方法
    16个最佳响应式HTML5框架
  • 原文地址:https://www.cnblogs.com/Qbit/p/AppFabric.html
Copyright © 2011-2022 走看看