我们知道现在基于net 4.0的缓存可以使用Windows Server AppFabric,今天来介绍一下。首先我的环境是Win7 Enterprise,安装II7相关组件。下载安装包,或通过
Web Platform Installer 进行安装也可以了。
接下来是配置Windows Server AppFabric Cache,有一个向导也很简单,此处我们选择默认的SQL Server AppFabric Caching Service Configuration Store Provider配置
接着会让你创建数据库或注册现有的数据库,下面是配制端口:
最后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。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)