예제 #1
0
        public static async Task <bool> Set(ReleaseParams releaseParams, ResponseCachedItem cachedItem)
        {
            if (Container == null)
            {
                return(false);
            }
            var result = true;

            foreach (var item in releaseParams.CachedProviders)
            {
                try
                {
                    var provider = GetProvider(item);
                    if (provider == null)
                    {
                        throw new Exception(string.Format("未找到缓存提供者:{0}.", item));
                    }
                    if (!await provider.Set(releaseParams, cachedItem))
                    {
                        result = false;
                    }
                }
                catch (Exception ex)
                {
                    // todo: log
                }
            }
            return(result);
        }
예제 #2
0
 /// <summary>
 /// 写入静态文件
 /// </summary>
 /// <param name="cachedItem"></param>
 /// <param name="releaseParams"></param>
 /// <returns></returns>
 private async Task <bool> Input(ResponseCachedItem cachedItem, ReleaseParams releaseParams)
 {
     try
     {
         return(await ResponseCachedMamnager.Set(releaseParams, cachedItem));
     }
     catch (Exception ex)
     {
         // todo: 日志
     }
     return(false);
 }
        public async Task <bool> Set(ReleaseParams releaseParams, ResponseCachedItem cachedItem)
        {
            string path = Config.savePath + releaseParams.Key;

            if (!path.TryCreateDirectory())
            {
                return(false);
            }
            var jsonObj = JsonConvert.SerializeObject(cachedItem);
            var buffer  = SystemConfig.Instance.TryGetEncoding.GetBytes(jsonObj);

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                await stream.WriteAsync(buffer, 0, buffer.Length);
            }
            return(File.Exists(path));
        }
예제 #4
0
        /// <summary>
        /// 从当前请求中解析出ReleaseParams
        /// </summary>
        /// <param name="path"></param>
        /// <param name="releaseItem"></param>
        /// <returns>返回模板,物理路径</returns>
        private ReleaseParams InnerParse(string path, ReleaseConfigItem releaseItem)
        {
            if (!releaseItem.IsMatch(path))
            {
                return(null);
            }
            var releaseParams = new ReleaseParams()
            {
                Template     = releaseItem.Template,
                ClientCached = releaseItem.ClientCached
            };

            releaseParams.CachedProviders = releaseItem.TryGetProviders;
            releaseParams.Uri             = path;
            releaseParams.Key             = path;
            releaseParams.Parameters.AddRange(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
            return(releaseParams);
        }
        public async Task <ResponseCachedItem> Get(ReleaseParams releaseParams)
        {
            string path = Config.savePath + releaseParams.Key;

            if (!File.Exists(path))
            {
                return(null);
            }
            byte[] buffer = null;
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                buffer = new byte[stream.Length];
                await stream.ReadAsync(buffer, 0, buffer.Length);
            }
            var jsonObj = SystemConfig.Instance.TryGetEncoding.GetString(buffer);

            return(JsonConvert.DeserializeObject <ResponseCachedItem>(jsonObj));
        }
예제 #6
0
 public async Task <bool> Set(ReleaseParams releaseParams, ResponseCachedItem cachedItem)
 {
     try
     {
         Lock.EnterWriteLock();
         if (CachedBuffer.ContainsKey(releaseParams.Key))
         {
             CachedBuffer.Remove(releaseParams.Key);
         }
         CachedBuffer.Add(releaseParams.Key, cachedItem);
         await Task.CompletedTask;
         return(true);
     }
     finally
     {
         Lock.ExitWriteLock();
     }
 }
예제 #7
0
        public async Task <ResponseCachedItem> Get(ReleaseParams releaseParams)
        {
            try
            {
                Lock.EnterReadLock();
                if (CachedBuffer.ContainsKey(releaseParams.Key))
                {
                    return(CachedBuffer[releaseParams.Key]);
                }
            }
            finally
            {
                Lock.ExitReadLock();
            }
            await Task.CompletedTask;

            return(null);
        }
예제 #8
0
        /// <summary>
        /// 输出静态文件至前端
        /// </summary>
        /// <param name="context"></param>
        /// <param name="releaseParams"></param>
        /// <returns></returns>
        private async Task <bool> Output(HttpContext context, ReleaseParams releaseParams)
        {
            try
            {
                var cachedItem = await ResponseCachedMamnager.Get(releaseParams);

                if (cachedItem == null)
                {
                    return(false);
                }
                context.Response.Headers.Clear();
                if (releaseParams.ClientCached)
                {
                    var clientTag = context.Request.Headers[HeaderNames.IfNoneMatch];
                    if (!string.IsNullOrEmpty(clientTag) && clientTag == cachedItem.HashCode)
                    {
                        context.Response.StatusCode = 304;
                        return(true);
                    }
                    context.Response.Headers.Add(HeaderNames.ETag, cachedItem.HashCode);
                }
                context.Response.StatusCode = cachedItem.StatusCode;
                foreach (var header in cachedItem.Headers)
                {
                    if (!context.Response.Headers.ContainsKey(header.Key))
                    {
                        context.Response.Headers.Add(header.Key, header.Value);
                    }
                }
                await context.Response.Body.WriteAsync(cachedItem.Body, 0, cachedItem.Body.Length);

                return(true);
            }
            catch (Exception ex)
            {
                // todo: 日志
            }
            return(false);
        }
예제 #9
0
        public static async Task <ResponseCachedItem> Get(ReleaseParams releaseParams)
        {
            if (Container == null)
            {
                return(null);
            }
            ResponseCachedItem      result      = null;
            IResponseCachedProvider preProvider = null;

            foreach (var item in releaseParams.CachedProviders)
            {
                try
                {
                    var provider = GetProvider(item);
                    if (provider == null)
                    {
                        throw new Exception(string.Format("未找到缓存提供者:{0}.", item));
                    }
                    result = await provider.Get(releaseParams);

                    if (result != null)
                    {
                        if (preProvider != null)
                        {
                            await provider.Set(releaseParams, result);
                        }
                        break;
                    }
                    preProvider = provider;
                }
                catch (Exception ex)
                {
                    // todo: log
                }
            }
            return(result);
        }