首页 > 解决方案 > Asp.net 缓存在 IIS 上不起作用。IIS上有没有设置

问题描述

我创建了一个如下所示的基本缓存示例。当我发布到本地 IIS 时,我的代码运行良好。但是,当我发布我的真实服务器时无法正常工作。没有任何错误。但是缓存总是空的。这可能是什么原因?IIS 或其他 Windows 配置中是否有任何设置?

using System;
using System.Runtime.Caching;
using System.Web.Http;

namespace ToplumMerkezleriCommonApi.Controllers
{
    public class ValuesController : ApiController
    {
        protected ObjectCache Cache => MemoryCache.Default;
        protected CacheItemPolicy policy = null;

        public object Get(string id)
        {
            if (Cache.Get("testcache") == null)
            {
                GetAllCacheItems();
            }
            return Cache[id] as Object;
        }

        private void GetAllCacheItems()
        {
            using (CommunityCenterEntities context = new CommunityCenterEntities())
            {
                policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(24);

                Cache.Set("testcache", DateTime.Now.ToString(), policy);
            }
        }
    }
}

标签: c#asp.netasp.net-mvciiscaching

解决方案


请检查,转到 IIS 管理器-> 站点节点-> 输出缓存-> 编辑功能设置-> 取消选中启用缓存对于静态文件,您启用 IIS 管理器-> 站点节点-> HTTP 响应标头-> 设置常用标头->使网页内容过期->立即。


推荐阅读