標籤:config ever title factor ber data 選擇 hit 網易
原文連結: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(二)使用