using System.Collections.Generic;
using CYQ.Data.Table;
using CYQ.Data.Tool;
using System;
using CYQ.Data.SQL;
namespace CYQ.Data.Cache
{
///
/// 全局缓存类
///
///
/// 使用示例:
/// 实例化: CacheManage cache=CacheManage.Instance;
/// 添加: Cache.Set("路过秋天",new MDataTable);
/// 判断: if(cache.Contains("路过秋天"))
/// {
/// 获取: MDataTable table=cache.Get("路过秋天") as MDataTable;
/// }
///
public abstract partial class CacheManage
{
#region 对外实例
///
/// 返回唯一实例(根据配置(AppConfig.Cache.XXXCacheServers)决定启用顺序:Redis、MemCache、本地缓存)
///
public static CacheManage Instance
{
get
{
if (!string.IsNullOrEmpty(AppConfig.Cache.RedisServers))
{
return RedisInstance;
}
else if (!string.IsNullOrEmpty(AppConfig.Cache.MemCacheServers))
{
return MemCacheInstance;
}
else
{
return LocalShell.instance;
}
}
}
///
/// 单机本地缓存
///
public static CacheManage LocalInstance
{
get
{
return LocalShell.instance;
}
}
private static CacheManage _MemCacheInstance;
private static readonly object lockMemCache = new object();
///
/// MemCache缓存(需要配置AppConfig.Cache.MemCacheServers)
///
public static CacheManage MemCacheInstance
{
get
{
if (_MemCacheInstance == null)
{
lock (lockMemCache)
{
if (_MemCacheInstance == null)
{
_MemCacheInstance = new MemCache();
}
}
}
return _MemCacheInstance;
}
}
private static CacheManage _RedisInstance;
private static readonly object lockRedisCache = new object();
///
/// Redis缓存(需要配置AppConfig.Cache.RedisServers)
///
public static CacheManage RedisInstance
{
get
{
if (_RedisInstance == null)
{
lock (lockRedisCache)
{
if (_RedisInstance == null)
{
_RedisInstance = new RedisCache();
}
}
}
return _RedisInstance;
}
}
class LocalShell
{
internal static readonly LocalCache instance = new LocalCache();
}
//此种方式,会提前处理,导致异常。
//class MemShell
//{
// internal static readonly MemCache instance = new MemCache();
//}
//class RedisShell
//{
// internal static readonly RedisCache instance = new RedisCache();
//}
#endregion
///
/// 缓存的实例类型
///
public abstract CacheType CacheType { get; }
///
/// 缓存的信息
///
public abstract MDataTable CacheInfo { get; }
///
/// 设置一个Cache对象
///
public abstract void Set(string key, object value);
public abstract void Set(string key, object value, double cacheMinutes);
public abstract void Set(string key, object value, double cacheMinutes, string fileName);
///
/// 清除所有缓存
///
public abstract void Clear();
public abstract bool Contains(string key);
///
/// 获和缓存总数
///
public abstract int Count { get; }
///
/// 获得一个Cache对象
///
public abstract object Get(string key);
///
/// 获得一个Cache对象
///
public T Get(string key)
{
object o = Get(key);
if (o != null)
{
Type t = typeof(T);
try
{
return (T)StaticTool.ChangeType(o, t);
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
return default(T);
}
}
return default(T);
}
///
/// 删除一个Cache对象
///
public abstract void Remove(string key);
public abstract string WorkInfo { get; }
}
public abstract partial class CacheManage
{
///
/// 获取系统内部缓存Key
///
public static string GetKey(CacheKeyType ckt, string tableName)
{
return GetKey(ckt, tableName, AppConfig.DB.DefaultDataBase, AppConfig.DB.DefaultDalType);
}
///
/// 获取系统内部缓存Key
///
public static string GetKey(CacheKeyType ckt, string tableName, string dbName, DalType dalType)
{
switch (ckt)
{
case CacheKeyType.Schema:
return TableSchema.GetSchemaKey(tableName, dbName, dalType);
case CacheKeyType.AutoCache:
return AutoCache.GetBaseKey(dalType, dbName, tableName);
}
return string.Empty;
}
}
public abstract partial class CacheManage
{
///
/// 通过该方法可以预先加载整个数据库的表结构缓存
///
public static void PreLoadDBSchemaToCache()
{
PreLoadDBSchemaToCache(AppConfig.DB.DefaultConn, true);
}
private static readonly object obj = new object();
///
/// 通过该方法可以预先加载整个数据库的表结构缓存(异常会抛,外层Try Catch)
///
/// 指定数据链接
/// 是否开启线程
public static void PreLoadDBSchemaToCache(string conn, bool isUseThread)
{
if (TableSchema.tableCache == null || TableSchema.tableCache.Count == 0)
{
lock (obj)
{
if (TableSchema.tableCache == null || TableSchema.tableCache.Count == 0)
{
if (isUseThread)
{
ThreadBreak.AddGlobalThread(new System.Threading.ParameterizedThreadStart(LoadDBSchemaCache), conn);
}
else
{
LoadDBSchemaCache(conn);
}
}
}
}
}
private static void LoadDBSchemaCache(object connObj)
{
string conn = Convert.ToString(connObj);
Dictionary dic = DBTool.GetTables(Convert.ToString(conn));
if (dic != null && dic.Count > 0)
{
DbBase helper = DalCreate.CreateDal(conn);
if (helper.dalType != DalType.Txt && helper.dalType != DalType.Xml)
{
foreach (string key in dic.Keys)
{
TableSchema.GetColumns(key, ref helper);
}
}
helper.Dispose();
}
}
}
///
/// 支持的Cache类型
///
public enum CacheType
{
///
/// 本地缓存
///
LocalCache,
///
/// MemCached分布式缓存
///
MemCache,
///
/// Redis分布式缓存
///
Redis
}
///
/// Cache的Key类型
///
public enum CacheKeyType
{
///
/// 表架构的Key
///
Schema,
///
/// 智能缓存Key
///
AutoCache
}
}