コード例 #1
0
        ///// <summary>
        ///// 设置全局数据缓存,如果存在仅修改值
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键值名称</param>
        ///// <param name="objObject">值</param>
        //public static void SetCache(this HttpContext context, string cacheKey, object objObject)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        context.Cache.Insert(cacheKey, objObject);
        //        return;
        //    }
        //    context.Cache[cacheKey] = objObject;
        //}

        ///// <summary>
        ///// 设置数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键值名称</param>
        ///// <param name="objObject">值</param>
        ///// <param name="timeout">单位秒,默认7200秒</param>
        //public static void SetCache(this HttpContext context, string cacheKey, object objObject, int timeout = 7200)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    //相对过期
        //    //context.Cache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
        //    //绝对过期时间
        //    context.Cache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
        //}

        ///// <summary>
        ///// 获取数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键</param>
        ///// <returns>返回一个值,如果键值名称不存在则返回NULL</returns>
        //public static object GetCache(this HttpContext context, string cacheKey)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        return null;
        //    }
        //    return context.Cache.Get(cacheKey); ;
        //}

        ///// <summary>
        ///// 移除指定数据缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        ///// <param name="cacheKey">键</param>
        //public static void RemoveCache(this HttpContext context, string cacheKey)
        //{
        //    if (string.IsNullOrWhiteSpace(cacheKey))
        //    {
        //        throw new System.SystemException("键值名称不能为空!");
        //    }
        //    if (context.Cache == null || context.Cache[cacheKey] == null)
        //    {
        //        return;
        //    }
        //    context.Cache.Remove(cacheKey);
        //}

        ///// <summary>
        ///// 移除全部缓存
        ///// </summary>
        ///// <param name="context">HttpContext</param>
        //public static void RemoveAllCache(this HttpContext context)
        //{
        //    if (context.Cache == null)
        //    {
        //        return;
        //    }
        //    var cacheEnum = context.Cache.GetEnumerator();
        //    while (cacheEnum.MoveNext())
        //    {
        //        context.Cache.Remove(cacheEnum.Key.ToString());
        //    }
        //}

        ///// <summary>
        ///// 调用此方法可以对请求的信息进行压缩处理
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <returns>返回当前发起请求的用户浏览器是否支持压缩信息 状态:<see cref="bool"/>类型</returns>
        //public static bool Deflate_Gzip(this HttpContext reques)
        //{
        //    string Accept = reques.Request.Headers["Accept-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Accept))
        //    {
        //        return false;
        //    }

        //    if (Accept.Contains("gzip"))
        //    {
        //        reques.Response.AppendHeader("Content-Encoding", "gzip");
        //        reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Compress);
        //        return true;
        //    }
        //    else if (Accept.Contains("deflate"))
        //    {
        //        reques.Response.AppendHeader("Content-Encoding", "deflate");
        //        reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Compress);
        //        return true;
        //    }
        //    return false;
        //}

        ///// <summary>
        ///// 调用此方法可以对请求的信息进行压缩处理
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <param name="httpEncoding">压缩枚举</param>
        ///// <returns>返回当前发起请求的用户浏览器是否支持压缩信息 状态:<see cref="bool"/>类型</returns>
        //public static bool Deflate_Gzip(this HttpContext reques, HttpEncoding httpEncoding)
        //{
        //    string Accept = reques.Request.Headers["Accept-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Accept))
        //    {
        //        return false;
        //    }

        //    if (httpEncoding == HttpEncoding.gzip)
        //    {
        //        if (Accept.Contains("gzip"))
        //        {
        //            reques.Response.AppendHeader("Content-Encoding", "gzip");
        //            reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Compress);
        //            return true;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    else if (httpEncoding == HttpEncoding.deflate)
        //    {
        //        if (Accept.Contains("deflate"))
        //        {
        //            reques.Response.AppendHeader("Content-Encoding", "deflate");
        //            reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Compress);
        //            return true;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        ///// <summary>
        ///// 解开以压缩的输出流
        ///// </summary>
        ///// <param name="reques"><see cref="HttpContext"/>对象</param>
        ///// <returns>返回状态</returns>
        //public static bool DecodeDeflate_Gzip(this HttpContext reques)
        //{
        //    string Content = reques.Response.Headers["Content-Encoding"];

        //    if (string.IsNullOrWhiteSpace(Content))
        //    {
        //        return false;
        //    }

        //    if (Content.Contains("deflate"))
        //    {
        //        //reques.Response.Headers.Remove("Accept-Encoding");
        //        reques.Response.Headers.Remove("Content-Encoding");
        //        reques.Response.Filter = new DeflateStream(reques.Response.Filter, CompressionMode.Decompress);
        //        return true;
        //    }
        //    else if (Content.Contains("gzip"))
        //    {
        //        //reques.Response.Headers.Remove("Accept-Encoding");
        //        reques.Response.Headers.Remove("Content-Encoding");
        //        reques.Response.Filter = new GZipStream(reques.Response.Filter, CompressionMode.Decompress);
        //        return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 将文件流分流写入
        /// </summary>
        /// <param name="ReadStream">原文件流</param>
        /// <param name="WriteStream">更新文件流</param>
        /// <param name="minlen">最小资源大小</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task StreamMove(System.IO.Stream ReadStream, System.IO.Stream WriteStream, int minlen)
        {
            long length = ReadStream.Length;
            long len    = (minlen) > length ? length : (minlen);

            byte[] slice = new byte[len];

            long seekiength = 0;

            do
            {
                int i = await ReadStream.ReadAsync(slice.AsMemory(0, slice.Length));

                await WriteStream.WriteAsync(slice.AsMemory(0, i));

                await WriteStream.FlushAsync();

                seekiength += i;
                Array.Clear(slice, 0, i);
            } while (length > seekiength);

            await ReadStream.DisposeAsync();
        }