Example #1
0
        public void AddOrUpdateCache(string requestUrl, Stream httpBytesStream, DateTime lastModified, int expireInSeconds)
        {
            if (httpBytesStream == null)
            {
                return;
            }
            var bodyBuf = httpBytesStream.ReadAllBytes();

            //由于浏览器发送过来的报文头中的If-Modified-since字段没有精确到毫秒,所有写到缓存的lastModified也不应该精确到毫秒
            var cachedData = new CacheData {
                Url = requestUrl, HttpBodyBytes = bodyBuf, LastModified = lastModified.OmitMillisecond()
            };

            if (redisClient.Exists(cachedData.Url) > 0)
            {
                redisClient.Del(cachedData.Url);
            }
            redisClient.Set <CacheData>(cachedData.Url, cachedData);
            Expire(cachedData.Url, expireInSeconds);
        }
Example #2
0
        /// <summary>
        /// 单纯获取缓存日志中的对应行,不考虑缓存过期与否
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <returns></returns>
        private CacheData GetCache(string requestUrl)
        {
            CacheData r = null;

            if (!File.Exists(cacheFile))
            {
                return(r);
            }
            using (StreamReader sr = new StreamReader(cacheFile))
            {
                do
                {
                    string line = sr.ReadLine();
                    if (line == null || line == "")
                    {
                        break;
                    }
                    var      strs         = line.Split('@');
                    string   theUrl       = strs[0];
                    DateTime lastModified = DateTime.Parse(strs[1]);
                    int      exp          = -1;
                    int.TryParse(strs[2], out exp);
                    string theCachedPageName = strs[3];
                    if (requestUrl == theUrl)
                    {
                        var cachedHtmlPath = string.Format(@"{0}\{1}", htmlCachePath, theCachedPageName);
                        var buf            = File.ReadAllBytes(cachedHtmlPath);
                        r = new CacheData {
                            Url = theUrl, LastModified = lastModified, CachedHtmlFile = theCachedPageName, HttpBodyBytes = buf, ExpireInSeconds = exp
                        };
                        break;
                    }
                } while (true);
                sr.Close();
            }
            return(r);
        }
Example #3
0
 private void DeleteCacheFile(CacheData existedCacheItem)
 {
     File.Delete(string.Format(@"{0}\CachedHtml\{1}", baseDir, existedCacheItem.CachedHtmlFile));
 }