微软高性能缓存AppFabric(二)使用

原文链接: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();

时间: 2024-08-28 01:53:30

微软高性能缓存AppFabric(二)使用的相关文章

构建高性能服务(二)java高并发锁的3种实现

构建高性能服务(二)java高并发锁的3种实现 来源:http://www.xymyeah.com/?p=46 提高系统并发吞吐能力是构建高性能服务的重点和难点.通常review代码时看到synchronized是我都会想一想,这个地方可不可以优化.使用synchronized使得并发的线程变成顺序执行,对系统并发吞吐能力有极大影响,我的博文 http://maoyidao.iteye.com/blog/1149015 介绍了可以从理论上估算系统并发处理能力的方法. 那么对于必须使用synchr

高性能缓存加速器varnish(概念篇)

高性能缓存加速器varnish(概念篇) 一.varnish简介 varnish是一款高性能的开源HTTP加速器,现在很多门户网站已经部署了varnish,并且反应都很好,甚至反应比squid还稳定,且效率更高,资源暂用更少. 作者Poul-Henning Kamp是FreeBSD的内核开发者之一.Varnish采用全新的软件体系架构,和现在的硬件提交配合紧密.在1975年时,储存媒介只有两种:内存与硬盘.但现在计算 机系统的内存除了主存外,还包括了cpu内的L1.L2,甚至有L3快取.硬盘上也

高性能缓存服务器Varnish架构配置

Varnish跟Squid都是一款内容加速缓存服务器,我们可以使用它们来对我们的网页内容进行缓存,以此来从某个方面提高用户体验度,提升网站整体的抗压能力. 目前自建的CDN中,有很多都是基于Squid.Varnish等相关缓存软件,经过内部的二次开发实现了更好的内容加速及管理. 那今天我们一起来学习一下Varnish简单的搭建及日常的维护,深入的东西后期分享,跟大家一起来交流.这里直接上Shell脚本自动初始化并安装: #!/bin/sh #auto install varnish #2014-

知乎技术分享:从单机到2000万QPS并发的Redis高性能缓存实践之路

本文来自知乎官方技术团队的"知乎技术专栏",感谢原作者陈鹏的无私分享. 1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动化运维服务体系,提供很多强大的功能.本文作者陈鹏是该系统的负责人,本次文章深入介绍了该系统的方方面面,值得互联网后端程序员仔细研究. (本文同步发布于:http://www.52im.net/thread-1968-1-1.html) 2.关于作者 陈鹏:现任知乎存储平台组 Redis 平

读薄《高性能MySql》(二)Schem与数据优化

读薄<高性能MySql>(一)MySql基本知识 读薄<高性能MySql>(二)Schem与数据优化 选择更优的数据类型 当我们设计数据类型的时候应该选择最优的数据类型,因为好的数据类型会使数据库性能提升很多,特别是在使用 ORM 的时候要尤其消息,因为需求的复杂性,ORM 基本上没什么可能会生成最优的类型. 接下来介绍一些通用的数据类型结构. 更小的通常更好 一般情况下,应该尽量使用存储数据最小的数据单类型 尽量使用自带的数据类型 比如保存日期最好用 mysql 内有的日期类型而

Solr4.8.0源码分析(19)之缓存机制(二)

Solr4.8.0源码分析(19)之缓存机制(二) 前文<Solr4.8.0源码分析(18)之缓存机制(一)>介绍了Solr缓存的生命周期,重点介绍了Solr缓存的warn过程.本节将更深入的来介绍下Solr的四种缓存类型,以及两种SolrCache接口实现类. 1.SolrCache接口实现类 前文已经提到SolrCache有两种接口实现类:solr.search.LRUCache 和 solr.search.LRUCache. 那么两者具体有啥区别呢? 1.1 solr.search.LR

[.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能

[.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能 本节导读: 上节说了缓存是以空间来换取时间的技术,介绍了客户端缓存和两种常用服务器缓布,本节主要介绍一种.NET中特别重要的缓布技术Cache.利用Cache提升程序性能. 1. 缓存Cache的命名空间 .NET中对缓存有两个命名空间 命名空间1:System.Web.Caching 命名空间2:System.Runtime.Caching 引用范围:这两个命名空间,都可以在Web和非WEB应用程序中

那些年我们一起追过的缓存写法(二)

引言 感谢园子里的同学对上一篇的支持,很高兴楼主的一些经验及想法能够对大家有一些帮助. 上次主要讨论缓存读写这块各种代码实现.本篇是就上次的问题接着来,继续看那些年我们各种缓存用法. 目录 一:缓存预热 二:多级缓存 2.1 介绍 2.2 线程缓存 2.3 内存缓存 2.4 文件缓存 2.5 分布式缓存 2.6 DB缓存 三:多层缓存 四:总结 一:缓存预热 上次有同学问过.在第一次加载时,我们的缓存都为空,怎么进行预热. 单机Web情况下,一般我们使用RunTimeCache.相对于这种情况下

.net redis数据缓存(二) redis操作List集合带分页

1.编写一个简单的redishelper类库,封装ServiceStack.Redis 1 public class RedisHelper 2 { 3 #region 基本用户名密码,使用配置文件 4 /// <summary> 5 /// 写入redis服务器的ip+port 6 /// </summary> 7 public static string WriteServerList = ConfigurationManager.AppSettings["Write