Microsoft Distributed cache appfabric (velocity)-Basic Cache Usage

Source: Internet
Author: User

Previous articlesArticleAfter the theory is explained, we will start to practice

I. cache service preparation

I don't have much resources, and everything is installed on my PC (this notebook is not owned by me and has a long history. I also lost a key. Poor, I can only connect to an external keyboard ). the installation is completed using the default settings.

Use the built-in powershell tool after installation to add a cache named "firstcache. Run the following command:

New-Cache firstcache

UseRun the get-cachehost command to view the existing host:

Host Name: FENGXU-MSFT, port number is the default 22233, service name default: distributedcacheservice

UseGet-Cache command to view the existing Cache

We can see that there are two caches in the cluster. default is the default cache after installation, and firstcache is the cache you just created.

Ii. Use of basic cache APIs

 Create a project and add fourProgramSet Reference, configure the app. config file as follows

App. config  <? XML version = "1.0" ?>  <  Configuration  >    <! -- Configsections must be the first element -->    <  Configsections  >      <! -- Required to read the <datacacheclient> element -->      < Section   Name = "Datacacheclient"   Type = "Microsoft. Data. caching. datacacheclientsection, cachebaselibrary"   Allowlocation = "True"   Allowdefinition = "Everywhere"  />      <! -- Required to read the <fabric> element, when present -->      < Section   Name = "Fabric"   Type = "System. Data. fabric. Common. configfile, fabriccommon"   Allowlocation = "True"   Allowdefinition = "Everywhere"  />    </  Configsections  >   <! -- Routing client -->    <  Datacacheclient   Deployment = "Simple"   Timeout = "30000"  >      <! -- (Optional) specify local cache <localcache isenabled = "true" Sync = "ttlbased" objectcount = "100000" ttlvalue = "300"/> -->      <! -- (Optional) specify cache restrictions poll interval <clientnotification pollinterval = "300"/> -->     <! -- Cache host (s) -->      <  Hosts  >        <  Host   Name = FENGXU-MSFT"   Cacheport = "22233"   Cachehostname = "Distributedcacheservice"  />      </ Hosts  >    </  Datacacheclient  >  <  Startup  >  <  Supportedruntime   Version = "V4.0"   SKU = ". Netframework, version = v4.0" />  </  Startup  >  </  Configuration  > 

The local cache is used here. In the host node, the host name is the host name of the cache hosts, and the cachehostname is the service name.

InCodeBasic read/write

 Basic usage of cache Client  Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; // Add? ? Null? Between?  Using Microsoft. Data. caching; Namespace Volocitydemo { Class Basemethod {datacachefactory factory; Public Basemethod (){ // Fang? Legal disclaimer? I? : Enabled? Use? Mo? Recognize? Configuration? Set? Create a role? Jian Yu Factory = New Datacachefactory (); // Generation? Code? Is the D dynamic? ? Dingju             // Fang? Legal disclaimer? Two T: includeclare array for cache host (s)              // Datacacheserverendpoint [] servers = new datacacheserverendpoint [1];              //// Specify cache host (s)              // Servers [0] = new datacacheserverendpoint ("FENGXU-MSFT ",              /// 22233, "distributedcacheservice ");              // Factory = new datacachefactory (servers, false, false ); }~ Basemethod () {factory. Dispose ();} Public   Void Test () {datacache = factory. getcache ("Firstcache "); // Add? ? Right? Míó              If (Datacache. Get (" Time1 ") = Null ) {Datacache. Add (" Time1 ", Datetime. Now);} console. writeline ( String . Format (" Read? Slow o storage? Right? Like ótime1: hour {0: yyyy-mm-dd hh: mm: SS} ", Datacache. Get (" Time1 "); Datacache. Put (" Time1 ", Datetime. Now); console. writeline ( String . Format (" Read? Change U new? ? Slow o storage? Right? Like ótime1: hour {0: yyyy-mm-dd hh: mm: SS} ", Datacache. Get (" Time1 "))); // Shift? Except y pair? Míó              // Remove object in cache using array notation Datacache [" Time1 "] = Null ; // Remove object in cache using key "key0"              // Datacache. Remove ("key0 "); }}}

When the cache cluster restarts, the cache service will load the existing cache configuration from the configuration file, and the firstcache will still exist. You can also use the powershell tool.Export-cacheclusterconfig commandFirst export the cache configuration first, and then useImport-cacheclusterconfig command Export

The Exported configuration in the example is as follows:

 Exported configuration  <? XML version = "1.0" encoding = "UTF-8" ?>  <  Configuration  >      <  Configsections  >         <  Section   Name = "Datacache"   Type = "Microsoft. Data. caching. datachesection, cachebaselibrary, version = 1.0.0.0, culture = neutral, publickeytoken = 89845dcd80cc91"   />      </  Configsections  >      <  Datacache   Cluster = "Democache"   Size = "Small"  >          <  Caches  >              <  Cache   Type = "Partitioned"   Consistency = "Strong"   Name ="Default"  >                  <  Policy  >                      <  Eviction   Type = "LRU"   />                      <  Expiration   Defaultttl = "10"   Isexpirable = "True"   />                  </  Policy  >              </  Cache  >              <  Cache   Type = "Partitioned"   Consistency = "Strong"  Name = "Firstcache"  >                  <  Policy  >                      <  Eviction   Type = "LRU"   />                      <  Expiration   Defaultttl ="10"   Isexpirable = "True"   />                  </  Policy  >              </  Cache  >          </  Caches  >          <  Hosts >              <  Host   Clusterport = "22234"   Hostid = "1743595319"   Size = "1228"   Quorumhost = "True"                  Name = FENGXU-MSFT"   Cachehostname = "Distributedcacheservice"                  Cacheport = "22233"   />          </  Hosts  >          <  Advancedproperties  >              <  Partitionstoreconnectionsettings   Providername ="System. Data. sqlserverce.3.5"                  Connectionstring = "\ 127.0.0.1 \ microsoftdistributecache \ configstore. SDF"   />          </  Advancedproperties  >      </  Datacache  >  </  Configuration  > 

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.