/// <summary> /// 根据key查询缓存信息 /// </summary> /// <param name="key">HttpRequest唯一key</param> /// <returns>Html</returns> public CacheInfo Query(string key) { GetRealPath(); FileInfo fileinfo = new FileInfo(GetPath(key)); if (fileinfo.Exists) { var info = new CacheInfo(); info.StoreTime = fileinfo.LastWriteTime; info.Size = fileinfo.Length; return info; } return null; }
public void Save(string key, Stream repInfo) { Cache[key] = repInfo; CacheInfo info = null; if (infos.ContainsKey(key)) { info = infos[key]; } else { info = new CacheInfo(); } info.Size = repInfo.Length; info.StoreTime = DateTime.Now; infos[key] = info; }
/// <summary> /// 根据请求信息,生成实际的sql语句 /// </summary> /// <param name="req"></param> /// <returns></returns> private String buildSql(System.Web.HttpRequest req, CacheInfo info) { String sql = Sql; foreach (var item in Values) { var value = req.QueryString[item]; if (!String.IsNullOrEmpty(value)) { sql = sql.Replace("GET[" + item + "]", value); } else { LogHelp.Warn("SqlExpire error:" + "配置了Get变量" + item + ",在Request.QueryString中确找不到"); } } sql = sql.Replace("[STORETIME]", info.StoreTime.AddSeconds(3).ToString("yyyy-MM-dd HH:mm:ss")); sql = sql.Replace("[LENGTH]", info.Size.ToString()); sql = sql.Replace("[KEY]", info.Key); return sql; }
public bool IsExpire(HttpRequest req, CacheInfo info) { return info == null || (DateTime.Now - info.StoreTime).TotalSeconds > _second; }
public bool IsExpire(System.Web.HttpRequest req, CacheInfo info) { return info == null || (DateTime.Now - info.StoreTime).TotalSeconds > _second || innerSqlExpire(req, info); }
/// <summary> /// 根据请求及配置的sql语句,查询数据库是否有记录,如果有记录代表已过期,反之亦然 /// </summary> /// <param name="req"></param> /// <param name="info"></param> /// <returns></returns> private bool innerSqlExpire(System.Web.HttpRequest req, CacheInfo info) { var sql = buildSql(req, info); var isExipre = false; if (!String.IsNullOrEmpty(ConnStr)) { using (var conn = GetConn()) { var comm = conn.CreateCommand(); comm.CommandText = sql; isExipre = comm.ExecuteReader().HasRows; } } return isExipre; }
public bool IsExpire(HttpRequest req, CacheInfo info) { return(info == null || (DateTime.Now - info.StoreTime).TotalSeconds > _second); }
public bool IsExpire(System.Web.HttpRequest req, CacheInfo info) { return(info == null || (DateTime.Now - info.StoreTime).TotalSeconds > _second || innerSqlExpire(req, info)); }
/// <summary> /// 缓存没过期,直接输出缓存 /// </summary> /// <param name="context"></param> /// <param name="key"></param> /// <param name="info"></param> private void ResponceCache(HttpContext context, string key, CacheInfo info) { var req = context.Request; var rep = context.Response; string time = info.StoreTime.ToString("r"); if (req.Headers["If-Modified-Since"] == time) { rep.StatusCode = (int)System.Net.HttpStatusCode.NotModified; LogHelp.Info("cache hit 304 " + req.RawUrl); } else { var httpInfo = new HttpInfo(); var _stream = Store.Get(key); int headEndPosition = 0; string firstLine = ""; httpInfo.Headers = HttpParseHelp.ParseHeader(_stream, ref headEndPosition, ref firstLine); rep.StatusCode = ParseRespondCode(firstLine); if (rep.StatusCode == 200) { rep.AppendHeader("Last-Modified", time); } httpInfo.Content = _stream; OutResponse(req, rep, httpInfo); LogHelp.Info("cache hit html " + req.RawUrl); } context.ApplicationInstance.CompleteRequest(); }