Memcached——非关系型数据库分布式处理

时间:2022-05-22 18:03:12

Memcached登录校验应用:

Memcached——非关系型数据库分布式处理

MMCacheWriter.cs类

using Memcached.ClientLibrary;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Common
{
public class MMCacheWriter : ICacheWriter
{
public static readonly MemcachedClient MemcachedClient;
static MMCacheWriter()
{
//分布Memcachedf服务IP 端口
string[] servers = ConfigurationManager.AppSettings["memcachedServer"].Split(',');
if (servers==null)
{
throw new Exception("请选择正确的配置");
}
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
pool.SetServers(servers);
pool.InitConnections = ;
pool.MinConnections = ;
pool.MaxConnections = ;
pool.SocketConnectTimeout = ;
pool.SocketTimeout = ;
pool.MaintenanceSleep = ;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
//客户端实例
MemcachedClient = new Memcached.ClientLibrary.MemcachedClient();
MemcachedClient.EnableCompression = false; }
public void Set(string key, object value, DateTime exp)
{
MemcachedClient.Set(key, value, exp);
} public void Set(string key, object value)
{
MemcachedClient.Set(key, value);
} public object Get(string key)
{
return MemcachedClient.Get(key);
}
}
}

CacheHelper.cs类

using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Common
{
public class CacheHelper
{
public static ICacheWriter CacheWriter { get; set; }
/// <summary>
/// 解决实例化对象问题
/// </summary>
static CacheHelper()
{
IApplicationContext ctx = ContextRegistry.GetContext();
var userInfoDal = ctx.GetObject("CacheHelper") as CacheHelper;
}
public static void Set(string key, object value, DateTime exp)
{
CacheWriter.Set(key, value, exp);
}
public static void Set(string key, object value)
{
CacheWriter.Set(key, value);
}
public static object Get(string key)
{
return CacheWriter.Get(key);
}
}
}

HttpRunTimeCacheWriter.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace Common
{
public class HttpRunTimeCacheWriter : ICacheWriter
{ void ICacheWriter.Set(string key, object value, DateTime exp)
{
HttpRuntime.Cache.Insert(key, value, null, exp, TimeSpan.Zero);
} void ICacheWriter.Set(string key, object value)
{
HttpRuntime.Cache.Insert(key, value);
} object ICacheWriter.Get(string key)
{
return HttpRuntime.Cache[key];
}
}
}

ICacheWriter.cs接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Common
{
public interface ICacheWriter
{
void Set(string key, object value, DateTime exp);
void Set(string key, object value);
object Get(string key);
}
}