The distributed cache has its own benefits.
The following constvalue.sessionid is a constant
public static readonly String sessionId = "SessionId";
The Expiration time. Automatically 0 with less than 0
public static readonly int sessionexpressminute = 20;
Here is a tool class I wrote. Basically implement a session-like distributed cache
Need to reference some assemblies, here I do not write more, there is a problem in the following message on it.
Class name Memcachehelper
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.InteropServices;
Using System.Text;
Using System.Web.ModelBinding;
Using Memcached.clientlibrary;
Using System.Web;
Namespace CZBK.ItcastOA.Common
{
public class Memcachehelper
{
private static readonly memcachedclient MC = null;
Static Memcachehelper ()
{
Connection pool
String[] ServerList = {"127.0.0.1:11211", "10.0.0.132:11211"};
Initialize Pool
Sockiopool pool = sockiopool.getinstance ();
Pool. Setservers (serverlist);
Pool. Initconnections = 3;
Pool. Minconnections = 3;
Pool. MaxConnections = 5;
Pool. socketconnecttimeout = 1000;
Pool. Sockettimeout = 3000;
Pool. Maintenancesleep = 30;
Pool. Failover = true;
Pool. Nagle = false;
Pool. Initialize ();
Get the Client instance
MC = new Memcachedclient ();
Mc. EnableCompression = false;
}
public static bool Set (string key, Object value)
{
return MC. Set (key, value);
}
public static bool Set (string key, object value, DateTime time)
{
return MC. Set (key, value, time);
}
<summary>
Get Data
</summary>
<param name= "Key" ></param>
<returns></returns>
public static object Get (String key)
{
return MC. Get (key);
}
<summary>
Delete
</summary>
<param name= "Key" ></param>
public static bool Delete (string key)
{
if (MC. Keyexists (Key))
{
return MC. Delete (key);
}
return false;
}
<summary>
The way to imitate the session
</summary>
<typeparam name= "T" > the type that needs to be deserialized </typeparam>
<param name= "key" > Keys </param>
<param name= "date" > Relative expiration time, whichever is minutes </param>
<returns> returns an object that has been deserialized, as can be transformed directly </returns>
public static object Sessionget<t> (string key, int minute =20) where T:class, new ()
{//Retrieving values from the cache
Object obj = mc. Get (key);
if (obj!= null && MC. Set (key, obj, DateTime.Now.AddMinutes (minute)))
{
return serializehelper.deserializetoobject<t> (obj. ToString ());
}
Mc. Delete (key);
return null;
;
}
<summary>
To determine the write cache
</summary>
<param name= "value" > Keys </param>
<param name= "Context" > Contextual </param>
<param name= "Minute" > Expiry time defaults to 20</param>
<returns> is successful </returns>
public static bool Sessionset (object value,httpcontext context,int minute = 20)
{
Set Default Expiration Time
if (minute <= 0)
{
minute = Constvalue.sessionexpressminute;
}
Value = serializehelper.serializetostring (value);
String key = Guid.NewGuid (). ToString ();
if (MC. Set (key, value, minute))
{
Context. Response.cookies[constvalue.sessionid]. Value = key;
return true;
}
return false;
}
<summary>
///
</summary>
<param name= "value" > Pass over an entity type </param>
<param name= "Response" > Response body </param>
<param name= "Minute" > Expiry time defaults to 20</param>
<returns></returns>
public static bool Sessionset (object value, httpresponsebase response, int minute = 20)
{
Set Default Expiration Time
if (minute <= 0)
{
minute = Constvalue.sessionexpressminute;
}
Value = serializehelper.serializetostring (value);
String key = Guid.NewGuid (). ToString ();
if (MC. Set (key, value))
{
Response. Cookies[constvalue.sessionid]. Value = key;
return true;
}
return false;
}
}
}
About the tool classes used by memcache