微软高性能缓存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[];
servers[] = 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();
补充在最后:
看到其他兄弟写的一些Velocity的代码,数据是存储在一些Region 中的。这样在需要删除一部分Key的时候确实很方便。但是今天配置Session过程中注意到一条官方提醒
存储在region中的对象将不会在缓存主机之间负载平衡,但位于创建region的缓存主机中。因此,它通常不是推荐的配置。
仅当特殊要求查找单个主机上的所有会话对象时,才应该使用region。
微软高性能缓存AppFabric(二)使用的更多相关文章
- 微软高性能缓存AppFabric (一) 安装
博客原文链接:http://www.cnblogs.com/Qbit/p/6088703.html AppFabric 缓存功能的前身是VeloCity ,它是基于windows平台的一个高速内存缓存 ...
- 微软分布式缓存 appfabric
appfabric为微软自家产的分布式缓存解决方案,随dotnet4.0一起发布.目前版本为1.1
- 知乎技术分享:从单机到2000万QPS并发的Redis高性能缓存实践之路
本文来自知乎官方技术团队的“知乎技术专栏”,感谢原作者陈鹏的无私分享. 1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动 ...
- 构建高性能服务(二)java高并发锁的3种实现
构建高性能服务(二)java高并发锁的3种实现 来源:http://www.xymyeah.com/?p=46 提高系统并发吞吐能力是构建高性能服务的重点和难点.通常review代码时看到sync ...
- [.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能
[.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能 本节导读: 上节说了缓存是以空间来换取时间的技术,介绍了客户端缓存和两种常用服务器缓布,本节主要介绍一种. ...
- Solr4.8.0源码分析(19)之缓存机制(二)
Solr4.8.0源码分析(19)之缓存机制(二) 前文<Solr4.8.0源码分析(18)之缓存机制(一)>介绍了Solr缓存的生命周期,重点介绍了Solr缓存的warn过程.本节将更深 ...
- java 手写 jvm高性能缓存
java 手写 jvm高性能缓存,键值对存储,队列存储,存储超时设置 缓存接口 package com.ws.commons.cache; import java.util.function.Func ...
- [译]高性能缓存库Caffeine介绍及实践
概览 本文我们将介绍Caffeine-一个Java高性能缓存库.缓存和Map之间的一个根本区别是缓存会将储存的元素逐出.逐出策略决定了在什么时间应该删除哪些对象,逐出策略直接影响缓存的命中率,这是缓存 ...
- 使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)
概述 Velocity是微软推出的分布式缓存解决方案,为开发可扩展性,可用的,高性能的应用程提供支持,可以缓存各种类型的数据,如CLR对象. XML.二进制数据等,并且支持集群模式的缓存服务器.Vel ...
随机推荐
- iTop4412 裸机开发 LED
平台:iTop4412 SCP 2G 开发板有两个可控制的LED灯:分别接到引脚:KP_COL0和VDD50_EN KP_COL0就是GPL2_0 VDD50_EN就是GPK1_1 配置GPX_CON ...
- [Windows]获取系统版本号
1 string GetMainProgInfo() 2 { 3 string strRet; 4 TCHAR szPath[MAX_PATH]; 5 GetModuleFileName(NULL,s ...
- Cogs 465. 挤牛奶
465. 挤牛奶 ★ 输入文件:milk2.in 输出文件:milk2.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 三个农民每天清晨5点起床,然后去牛棚给3 ...
- 浅析HTTP协议get请求与post请求
一.HTTP简介 1. HTTP协议:(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则(报文格式). 2.报文: ...
- 20165224 陆艺杰 Exp7 网络欺诈防范
通常在什么场景下容易受到DNS spoof攻击 一样的局域网环境下 在日常生活工作中如何防范以上两攻击方法 使用攻击检查工具 简单应用SET工具建立冒名网站 打开 apache服务 apache 是 ...
- Leetcode初级算法(链表篇)
删除链表的倒数第N个节点 感觉自己对于链表的知识还是了解的不够深入,所以没有想到用双指针进行操作.我的想法是这样的,首先计算整个链表的长度,然后遍历到长度减去n的节点处,执行删除操作. 自己的代码: ...
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- ResourceBundle 读取properties文件中文乱码
1.确认properties文件是什么编码格式,并确认文件在该格式下中文是正常显示的2.读取时候,进行转一层,先用ISO-8859-1读取字节流,然后根据properties的文件格式进行new St ...
- Python+Selenium----处理登录图片验证码
1.说明 在做自动化测试的时候,经常会遇到登录,其中比较麻烦的就是验证码的处理,现在比较常用的图形验证码,每次刷新,得到的验证码不一致,所以,一般来说,获取验证码图片有两种方式: (1)拿到验证码的图 ...
- Sandcastle Help File Builder 生成NET帮助文档
Sandcastle是微软提供的一个根据XML注释和DLL文件生成帮助文件的工具,目前是在CodePlex上的一个开源项目,可以去这里下载:Sandcastle Sandcastle生成的输出结果具有 ...