首页 > 代码库 > 微软高性能缓存AppFabric(二)使用

微软高性能缓存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();

微软高性能缓存AppFabric(二)使用