昨天在codeplex.com上闲逛,偶遇到个好东西,提来给大家拜年。
-----------------------------------------------------------------------------------------
一、简介
出处:http://wuchang.cnblogs.com
indeXus.Net SharedCache是高性能的、分布式的内存对象系统,用于在动态WEB或Win应用程序中减少数据库的负责,提高访问速度。
SharedCache 全部的代码都是用c#写的,100% DotNet原生态。
先来看一下SharedCache 的几种方式:
1.Distributed Caching - partitioned
2.Replicated Caching
3.Single Instance Caching
二、小试牛刀
1.服务端
全部源码可以在SharedCache项目网站(http://www.codeplex.com/SharedCache)下载。
方案中中有7个项目
可以直接在vs2008 中打开编译。
但压缩包中缺少ICSharpCode.SharpZipLib.dll,编译前䉣准备好这个。
编译成功后MergeSystem.Indexus.WinService\bin\Debug目录的文件
job_install.bat / job_uninstall.bat 安装/卸载windows服务
job_startService.bat / job_stopService.bat 开启/停止服务
job_Console.bat 在控制台中加载服务。
如果不想安装到Windows服务中,直接用Job_Console.bat启动即可,默认监听端口是48888(在MergeSystem.Indexus.WinService.vshost.exe.config中配置),
2.测试程序
新建一个控制台程序,添加MergeSystem.Indexus.WinServiceCommon的引用。测试代码如下:
using MergeSystem.Indexus.WinServiceCommon.Provider.Cache;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
User[] data = new [] { new User{ Name = " user1 " ,Age = 20 },
new User{ Name = " user2 " ,Age = 1 },
new User{ Name = " user3 " ,Age = 22 }
};
foreach (var item in data)
{
IndexusDistributionCache.SharedCache.Add(item.Name, item);
}
Console.WriteLine( " count={0} " , IndexusDistributionCache.SharedCache.Count);
User user = IndexusDistributionCache.SharedCache.Get < User > ( " user2 " );
Console.WriteLine( " name={0},age={1} " , user.Name, user.Age);
Console.WriteLine( " press any key to exit. " );
Console.ReadKey();
}
}
[Serializable]
public class User
{
public string Name { get ; set ; }
public int Age { get ; set ; }
}
}
App.config中配置
< configuration >
< configSections >
< section name ="indexusNetSharedCache" type ="MergeSystem.Indexus.WinServiceCommon.Configuration.Client.IndexusProviderSection, MergeSystem.Indexus.WinServiceCommon" />
</ configSections >
< indexusNetSharedCache defaultProvider ="IndexusSharedCacheProvider" >
< servers >
< add key ="SrvZh03" ipaddress ="127.0.0.1" port ="48888" />
</ servers >
< providers >
< add
name ="IndexusSharedCacheProvider"
type ="MergeSystem.Indexus.WinServiceCommon.Provider.Cache.IndexusSharedCacheProvider, MergeSystem.Indexus.WinServiceCommon"
>
</ add >
</ providers >
</ indexusNetSharedCache >
</ configuration >
测试程序运行结果
服务端响应:
要注意的是,缓存的对象必需是可序列化的(Serializable)
出处:wuchang.cnblogs.com