Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Collections;
Using System.Reflection;
Using Memcached.clientlibrary;
Using System.Text.RegularExpressions;
Using System.Configuration;
Using System.Threading;
Using Mopon.ZY.Data;
Namespace Mopon.caching
{
public sealed class Memcachemanager:icachemanager
{
Private ReaderWriterLockSlim Cachelock = new ReaderWriterLockSlim ();
static Memcachedclient memcached = new Memcachedclient ();
static hashset<string> Cachekeys = new hashset<string> ();
Static Memcachemanager ()
{
Memcached. EnableCompression = false;
}
Public Memcachemanager () {}
Public T get<t> (string key)
{
if (memcache.isenable)
{
Try
{
Return (T) memcached. Get (key);
}
catch (Exception ex)
{
Errorlog.write ("memcachemanager.get<t> (" + key + ")", "[-9113] Cache service _ Get Cache Item Exception", ex);
}
}
return default (T);
}
public void Set (string key, object data, int cacheTime)
{
if (memcache.isenable)
{
Try
{
if (memcached. Set (key, data, DateTime.Now.AddMinutes (cacheTime)))
{
Cachelock.enterwritelock ();
Try
{
Cachekeys.add (key);
}
Finally
{
Cachelock.exitwritelock ();
}
}
Else
{
Errorlog.write ("memcachemanager.set<t> (" + key + "), [-9113] Cache service _ failed to set cache item.");
}
}
catch (Exception ex)
{
Errorlog.write ("memcachemanager.set<t> (" + key + ")", "[-9113] Cache service _ Set cache item Failed", ex);
}
}
}
public bool IsSet (string key)
{
if (memcache.isenable)
{
Try
{
Return memcached. Keyexists (key);
}
catch (Exception ex)
{
Errorlog.write ("Memcachemanager.isset (" + key + ")", "[-9113] Cache service _ Get Cache Item Exception", ex);
}
}
return false;
}
public void Remove (string key)
{
if (memcache.isenable)
{
Try
{
if (memcached. Delete (Key))
{
Cachelock.enterwritelock ();
Try
{
Cachekeys.remove (key);
}
Finally
{
Cachelock.exitwritelock ();
}
}
}
catch (Exception ex)
{
Errorlog.write ("Memcachemanager.isset (" + key + ")", "[-9113] Cache Service _ Remove Cache Item Exception", ex);
}
}
}
public void Removebypattern (string pattern)
{
var regex = new Regex (Pattern, Regexoptions.singleline | regexoptions.compiled | Regexoptions.ignorecase);
var keystoremove = new list<string> ();
foreach (var item in Cachekeys)
if (regex. IsMatch (item))
Keystoremove.add (item);
foreach (string key in Keystoremove)
{
Remove (key);
}
}
public void Clear ()
{
if (memcache.isenable)
{
Try
{
Memcached. Flushall ();
Cachelock.enterwritelock ();
Try
{
Cachekeys.clear ();
}
Finally
{
Cachelock.exitwritelock ();
}
}
catch (Exception ex)
{
Errorlog.write ("Memcachemanager.clear ()", "[-9113] Cache service _ empty cache for all items" exception occurred ", ex);
}
}
}
Static Classes--static constructors are initialized globally
private Static Class Memcache
{
Static Memcache ()
{
var memrespository = new linqrepository<memcachedconfig> ();
var Listresult = Memrespository.findall (item = Item. IsEnabled?? FALSE);
If (Listresult.count () > 0)
{
var serverlist = string. Join (",", listresult.select (CTX = CTX). serverlist). ToArray ()). Trim (', '); The
if (string. IsNullOrEmpty (serverlist))
{
Errorlog.write ("[-9110] Cache service _ Initialize cache service connection parameter [cache server address] is empty");
}
Else
{
Try
{
//Initialize pool
Errorlog.write ("Initialize cache server connection starts. Server address list:" + serverlist.tostring ());
Sockiopool pool = sockiopool.getinstance ();
Pool. Setservers (Serverlist.split (', '));
Pool. Initconnections = 5;
Pool. Minconnections = 5;
Pool. MaxConnections = 500;
Pool. socketconnecttimeout = 1000;
Pool. Sockettimeout = 3000;
Pool. Maintenancesleep = 30;
Pool. Failover = true;
Pool. Nagle = false;
Pool. Initialize ();
//If the connection cache server fails, the successor is no longer connected
var Fieldpool = typeof (Sockiopool). GetField ("_availpool", BindingFlags.Instance | BindingFlags.NonPublic);
var availpool = Fieldpool.getvalue (pool) as Hashtable;
Isenable = availpool! = null && availpool.count > 0;
Errorlog.write ("Initialize cache server connection end");
if (! isenable) Shutdown ();
}
catch (Exception ex)
{
Errorlog.write ("Memcachemanage.connetservers ()", "[-9111] Cache service _ Initialize cache service Connection exception", ex);
}
}
}
}
public static void Shutdown ()
{
Sockiopool.getinstance (). Shutdown ();
Errorlog.write ("Memcachemanage.colseserver ()", "Close cache service Time:" + DateTime.Now.ToString (), NULL);
}
public static Boolean isenable {get; private set;}
}
}
}
How caching classes are written