NancyFx 2.0的开源框架的使用-Caching

新建一个空的Web项目,命名CachingDemo

 

 

 

 

然后添加三个Nuget安装包

  • Nancy
  • Nancy.Hosting.Aspnet
  • Nancy.ViewsEngines.Razor

然后往项目里面添加Models,Module,Views三个文件夹

再往Models文件夹里面添加CachingExtensions文件夹

然后往CachingExtensions文件夹里面添加ContextExtensions类

 public static class ContextExtensions
    {
        public const string OUTPUT_CACHE_TIME_KEY = "OUTPUT_CACHE_TIME";

        //启用此路由的输出缓存
        public static void EnableOutputCache(this NancyContext context,int seconds)
        {
            context.Items[OUTPUT_CACHE_TIME_KEY]=seconds;
        }
        //禁用此路由的输出缓存
        public static void DisableOutputCache(this NancyContext context)
        {
            context.Items.Remove(OUTPUT_CACHE_TIME_KEY);
        }
    }

再往Models文件夹里面添加CachedResponse类

        //在缓存的响应中封装常规响应
        //缓存的响应调用旧的响应并将其存储为字符串。
        //显然, 这只适用于基于 ascii 文本的响应, 所以不要使用此
        //在实际应用中:-)
        private readonly Response response;
        public CachedResponse(Response response)
        {
            this.response = response;
            this.ContentType = response.ContentType;
            this.Headers = response.Headers;
            this.Contents = this.GetContents();
        }
        public override Task PreExecute(NancyContext context)
        {
            //return base.PreExecute(context);
            return this.response.PreExecute(context);
        }
        private Action<Stream> GetContents()
        {
            return stream =>
            {
                using (var memoryStream = new MemoryStream())
                {
                    this.response.Contents.Invoke(memoryStream);
                    var contents = Encoding.ASCII.GetString(memoryStream.GetBuffer());
                    var writer = new StreamWriter(stream)
                    {
                        AutoFlush = true
                    };
                    writer.Write(contents); 
                }
            };
        }

然后往Module文件夹里面添加MainModule类

public MainModule()
        {
            Get("/",Lexan=>
            {
                return View["index.cshtml",DateTime.Now.ToString()];
            });
            Get("/cached",Lexan=>
            {
                this.Context.EnableOutputCache(30);
                return View["Payload.cshtml",DateTime.Now.ToString()];
            });
            Get("/uncached",Lexan=>
            {
                this.Context.DisableOutputCache();
                return View["Payload.cshtml",DateTime.Now.ToString()];
            });
        }

继续往根目录添加Bootstrapper类

        private const int CACHE_SECONDS = 30;
        private readonly Dictionary<string, Tuple<DateTime, Response, int>> cachedResponses = new Dictionary<string, Tuple<DateTime, Response, int>>();
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
            pipelines.BeforeRequest += CheckCache;
            pipelines.AfterRequest += SetCache;
        }
        //检查我们是否有缓存条目-如果我们做了, 看看它是否已经过期,
        //如果没有返回, 否则返回 null;
        public Response CheckCache(NancyContext context)
        {
            Tuple<DateTime, Response, int> cacheEntry;
            if (this.cachedResponses.TryGetValue(context.Request.Path,out cacheEntry))
            {
                if (cacheEntry.Item1.AddSeconds(cacheEntry.Item3)>DateTime.Now)
                {
                    return cacheEntry.Item2;
                }
            }
            return null;
        }
        //如果需要, 将当前响应添加到缓存中
        //仅按路径存储, 并将响应存储在字典中。
        //不要将此作为实际缓存:-)
        public void SetCache(NancyContext contexts)
        {
            if (contexts.Response.StatusCode!=HttpStatusCode.OK)
            {
                return;
            }
            object cacheSecondsObject;
            if (!contexts.Items.TryGetValue(ContextExtensions.OUTPUT_CACHE_TIME_KEY,out cacheSecondsObject))
            {
                return;
            }
            int cacheSeconds;
            if (!int.TryParse(cacheSecondsObject.ToString(),out cacheSeconds))
            {
                return;
            }
            var cachedResponse = new CachedResponse(contexts.Response);
            this.cachedResponses[contexts.Request.Path]=new Tuple<DateTime, Response, int>(DateTime.Now,cachedResponse,cacheSeconds);
            contexts.Response = cachedResponse;
        }

然后往Views文件夹里面添加index,payload,两个视图

index页面

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>首页</title>
</head>
<body>
    <div>
    <h1>缓存Demo</h1>
        <p><a href="/cached">Cached页面</a></p>
        <p><a href="/uncached">Uncached页面</a></p>
    </div>
</body>
</html>

Payload页面

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>首页</title>
</head>
<body>
    <div>
    <h1>缓存Demo</h1>
        <p>此页是在: @Model</p>
        <p>这可能是或可能不会被缓存</p>
        <a href="/">主页</a>
    </div>
</body>
</html>

修改Web.config配置文件

    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
    </handlers>
  </system.webServer>

然后运行一下项目F5

谢谢你的欣赏,如有不对请多多指教!

转载于:https://www.cnblogs.com/R00R/p/6863134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`Flask-Caching` 是 Flask 框架的一个扩展,它提供了一个缓存装饰器,可以缓存 Flask 应用程序的视图函数的输出。Flask-Caching 支持多种缓存后端,例如内存、文件、Redis、Memcached 等,可以根据实际需求选择合适的缓存后端。 使用 Flask-Caching,你可以很容易地将缓存添加到 Flask 应用程序中。只需要在 Flask 应用程序中导入 `flask_caching.Cache` 类,然后创建一个缓存对象。在视图函数中使用 `@cache.cached` 装饰器,就可以缓存视图函数的输出了。 下面是一个使用 Flask-Caching 的示例: ``` from flask import Flask from flask_caching import Cache app = Flask(__name__) cache = Cache(app, config={'CACHE_TYPE': 'simple'}) @app.route('/') @cache.cached(timeout=60) def index(): return 'Hello, World!' ``` 在上面的示例中,`cache` 是一个 Flask-Caching 缓存对象,使用 `simple` 缓存后端。`@cache.cached` 装饰器表示对视图函数进行缓存,`timeout=60` 表示缓存的超时时间为 60 秒。当第一个用户访问 `/` 路径时,视图函数 `index()` 的输出将被缓存起来,并在下一次请求时直接返回缓存的输出,而不是再次执行视图函数。 Flask-Caching 还支持其他缓存选项,例如缓存键生成、条件缓存、缓存清除等。你可以根据实际需求选择合适的选项,以提高应用程序的性能和响应速度。 总之,Flask-Caching 是一个非常方便的 Flask 扩展,可以帮助你轻松地将缓存添加到 Flask 应用程序中,从而提高应用程序的性能和响应速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值