.Net 自带有cache 用法也不详细讲了 不要ASP.NET去操作 IIS 创建的cache 在数据量太多的情况下 即使在厉害的人物,在页面处理大数据量都不会有太好的办法
简单说,刷新所有用户的缓存 页面读数据 换 cache 效率可想而知
这里 我先贴出一个基类 VB.NET C#都有 呵呵
C#版本
string[] servers = { "127.0.0.1:11211", "127.0.0.1:11211" };
SockIOPool pool;
MemcachedClient mc;
public Memcached()
{
//初始化池
pool = SockIOPool.GetInstance();
pool.SetServers(servers);
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 1000;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
mc = new MemcachedClient();
mc.EnableCompression = false;
}
public void Remove(string key)
{
mc.Delete(key);
}
public bool Set(string key, object value)
{
return mc.Set(key, value);
}
public bool Set(string key, object value, int minute)
{
return mc.Set(key, value, DateTime.Now.AddMinutes(minute));
}
public Hashtable Stats()
{
return mc.Stats();
}
public object Get(string key)
{
return mc.Get(key);
}
public bool ContainsKey(string key)
{
return mc.KeyExists(key);
}
VB.NET
代码
Dim
servers()
As
String
=
{
"
127.0.0.1:11211
"
,
"
127.0.0.1:11211
"
}
Dim pool As SockIOPool
Dim mc As New MemcachedClient
Public Function Memcached() As Integer
' 初始化池
pool = SockIOPool.GetInstance()
pool.SetServers(servers)
pool.InitConnections = 3
pool.MinConnections = 3
pool.MaxConnections = 1000
pool.SocketConnectTimeout = 1000
pool.SocketTimeout = 3000
pool.MaintenanceSleep = 30
pool.Failover = True
pool.Nagle = False
pool.Initialize()
mc = New MemcachedClient()
mc.EnableCompression = False
End Function
Public Sub RemoveMEM( ByVal key As String )
mc.Delete(key)
End Sub
Public Function SetMEM( ByVal key As String , ByVal value As Object ) As Boolean
Return mc.Set(key, value)
End Function
Public Function SetMEM( ByVal key As String , ByVal value As Object , ByVal minute As Integer ) As Boolean
Return mc.Set(key, value, DateTime.Now.AddMinutes( minute ))
End Function
Public Function StatsMEM() As Hashtable
Return mc.Stats()
End Function
Public Function GetMEM( ByVal key As String ) As Object
Return mc.Get(key)
End Function
Public Function ContainsKey( ByVal key As String ) As Boolean
Return mc.KeyExists(key)
End Function
Dim pool As SockIOPool
Dim mc As New MemcachedClient
Public Function Memcached() As Integer
' 初始化池
pool = SockIOPool.GetInstance()
pool.SetServers(servers)
pool.InitConnections = 3
pool.MinConnections = 3
pool.MaxConnections = 1000
pool.SocketConnectTimeout = 1000
pool.SocketTimeout = 3000
pool.MaintenanceSleep = 30
pool.Failover = True
pool.Nagle = False
pool.Initialize()
mc = New MemcachedClient()
mc.EnableCompression = False
End Function
Public Sub RemoveMEM( ByVal key As String )
mc.Delete(key)
End Sub
Public Function SetMEM( ByVal key As String , ByVal value As Object ) As Boolean
Return mc.Set(key, value)
End Function
Public Function SetMEM( ByVal key As String , ByVal value As Object , ByVal minute As Integer ) As Boolean
Return mc.Set(key, value, DateTime.Now.AddMinutes( minute ))
End Function
Public Function StatsMEM() As Hashtable
Return mc.Stats()
End Function
Public Function GetMEM( ByVal key As String ) As Object
Return mc.Get(key)
End Function
Public Function ContainsKey( ByVal key As String ) As Boolean
Return mc.KeyExists(key)
End Function
实际应用中 考虑到服务器重复使用的问题
s1_u999_dt_uMain
所有的key必须有 s1 类似开头,表明数据来自那个服务器 这样在一台服务器上多服公用就能很好的解决了
u999 就是用户编号
上例 来自 1服 编号999 用户的主表信息
s1_u999_str_bag