zoukankan      html  css  js  c++  java
  • 使用微软分布式缓存服务Velocity Part 2

    概述

    Velocity是微软推出的分布式缓存解决方案,为开发可扩展性,可用的,高性能的应用程提供支持,可以缓存各种类型的数据,如CLR对象、XML、二进制数据等,并且支持集群模式的缓存服务器。Velocity也将集成在.NET Framework 4.0中,本文将介绍Velocity中的配置模型、缓存复杂数据和创建分区、使用标签以及ASP.NET SessionState提供者。

    配置模型

    在本文开始之前,先简单介绍一下Velocity中的配置模型,主要包括三方面的配置,缓存集群的配置,缓存宿主服务器配置以及应用程序的配置,如下图所示:

    Velocity_001

    缓存集群的配置,可以基于XML、SQL Server CE或者SQL Server数据库来进行存储,包括各个服务器以及所有的命名缓存、是否过期等配置,当我们使用Windows PowerShell管理工具进行配置时,将会修改该配置文件,如下代码所示:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="dcache" type="System.Data.Caching.DCacheSection, 
                 CacheBaseLibrary, Version=1.0.0.0, Culture=neutral,
                 PublicKeyToken=89845dcd8080cc91" />
      </configSections>
      <dcache cluster="localhost" 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="other">
            <policy>
              <eviction type="lru" />
              <expiration defaultTTL="10" isExpirable="true" />
            </policy>
          </cache>
        </caches>
        <hosts>
          <host clusterPort="22234" hostId="1319514812" size="1024" quorumHost="true"
              name="TERRYLEE-PC" cacheHostName="DistributedCacheService"
              cachePort="22233" />
        </hosts>
        <advancedProperties>
          <partitionStoreConnectionSettings providerName="System.Data.SqlServerCe.3.5"
              connectionString="D:\CacheShare\ConfigStore.sdf" />
        </advancedProperties>
      </dcache>
    </configuration>

    在上一篇的示例中,并没有使用应用程序配置文件,事实上使用配置文件是更好的编程实践,首先需要添加一个配置区:

    <section name="dcacheClient"
    type="System.Data.Caching.DCacheSection, 
          CacheBaseLibrary, Version=1.0.0.0, 
          Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    配置信息包括部署方式,是否启用本地缓存以及缓存宿主等,如下代码所示:
    
    <dcacheClient>
      <localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
      <hosts>
        <host name="localhost" cachePort="22233"
              cacheHostName="DistributedCacheService"/>
      </hosts>
    </dcacheClient>

    现在Velocity CTP2对于应用程序使用配置的支持似乎有些问题。缓存宿主的配置放在DistributedCache.exe.config文件中,可以在Velocity安装目录下找到。

    缓存复杂数据类型

    在Velocity中,可以缓存任何类型的数据,如CLR对象、XML或者二进制数据等。现在看一个简单的示例,如何缓存复杂类型数据,定义一个如下的Customer类,注意要能够序列化:

    [Serializable]
    public class Customer
    {
        public String ID { get; set; }
    
        public String FirstName { get; set; }
    
        public String LastName { get; set; }
    
        public int Age { get; set; }
    
        public String Email { get; set; }
    }

    对应用程序做配置,参考本文的配置模型部分,使用方法与简单数据类型的基本一致,如添加缓存项,使用Customer主键作为缓存键,其中GetCurrentCache()方法的实现请参考上一篇文章:

    Cache cache = GetCurrentCache();
    Customer customer = new Customer()
    {
        ID = "C20081117002",
        FirstName = "Terry",
        LastName = "Lee",
        Age = 25,
        Email = "lhj_cauc[#AT#]163.com"
    };
    
    cache.Add(customer.ID, customer);

    获取缓存项:

    Cache cache = GetCurrentCache();
    Customer customer = cache.Get("C20081117002") as Customer;

    移除缓存项:

    Cache cache = GetCurrentCache();
    cache.Remove("C20081117002");

    更新缓存中数据,可以有两种方法,一是直接使用缓存索引,如果确保缓存键存在:

    Cache cache = GetCurrentCache();
    Customer customer = new Customer()
    {
        ID = "C20081117002",
        FirstName = "Huijui",
        LastName = "Li",
        Age = 26,
        Email = "lhj_cauc[#AT#]163.com"
    };
    cache["C20081117002"] = customer;

    另外一种是使用Put方法,如果缓存键不存在,它将会新增到缓存中,否则会进行覆盖,如下代码所示:

    Cache cache = GetCurrentCache();
    Customer customer = new Customer()
    {
        ID = "C20081117002",
        FirstName = "Huijui",
        LastName = "Li",
        Age = 26,
        Email = "lhj_cauc[#AT#]163.com"
    };
    cache.Put(customer.ID, customer);

    使用分区

    在实际部署中,经常会出现多个应用程序共享同一个缓存集群,这不可避免的会出现缓存键冲突,如上面的示例中使用CustomerID作为缓存键,此时可以使用Velocity中的分区功能,它会在逻辑上把各个命名缓存再进行分区,这样可以完全保持数据隔离,如下图所示:

    TerryLee_0216

    图中共有三个命名缓存,其中在缓存Catalog中又分区为Sports和Arts。在Velocity中对于分区的操作提供了如下三个方法,可以用于创建分区,删除分区以及清空分区中所有的对象:

    public void ClearRegion(string region);
    public bool CreateRegion(string region, bool evictable);
    public bool RemoveRegion(string region);

    如下代码所示,创建了一个名为“Customers”的分区,在调用Add方法时可以指定数据将会缓存到哪个分区:

    Cache cache = GetCurrentCache();
    string regionName = "Customers";
    cache.CreateRegion(regionName, false);
    
    Customer customer = new Customer()
    {
        ID = "C20081117003",
        FirstName = "Terry",
        LastName = "Lee",
        Age = 25,
        Email = "lhj_cauc[#AT#]163.com"
    };
    
    cache.Add(regionName, customer.ID, customer);

    可以使用Get-CacheRegion命令在Windows PowerShell中来查看一下当前缓存集群中所有的分区信息,如下图所示:

    Velocity_002

    同样在检索缓存数据时,仍然可以使用分区名进行检索。

    使用标签

    在Velocity还允许对加入到缓存中的缓存项设置Tag,可以是一个或者多个,使用了Tag,就可以从多个方面对缓存项进行描述,这样在检索数据时,就可以根据Tag来一次检索多个缓存项。为缓存项设置Tag,如下代码所示:

    Cache cache = GetCurrentCache();
    string regionName = "Customers";
    
    Customer customer1 = new Customer()
    {
        ID = "C20081117004",
        FirstName = "Terry",
        LastName = "Lee",
        Age = 25,
        Email = "lhj_cauc[#AT#]163.com"
    };
    Customer customer2 = new Customer()
    {
        ID = "C20081117005",
        FirstName = "Terry",
        LastName = "Lee",
        Age = 25,
        Email = "lhj_cauc[#AT#]163.com"
    };
    
    Tag tag1 = new Tag("Beijing");
    Tag tag2 = new Tag("Tianjin");
    cache.Add(regionName, customer1.ID, customer1, new Tag[] { tag1, tag2 });
    cache.Add(regionName, customer2.ID, customer2, new Tag[] { tag2 });

    这样就可以对设置了Tag的缓存项进行检索,根据实际需求选择使用如下三个方法之一:

    GetAllMatchingTags(string region, Tag[] tags)
    GetAnyMatchingTag(string region, Tag[] tags)
    GetByTag(string region, Tag tag)

    第一个检索匹配所有Tag的数据,第二个检索匹配所有Tag中的任意一个即可,最后只使用一个Tag,如下代码所示:

    string regionName = "Customers";
    Tag[] tags = new Tag[] { new Tag("Beijing"), 
               new Tag("Tianjin")};
    
    List<KeyValuePair<string, object>> result
        = cache.GetAllMatchingTags(regionName, tags);

    使用Tag功能对于检索缓存项提供了极大的灵活性,对于任何一个数据,都可以使用多个Tag从很多方面去描述它。

    ASP.NET SessionState提供者

    Velocity还提供了对于ASP.NET SessionState提供者的支持,可以通过配置把Session信息缓存到缓存集群中,添加Velocity配置区:

    <section name="dcacheClient"
             type="System.Data.Caching.DCacheSection, 
             CacheBaseLibrary, Version=1.0.0.0, 
             Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

    配置缓存客户端信息:

    <dcacheClient>
      <localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
      <hosts>
        <host name="localhost" cachePort="22233" 
              cacheHostName="DistributedCacheService"/>
      </hosts>
    </dcacheClient>

    配置SessionState信息:

    <sessionState mode="Custom" customProvider="Velocity">
      <providers>
        <add name="Velocity" 
             type="System.Data.Caching.SessionStoreProvider,ClientLibrary"
             cacheName="default"/>
      </providers>
    </sessionState>

    需要指定使用哪个命名缓存,但是该功能似乎到目前还存在问题,无法测试通过L

    总结

    本文简单介绍了Velocity的配置模型,以及如何缓存复杂数据类型,对命名缓存分区,为缓存项设置Tag,以及对于ASP.NET SessionState的支持,希望对大家有用。

    原文:http://www.cnblogs.com/Terrylee/archive/2008/11/21/Microsoft-Distributed-Cache-Velocity-Part2.html


  • 相关阅读:
    网页链接在 什么时候 进行跳转到哪里?
    word中那些重要但是被人忽略的快捷键和长word文档的跳转
    如何在editplus中配置ctags?
    winsow xp不能安装软件, 提示"中断" 是因为设置了 软件限制策略
    firefox的plugin-container.exe进程如何关闭?
    thinkphp的url地址区分大小写?
    thinkphp单入口和多入口的访问方法
    什么时候使用tab键来对齐代码和代码的风格
    该不该用inline-block取代float? inline和float的区别?
    cad中关于点样式点的绘制
  • 原文地址:https://www.cnblogs.com/HeroBeast/p/1344738.html
Copyright © 2011-2022 走看看