Microsoft High Performance Cache AppFabric (ii) uses

Source: Internet
Author: User

Original link: http://www.cnblogs.com/Qbit/p/6102614.html

Select two DLLs from the installation directory of AppFabric to add to the project,

Default installation location: C:\Program files\ for Windows Server for AppFabric 1.1\

Method One: Specify the configuration Using code:

Create a cache object, which is referenced at the last specified cache name: the cache name created by PowerShell in The Microsoft High Performance cache AppFabric (i) installation article

"Use the new-cache command to create all the necessary named caches. "

PrivateDatacache Getcurrentcache () {Datacache dcache;datacacheserverendpoint[] servers=Newdatacacheserverendpoint[1];servers[0] =NewDatacacheserverendpoint ( This. Txtappfabricserverhost.text,//host name or IP address(int) This. Numappfabricportnumber.value);//Port number default: 22233 It always reminds me of NetEase's expression. #23!.. datacachefactoryconfiguration Factoryconfig=Newdatacachefactoryconfiguration (); Factoryconfig.servers=servers;varFactory =Newdatacachefactory (factoryconfig);d cache= Factory. GetCache ( This. Txtappfabriccachename.text);//Specify the cache name here for reference returnDcache;}

Method Two: Configure the cache with a configuration file

This configuration method is very flexible, and the program code is relatively concise, just need to add the appropriate configuration in the configuration file.

Private Datacache Getdefaultcache () {    var factory = new Datacachefactory ();    var Dcache = factory. GetCache (This.txtAppFabricCacheName.Text);//cache name     return dcache;}

  

Config file:

You first need to Add asection to the configuration to identify our configurations

   <configsections>      <!--required to read the <dataCacheClient> element -      < Sectionname= "Datacacheclient"type= "Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Ver sion=1.0.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35 "allowlocation= "true"allowdefinition= "Everywhere"/>   </configsections>

then add the cache configuration :

    <datacacheclient>      <hosts>         <Hostname= "127.0.0.1"Cacheport= "22233"/>         <!-- -      </hosts>   </datacacheclient>

To operate the cache using AppFabric:

//Reference document: 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 Add or update

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

//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 Delete

//remove object in cache using key "Key0"

Mycache.remove ("Key0");

//remove object in cache using array notation

mycache["Key0"] = null;

#endregion

return View ();

Microsoft High Performance Cache AppFabric (ii) uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.