.Net 内存缓存项目 SharpCached源码
测试代码:
using System;
using System.Data;
using SharpClient;
using System.Diagnostics;
namespace YesHJ.Search.Service
{
public partial class Debug : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SharpClient.SharpCachedAPI api = new SharpClient.SharpCachedAPI();
if (api.IsError)
{
Response.Write("Error");
Response.End();
}
else
{
Response.Write("时钟支持高精度:" + Stopwatch.IsHighResolution.ToString() + "<br/>");
Response.Write("时钟最小单位:" + Stopwatch.GetTimestamp().ToString() + "<br/>");
Response.Write("每秒计时频率:" + Stopwatch.Frequency.ToString() + "<br/><br/><br/>");
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
int count = api.Count();
watch.Stop();
Response.Write("获取键值数运行时间:" + watch.ElapsedMilliseconds + "<br/>");
Response.Write("keys count :" + count + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
double memory = api.Memory();
watch.Stop();
Response.Write("获取占用内存运行时间:" + watch.ElapsedMilliseconds + "<br/>");
Response.Write("Memory :" + memory.ToString("f3") + "M<br/><hr/>");
MyValueObject obj = new MyValueObject() ;
obj.ID = 100;
obj.Title = "1234567890123456789012345678901234567890123456789012345678901234567890";
obj.Content = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
api.Set("xp_mykey_MyValueObject", SharpClient.SharpCachedAPI.Serialize<MyValueObject>(obj));
watch.Stop();
Response.Write("写入自定义对象运行时间:" + watch.ElapsedMilliseconds + "<br/><hr/>");
DataTable dt = new DataTable("CachedTable");
dt.Columns.Add("ID");
dt.Columns.Add("title");
dt.Columns.Add("Content");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i * 1000;
dr["title"] = "1234567890123456789012345678901234567890123456789012345678901234567890";
dr["Content"] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
dt.Rows.Add(dr);
}
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
api.Set("xp_mykey_DataTable", SharpClient.SharpCachedAPI.Serialize<DataTable>(dt));
watch.Stop();
Response.Write("写入DataTable对象运行时间:" + watch.ElapsedMilliseconds + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
api.Set("xp_mykey_string", "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
watch.Stop();
Response.Write("写入字符串运行时间:" + watch.ElapsedMilliseconds + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
api.Set("xp_mykey_string_short", "abcdefg");
watch.Stop();
Response.Write("写入短字符串运行时间:" + watch.ElapsedMilliseconds + "<br/><hr/>");
bool isExist = false;
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_MyValueObject");
if (isExist)
{
MyValueObject getobj = SharpCachedAPI.Deserialize<MyValueObject>(api.Get("xp_mykey_MyValueObject"));
}
watch.Stop();
Response.Write("读取自定义对象运行时间:" + watch.ElapsedMilliseconds + "\t存在:" + isExist.ToString() + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_DataTable");
if (isExist)
{
DataTable getdt = SharpCachedAPI.Deserialize<DataTable>(api.Get("xp_mykey_DataTable"));
}
watch.Stop();
Response.Write("读取DataTable运行时间:" + watch.ElapsedMilliseconds + "\t存在:" + isExist.ToString() + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_string");
if (isExist)
{
string getstr = api.Get("xp_mykey_string");
}
watch.Stop();
Response.Write("读取字符串运行时间:" + watch.ElapsedMilliseconds + "\t存在:" + isExist.ToString() + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_string_short");
if (isExist)
{
string getstrshort = api.Get("xp_mykey_string_short");
}
watch.Stop();
Response.Write("读取短字符串运行时间:" + watch.ElapsedMilliseconds + "\t存在:" + isExist.ToString() + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_none");
if (isExist)
{
string getstrshort = api.Get("xp_mykey_none");
}
watch.Stop();
Response.Write("读取不存在键值运行时间:" + watch.ElapsedMilliseconds + "\t存在:" + isExist.ToString() + "<br/><hr/>");
watch = Stopwatch.StartNew();
watch.Reset();
watch.Start();
isExist = api.Exist("xp_mykey_MyValueObject");
if (isExist)
api.Remove("xp_mykey_MyValueObject");
isExist = api.Exist("xp_mykey_DataTable");
if (isExist)
api.Remove("xp_mykey_DataTable");
isExist = api.Exist("xp_mykey_string");
if (isExist)
api.Remove("xp_mykey_string");
isExist = api.Exist("xp_mykey_string_short");
if (isExist)
api.Remove("xp_mykey_string_short");
isExist = api.Exist("xp_mykey_none");
if (isExist)
api.Remove("xp_mykey_none");
watch.Stop();
Response.Write("移除上述键值运行时间:" + watch.ElapsedMilliseconds + "<br/><hr/>");
Response.End();
}
}
}
[Serializable]
public class MyValueObject
{
public int ID;
public string Title;
public string Content;
}
}
测试结果:
时钟支持高精度:True
时钟最小单位:188778184698140
每秒计时频率:2327540000
获取键值数运行时间:0
keys count :49523
获取占用内存运行时间:0
Memory :241.000M
写入自定义对象运行时间:4
写入DataTable对象运行时间:4
写入字符串运行时间:3
写入短字符串运行时间:3
读取自定义对象运行时间:0 存在:True
读取DataTable运行时间:1 存在:True
读取字符串运行时间:0 存在:True
读取短字符串运行时间:0 存在:True
读取不存在键值运行时间:0 存在:False
移除上述键值运行时间:28
结果基本很稳定。测试平台:Window 2003 ,2.33G 8核CPU,4G内存。本机测试,没有放到局域网测试。
/* SharpCached 是一个内存缓存工具
* by yurow @ http://birdshover.cnblogs.com
* 2008-7-29 created
*/
SharpCached V0.6
下载地址:https://files.cnblogs.com/birdshover/SharpCached.zip
有兴趣的朋友,可以和我一起研究,呵呵,不完善的地方期望大家能不吝指正。 本来想在CodePlex开个开源项目的,但是人太笨,用的SvnBridge一直传不上去,郁闷得不弄了,呵呵。
SharpCached是完全用C#开发的,基于.Net Framework 2.0运行的内存缓存服务,Visual Studio 2008项目。
一、实现功能
实现IIS进程外缓存功能,通过Remoting调用。
实现了未命中或者近段时间内未命中结果过期。
实现了限制使用最大内存(程序检测出的和Windows任务管理器不一致,任务管理器中看到使用内存要多一些。)
二、使用服务
编译后文件清单
SharpCached.exe
ISharpCarrier.dll
SharpCached.exe.config
SharpCached.exe.config配置文件说明
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- 服务端口 -->
<add key="Server_Port" value="8000"/>
<!-- 最大使用内存,单位M-->
<add key="Max_Memory" value="1024"/>
<!-- 存取Key初始数值 -->
<add key="Keys_InitCount" value="5000"/>
<!-- 未命中过期-->
<add key="TimeOut" value="10"/>
<!-- 服务名-->
<add key="Server_Name" value="SharpCached"/>
</appSettings>
</configuration>
注册:sc create "SharpCached" type= interact type= own start= auto binPath= "%cd%"SharpCached.exe"
启动:net start "SharpCached"
三、调用方法
客户端配置文件:
如果是Windows应用程序,把
<appSettings>
<!-- 服务器地址 -->
<add key="SharpServer" value="tcp://localhost:8000/SharpCached"/>
<!-- 连接超时时间 -->
<add key="TimeOut" value="1000"/>
</appSettings>
代码加入app.setting中
如果是Web程序,把以上代码加入Web.Config中。
项目引用SharpClient项目和ISharpCarrier项目
示例代码:
DataTable GetTable()
{
SharpCachedAPI api = new SharpCachedAPI();
string key = "key_123";
if (api.IsError)
{
return new DataTable();
}
else
{
if (api.Exist(key))
{
return SharpCachedAPI.Deserialize<DataTable>(api.Get(key));
}
else
{
DataTable dt = new DataTable();
string value = SharpCachedAPI.Serialize<DataTable>(dt);
api.Set(key, value);
return dt;
}
}
}



浙公网安备 33010602011771号