using System; using System.Collections.Generic; using System.Text; using System.Data; namespace IDbSource { /// <summary> /// 数据缓存类,缓存到缓存数据库 /// </summary> public class IDbWinCaching : Helper { public IDbWinCaching() { ConnString = IConnString.GetConnString(DbType.Caching); } /// <summary> /// 默认过期时间,为30分钟 /// </summary> private int expireMinute = 30; /// <summary> /// 当前记录是否存在 /// </summary> private bool isExist = true; #region 少量内容的缓存 /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期]默认30分钟 /// </summary> /// <param name="channelType">域名分类代码</param> /// <returns></returns> public bool ContentIsExpire(string channelType) { isExist = true; string sql = "SELECT TOP 1 c_CacheTime FROM C_T_Cache_Content WHERE channelType='" + channelType + "'"; string d0 = GetScalar(sql); if (d0 == "") //没有数据 { isExist = false; return true; } int h = DateTime.Now.Hour; //如果在7点前或晚上21点后,直接取缓存的数据 if (h < 7 || h > 21) { return false; } DateTime d; DateTime.TryParse(d0, out d); int d1 = ICore.IChecking.IntParse(DateTime.Now.ToString("MMddHHmm")); int d2 = ICore.IChecking.IntParse(d.ToString("MMddHHmm")); //30分钟以内 if ((d1 - d2) < expireMinute) { return false; } else { return true; } } /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期] /// </summary> /// <param name="channelType">域名分类代码</param> /// <param name="minute">过期的时间间隔,单位:分钟</param> /// <returns></returns> public bool ContentIsExpire(string channelType,int minute) { if (minute < 1) { minute = 1; } expireMinute = minute; return ContentIsExpire(channelType); } /// <summary> /// 保存需要缓存的内容 /// </summary> /// <param name="channelType"></param> /// <param name="content"></param> public void ContentSet(string channelType, string content) { //return; string sql = ""; sql = "EXEC [C_P_Cache_Content_Insert] '" + channelType + "','" + content.Replace("'", "''") + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"; ExecuteSql(sql); } /// <summary> /// 获取缓存的内容 /// </summary> /// <param name="channelType"></param> /// <returns></returns> public string ContentGet(string channelType) { string sql = "SELECT TOP 1 c_Content FROM C_T_Cache_Content WHERE channelType='" + channelType + "';"; return GetScalar(sql); } #endregion #region 大量内容的缓存 /// <summary> /// 判断缓存是否过期[ true 过期, false未过期]默认30分钟 /// </summary> /// <param name="channelType">域名分类代码</param> /// <returns></returns> public bool BigContentIsExpire(BigContentChannelType channel, int id) { isExist = true; string sql = "SELECT TOP 1 cacheTime FROM " + channel.ToString() + " WHERE id=" + id + ""; string d0 = GetScalar(sql); if (d0 == "") //没有数据 { isExist = false; return true; } int h = DateTime.Now.Hour; //如果在7点前或晚上21点后,直接取缓存的数据 if (h < 7 || h > 21) { return false; } DateTime d; DateTime.TryParse(d0, out d); int d1 = ICore.IChecking.IntParse(DateTime.Now.ToString("MMddHHmm")); int d2 = ICore.IChecking.IntParse(d.ToString("MMddHHmm")); //30分钟以内 if ((d1 - d2) < expireMinute) { return false; } else { return true; } } /// <summary> /// 判断缓存是否过期[ true 过期, false未过期] /// </summary> /// <param name="channel">分类代码</param> /// <param name="id">id</param> /// <param name="minute">过期的时间间隔,单位:分钟</param> /// <returns></returns> public bool BigContentIsExpire(BigContentChannelType channel, int id, int minute) { if (minute < 1) { minute = 1; } expireMinute = minute; return BigContentIsExpire(channel, id); } /// <summary> /// 保存需要缓存的内容 /// </summary> /// <param name="channel"></param> /// <param name="id"></param> public void BigContentSet(BigContentChannelType channel, int id, string content) { string sql; string tablename = channel.ToString(); if (isExist) { sql = "UPDATE " + tablename + " SET [Content]='" + content.Replace("'", "''") + "',cacheTime='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE id=" + id.ToString(); } else { sql = "EXEC [" + tablename.Replace("_T_", "_P_") + "_Insert] " + id.ToString() + ",'" + content.Replace("'", "''") + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"; } ExecuteSql(sql); } /// <summary> /// 获取缓存的内容 /// </summary> /// <param name="channel"></param> /// <param name="id"></param> /// <returns></returns> public string BigContentGet(BigContentChannelType channel, int id) { string sql = "SELECT TOP 1 [Content] FROM " + channel.ToString() + " WHERE ID=" + id.ToString() + ";"; return GetScalar(sql); } /// <summary> /// 删除除缓存的内容 /// </summary> /// <param name="channel"></param> /// <param name="id"></param> /// <returns></returns> public void BigContentDelete(BigContentChannelType channel, int id) { string sql = "DELETE " + channel.ToString() + " WHERE ID=" + id.ToString() + ";"; ExecuteSql(sql); } /// <summary> /// 清空缓存的内容 /// </summary> /// <param name="channel"></param> /// <param name="id"></param> /// <returns></returns> public void BigContentClear(BigContentChannelType channel) { string sql = "DELETE " + channel.ToString() + " ;"; ExecuteSql(sql); } public enum BigContentChannelType { C_T_Cache_XzShow } #endregion #region 列表查询页的缓存 /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期]默认30分钟 /// </summary> /// <param name="url">当前的网址</param> /// <returns></returns> public bool ListPageIsExpire(string url) { url = url.Replace("'", ""); isExist = true; string sql = "SELECT TOP 1 cacheTime FROM C_T_Cache_ListPage WHERE url='" + url + "'"; string d0 = GetScalar(sql); if (d0 == "") //没有数据 { isExist = false; return true; } int h = DateTime.Now.Hour; //如果在7点前或晚上21点后,直接取缓存的数据 if (h < 7 || h > 21) { return false; } DateTime d; DateTime.TryParse(d0, out d); int d1 = ICore.IChecking.IntParse(DateTime.Now.ToString("MMddHHmm")); int d2 = ICore.IChecking.IntParse(d.ToString("MMddHHmm")); //30分钟以内 if ((d1 - d2) < expireMinute) { return false; } else { return true; } } /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期] /// </summary> /// <param name="url">当前的网址</param> /// <param name="minute">过期的时间间隔,单位:分钟</param> /// <returns></returns> public bool ListPageIsExpire(string url, int minute) { if (minute < 1) { minute = 1; } expireMinute = minute; return ListPageIsExpire(url); } /// <summary> /// 保存需要缓存的内容 /// </summary> /// <param name="url"></param> /// <param name="content"></param> public void ListPageSet(string url, string content) { string sql; url = url.Replace("'", ""); content = content.Replace("'", "''"); if (isExist) { sql = "UPDATE C_T_Cache_ListPage SET [Content]='" + content + "',cacheTime='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE url='" + url + "'"; } else { sql = "EXEC [C_P_Cache_ListPage_INSERT] '" + url + "','" + content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"; } ExecuteSql(sql); } /// <summary> /// 获取缓存的内容 /// </summary> /// <param name="url"></param> /// <returns></returns> public string ListPageGet(string url) { url = url.Replace("'", ""); string sql = "SELECT TOP 1 [Content] FROM C_T_Cache_ListPage WHERE url='" + url + "';"; return GetScalar(sql); } #endregion #region public enum TiteCannelType { News, Down, Ask, Ba, Baike, Zhishi, Biz, Company, HomeArt } /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期]默认30分钟 /// </summary> /// <param name="channelType">域名分类代码</param> /// <returns></returns> public bool TitleListIsExpire(TiteCannelType channelType,string key) { isExist = true; string sql = "SELECT TOP 1 cacheTime FROM C_T_Cache_TitleList_" + channelType.ToString() + " WHERE keyword='" + key + "'"; string d0 = GetScalar(sql); if (d0 == "") //没有数据 { isExist = false; return true; } int h = DateTime.Now.Hour; //如果在7点前或晚上21点后,直接取缓存的数据 if (h < 7 || h > 21) { return false; } DateTime d; DateTime.TryParse(d0, out d); int d1 = ICore.IChecking.IntParse(DateTime.Now.ToString("MMddHHmm")); int d2 = ICore.IChecking.IntParse(d.ToString("MMddHHmm")); //30分钟以内 if ((d1 - d2) < expireMinute) { return false; } else { return true; } } /// <summary> /// 判断缓存内容是否过期[ true 过期, false未过期] /// </summary> /// <param name="channelType">域名分类代码</param> /// <param name="minute">过期的时间间隔,单位:分钟</param> /// <returns></returns> public bool TitleListIsExpire(TiteCannelType channelType, string key, int minute) { if (minute < 1) { minute = 1; } expireMinute = minute; return TitleListIsExpire(channelType,key); } /// <summary> /// 保存需要缓存的内容 /// </summary> /// <param name="channelType"></param> /// <param name="content"></param> public void TitleListSet(TiteCannelType channelType, string key, string titlelist) { string tablename = "C_T_Cache_TitleList_" + channelType.ToString() + ""; string sql; if (isExist) { sql = "UPDATE " + tablename + " SET titlelist='" + titlelist + "',cacheTime='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE keyword='" + key + "';"; } else { sql = "EXEC C_P_Cache_TitleList_Insert '" + tablename + "','" + key + "','" + titlelist + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'"; } ExecuteSql(sql); } /// <summary> /// 获取缓存的内容 /// </summary> /// <param name="channelType"></param> /// <returns></returns> public string TitleListGet(TiteCannelType channelType, string key) { string sql = "SELECT TOP 1 titlelist FROM C_T_Cache_TitleList_" + channelType.ToString() + " WHERE keyword='" + key + "';"; return GetScalar(sql); } #endregion public void SessionSet(string key,string value) { Sql = "EXEC C_P_Session '" + key + "','" + value.Replace("'", "''") + "'"; ExecuteSql(Sql); } public string SessionGet(string key) { Sql = "SELECT TOP 1 sValue FROM C_T_Session WHERE SNAME='" + key + "' AND sActTime>'" + DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:mm:ss") + "'"; return GetScalar(Sql); } } }