Windows Server AppFabric Cache 介绍一

         我们知道现在基于net 4.0的缓存可以使用Windows Server AppFabric,今天来介绍一下。首先我的环境是Win7 Enterprise,安装II7相关组件。下载安装包,或通过

Web Platform Installer 进行安装也可以了。

         imagesCAS887BA

        clip_image0028

接下来是配置Windows Server AppFabric Cache,有一个向导也很简单,此处我们选择默认的SQL Server AppFabric Caching Service Configuration Store Provider配置

image3

接着会让你创建数据库或注册现有的数据库,下面是配制端口:

image7

最后Finish,窗口就关闭了。然后打开Windows PowerShell 按制台,顺序执行下面这些Command :

增加一个分布式Cache管理模块

Import-Module DistributedCacheAdministration
设置cache cluster
Use-CacheCluster

授权你的某个帐户 domain\username :

Grant-CacheAllowedClientAccount domain\username

验证授权是否成功:

Get-CacheAllowedClientAccounts

最后启动:

Start-CacheCluster
这时你可以下载官方的Microsoft AppFabric Samples。以WEB测试,看下面的CODE很简明:
   1:  public class CacheUtil
   2:  {
   3:    private static DataCacheFactory _factory = null;
   4:    private static DataCache _cache = null;
   5:   
   6:    public static DataCache GetCache()
   7:    {
   8:        if (_cache != null)
   9:            return _cache;
  10:   
  11:        //-------------------------
  12:        // Configure Cache Client 
  13:        //-------------------------
  14:   
  15:        //Define Array for 1 Cache Host
  16:        List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(1);
  17:   
  18:        //Specify Cache Host Details 
  19:        //  Parameter 1 = host name
  20:        //  Parameter 2 = cache port number
  21:        servers.Add(new DataCacheServerEndpoint("localhost", 22233));
  22:   
  23:        //Create cache configuration
  24:        DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
  25:        
  26:        //Set the cache host(s)
  27:        configuration.Servers = servers;
  28:        
  29:        //Set default properties for local cache (local cache disabled)
  30:        configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();
  31:   
  32:        //Disable tracing to avoid informational/verbose messages on the web page
  33:        DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);
  34:   
  35:        //Pass configuration settings to cacheFactory constructor
  36:        _factory = new DataCacheFactory(configuration);
  37:   
  38:        //Get reference to named cache called "default"
  39:        _cache = _factory.GetCache("default");
  40:        
  41:      return _cache;
  42:    }
  43:  }

主要引用Microsoft.ApplicationServer.Caching.Client与Microsoft.ApplicationServer.Caching.Core两个程序集,上面主要应用DataCacheFactory与DataCache类.

打开CacheSampleWebApp.sln的,F5运行可以看到效果,CreateOrder,UpdateOrder,GetOrder操作的都是Cache。上面的代码注册端口,与host也可以配制在Web.config中。

 

希望对您开发有帮助。


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

原文地址:https://www.cnblogs.com/wintersun/p/1968229.html