public class BaseCache : IDisposable
{
protected string def_ip = string.Empty;
protected int def_port = 0;
protected string def_password = string.Empty;
public BaseCache()
{
}
public virtual void InitCache(string ip = "", int port = 0, string password = "")
{
}
public virtual bool SetCache<T>(string key, T t, int timeOutMinute = 10) where T : class,new()
{
return false;
}
public virtual T GetCache<T>(string key) where T : class,new()
{
return default(T);
}
public virtual bool Remove(string key)
{
return false;
}
public virtual bool FlushAll()
{
return false;
}
public virtual bool Any(string key)
{
return false;
}
public virtual void Dispose(bool isfalse)
{
if (isfalse)
{
}
}
//手动释放
public void Dispose()
{
this.Dispose(true);
//不自动释放
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Redis缓存
/// </summary>
public class RedisCache : BaseCache
{
public RedisClient redis = null;
public RedisCache()
{
//这里去读取默认配置文件数据
def_ip = "172.0.0.1";
def_port = 6379;
def_password = "";
}
#region Redis缓存
public override void InitCache(string ip = "", int port = 0, string password = "")
{
if (redis == null)
{
ip = string.IsNullOrEmpty(ip) ? def_ip : ip;
port = port == 0 ? def_port : port;
password = string.IsNullOrEmpty(password) ? def_password : password;
redis = new RedisClient(ip, port, password);
}
}
public override bool SetCache<T>(string key, T t, int timeOutMinute = 10)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
isfalse = redis.Set<T>(key, t, TimeSpan.FromMinutes(timeOutMinute));
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override T GetCache<T>(string key)
{
var t = default(T);
try
{
if (string.IsNullOrEmpty(key)) { return t; }
InitCache();
t = redis.Get<T>(key);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return t;
}
public override bool Remove(string key)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
isfalse = redis.Remove(key);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override void Dispose(bool isfalse)
{
if (isfalse && redis != null)
{
redis.Dispose();
redis = null;
}
}
#endregion
}
/// <summary>
/// Memcached缓存
/// </summary>
public class MemcachedCache : BaseCache
{
}
public RedisClient(); public RedisClient(RedisEndpoint config); public RedisClient(string host); public RedisClient(Uri uri); public RedisClient(string host, int port); public RedisClient(string host, int port, string password = null, long db = 0);
public enum CacheType
{
RedisCache,
MemcachedCache
}
public static BaseCache Current(CacheType cacheType = CacheType.RedisCache)
{
var nspace = typeof(BaseCache);
var fullName = nspace.FullName;
var nowspace = fullName.Substring(0, fullName.LastIndexOf('.') + 1);
return Assembly.GetExecutingAssembly().CreateInstance(nowspace + cacheType.ToString(), true) as BaseCache;
}
#region CacheRepository 缓存工厂(默认存储Session中)
/// <summary>
/// 缓存枚举
/// </summary>
public enum CacheType
{
BaseCache,
RedisCache,
MemcachedCache
}
/// <summary>
/// 缓存工厂(默认存储Session中)
/// </summary>
public class CacheRepository
{
/// <summary>
/// 缓存工厂(默认存储Session中, CacheKey = "SeesionKey")
/// </summary>
/// <param name="cacheType">缓存类型</param>
/// <returns></returns>
public static BaseCache Current(CacheType cacheType = CacheType.RedisCache)
{
var nspace = typeof(BaseCache);
var fullName = nspace.FullName;
var nowspace = fullName.Substring(0, fullName.LastIndexOf('.') + 1);
return Assembly.GetExecutingAssembly().CreateInstance(nowspace + cacheType.ToString(), true) as BaseCache;
}
}
/// <summary>
/// 缓存基类(默认存储Session中)
/// </summary>
public class BaseCache : IDisposable
{
protected string def_ip = string.Empty;
protected int def_port = 0;
protected string def_password = string.Empty;
protected string CacheKey = "SeesionKey";
public BaseCache()
{
}
/// <summary>
/// 获取自定义SessionId值
/// </summary>
/// <param name="key">key:使用唯一的登陆账号</param>
/// <returns>hash值的SessionId</returns>
public virtual string GetSessionId(string key)
{
return Md5Extend.GetSidMd5Hash(key);
}
public virtual void InitCache(bool isReadAndWriter = true, string ip = "", int port = 0, string password = "")
{
}
public virtual bool SetCache<T>(string key, T t, int timeOutMinute = 10, bool isSerilize = false) where T : class,new()
{
var isfalse = false;
try
{
key = key ?? CacheKey;
if (t == null) { return isfalse; }
var session_json = JsonConvert.SerializeObject(t);
HttpContext.Current.Session.Timeout = timeOutMinute;
HttpContext.Current.Session.Add(key, session_json);
isfalse = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return isfalse;
}
public virtual T GetCache<T>(string key = null, bool isSerilize = false) where T : class,new()
{
var t = default(T);
try
{
key = key ?? CacheKey;
var session = HttpContext.Current.Session[key];
if (session == null) { return t; }
t = JsonConvert.DeserializeObject<T>(session.ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return t;
}
public virtual bool Remove(string key = null)
{
var isfalse = false;
try
{
key = key ?? CacheKey;
HttpContext.Current.Session.Remove(key);
isfalse = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return isfalse;
}
/// <summary>
/// 增加缓存时间
/// </summary>
/// <returns></returns>
public virtual bool AddExpire(string key, int nTimeMinute = 10)
{
return true;
}
public virtual bool FlushAll()
{
return false;
}
public virtual bool Any(string key)
{
return false;
}
public virtual bool SetHashCache<T>(string hashId, string key, T t, int nTimeMinute = 10) where T : class,new()
{
return false;
}
public virtual List<string> GetHashKeys(string hashId)
{
return null;
}
public virtual List<string> GetHashValues(string hashId)
{
return null;
}
public virtual T GetHashValue<T>(string hashId, string key) where T : class,new()
{
var t = default(T);
return t;
}
public virtual bool RemoveHashByKey(string hashId, string key)
{
return false;
}
public virtual void Dispose(bool isfalse)
{
if (isfalse)
{
}
}
//手动释放
public void Dispose()
{
this.Dispose(true);
//不自动释放
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Redis缓存
/// </summary>
public class RedisCache : BaseCache
{
public IRedisClient redis = null;
public RedisCache()
{
//这里去读取默认配置文件数据
def_ip = "127.0.0.1";
def_port = 6379;
def_password = "";
}
#region Redis缓存
public static object _lockCache = new object();
public override void InitCache(bool isReadAndWriter = true, string ip = "", int port = 0, string password = "")
{
if (redis == null)
{
ip = string.IsNullOrEmpty(ip) ? def_ip : ip;
port = port == 0 ? def_port : port;
password = string.IsNullOrEmpty(password) ? def_password : password;
//单个redis服务
//redis = new RedisClient(ip, port, password);
//集群服务 如果密码,格式如:pwd@ip:port
var readAndWritePorts = new List<string> { "shenniubuxing3@127.0.0.1:6379" };
var onlyReadPorts = new List<string> {
"shenniubuxing3@127.0.0.1:6378",
"shenniubuxing3@127.0.0.1:6377"
};
var redisPool = new PooledRedisClientManager(
readAndWritePorts,
onlyReadPorts,
new RedisClientManagerConfig
{
AutoStart = true,
//最大读取链接
MaxReadPoolSize = 20,
//最大写入链接
MaxWritePoolSize = 10
})
{
//每个链接超时时间
ConnectTimeout = 20,
//连接池超时时间
PoolTimeout = 60
};
lock (_lockCache)
{
redis = isReadAndWriter ? redisPool.GetClient() : redisPool.GetReadOnlyClient();
}
}
}
public override bool AddExpire(string key, int nTimeMinute = 10)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
//isfalse = redis.ExpireEntryIn(key, TimeSpan.FromMinutes(nTimeMinute));
isfalse = redis.ExpireEntryAt(key, DateTime.Now.AddMinutes(nTimeMinute));
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override bool SetCache<T>(string key, T t, int timeOutMinute = 10, bool isSerilize = false)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
if (isSerilize)
{
var data = JsonConvert.SerializeObject(t);
var bb = System.Text.Encoding.UTF8.GetBytes(data);
isfalse = redis.Set<byte[]>(key, bb, TimeSpan.FromMinutes(timeOutMinute));
}
else { isfalse = redis.Set<T>(key, t, TimeSpan.FromMinutes(timeOutMinute)); }
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override T GetCache<T>(string key, bool isSerilize = false)
{
var t = default(T);
try
{
if (string.IsNullOrEmpty(key)) { return t; }
InitCache(false);
if (isSerilize)
{
var bb = redis.Get<byte[]>(key);
if (bb.Length <= 0) { return t; }
var data = System.Text.Encoding.UTF8.GetString(bb);
t = JsonConvert.DeserializeObject<T>(data);
}
else { t = redis.Get<T>(key); }
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return t;
}
public override bool Remove(string key)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
isfalse = redis.Remove(key);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override bool SetHashCache<T>(string hashId, string key, T t, int nTimeMinute = 10)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(key) || t == null) { return isfalse; }
InitCache();
var result = JsonConvert.SerializeObject(t);
if (string.IsNullOrEmpty(result)) { return isfalse; }
isfalse = redis.SetEntryInHash(hashId, key, result);
if (isfalse) { AddExpire(key, nTimeMinute); }
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override List<string> GetHashKeys(string hashId)
{
var hashKeys = new List<string>();
try
{
if (string.IsNullOrEmpty(hashId)) { return hashKeys; }
InitCache();
hashKeys = redis.GetHashKeys(hashId);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return hashKeys;
}
public override List<string> GetHashValues(string hashId)
{
var hashValues = new List<string>();
try
{
if (string.IsNullOrEmpty(hashId)) { return hashValues; }
InitCache();
hashValues = redis.GetHashValues(hashId);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return hashValues;
}
public override T GetHashValue<T>(string hashId, string key)
{
var t = default(T);
try
{
if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(key)) { return t; }
InitCache();
var result = redis.GetValueFromHash(hashId, key);
if (string.IsNullOrEmpty(result)) { return t; }
t = JsonConvert.DeserializeObject<T>(result);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return t;
}
public override bool RemoveHashByKey(string hashId, string key)
{
var isfalse = false;
try
{
if (string.IsNullOrEmpty(hashId) || string.IsNullOrEmpty(key)) { return isfalse; }
InitCache();
isfalse = redis.RemoveEntryFromHash(hashId, key);
}
catch (Exception ex)
{
}
finally { this.Dispose(); }
return isfalse;
}
public override void Dispose(bool isfalse)
{
if (isfalse && redis != null)
{
redis.Dispose();
redis = null;
}
}
#endregion
}
/// <summary>
/// Memcached缓存
/// </summary>
public class MemcachedCache : BaseCache
{
}
#endregion
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有