C# 添加后缓存中没有的项。使用webapi和内存缓存

C# 添加后缓存中没有的项。使用webapi和内存缓存,c#,asp.net-mvc,caching,asp.net-web-api2,memorycache,C#,Asp.net Mvc,Caching,Asp.net Web Api2,Memorycache,当我将项目添加到内存缓存时,在下一个请求中它是空的。有人能帮我吗 这是我的缓存服务: public class CacheService : ICacheService { private readonly ObjectCache _cache; public CacheService(ObjectCache cache) { _cache = cache; } public virtual void Add(object it

当我将项目添加到内存缓存时,在下一个请求中它是空的。有人能帮我吗

这是我的缓存服务:

    public class CacheService : ICacheService
{
    private readonly ObjectCache _cache;

    public CacheService(ObjectCache cache)
    {
        _cache = cache;
    }

    public virtual void Add(object item, string key, int cacheTime, ExpirationInterval expirationInterval = ExpirationInterval.Hours)
    {
        if (item == null)
            return;
        var cachePolicy = new CacheItemPolicy();
        switch (expirationInterval)
        {
            case ExpirationInterval.Minutes:
                cachePolicy.SlidingExpiration = new TimeSpan(0, cacheTime, 0);
                break;
            case ExpirationInterval.Hours:
                cachePolicy.AbsoluteExpiration = DateTime.Now.AddHours(cacheTime);
                break;
            case ExpirationInterval.Infinite:
                cachePolicy.AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration;
                break;
            default:
                throw new ArgumentOutOfRangeException("expirationInterval");
        }
        _cache.Add(key, item, cachePolicy);
    }

    public bool Exists(string key)
    {
        return _cache[key] != null;
    }

    public T Get<T>(string key)
    {

        return (T)_cache[key];
    }
在此处向ninject注册我的服务:

            kernel.Bind<System.Runtime.Caching.ObjectCache>()
            .To<System.Runtime.Caching.MemoryCache>()
            .InSingletonScope()
            .WithConstructorArgument("name", "Enquiries")
            .WithConstructorArgument("config", (object) null);
然后,我在查询服务中使用以下缓存服务:

  public class EnquiriesService : IEnquiriesService
{
    private readonly ICacheService _cacheService;
    private readonly IUnitOfWork _unitOfWork;

    public EnquiriesService(ICacheService cacheService, IUnitOfWork unitOfWork)
    {
        _cacheService = cacheService;
        _unitOfWork = unitOfWork;
    }

    public IEnumerable<Enquiry> GetEnquiries(DateTime date)
    {
        var cacheName = "Enquires_" + date.ToShortDateString();
        IEnumerable<Enquiry> enquiries;

        if (_cacheService.Exists(cacheName))
        {
            enquiries = _cacheService.Get<IEnumerable<Enquiry>>(cacheName);
        }
        else
        {
            enquiries =
                _unitOfWork.EnquiriesRepository.GetAll()
                    .Where(x => DbFunctions.TruncateTime(x.DateOfEnquiry) == date.Date);
            _cacheService.Add(enquiries, cacheName, 24);
        }
        return enquiries;
    }
然后将此服务注入我的控制器:

        public EnquiriesController(IEnquiriesService enquiriesService)
    {
        _enquiriesService = enquiriesService;
    }

    [HttpGet]
    public IEnumerable<Enquiry> Enquiries(DateTime date)
    {
        return _enquiriesService.GetEnquiries(date);
    }

你知道为什么我可以看到成功添加的条目,但当我第二次尝试检索它们时,缓存中没有任何内容吗?我假设memorycache在每个请求上都被实例化了?如何停止此操作并允许缓存对象持久化

看起来像是马车:

这两篇文章都很老,但你似乎遇到了相同的问题。也许您应该使用这种方法:

kernel.Bind<System.Runtime.Caching.ObjectCache>()
        .To<System.Runtime.Caching.MemoryCache>()
        .ToConstant(new MemoryCache("Enquiries"));
或者只是

 kernel.Bind<System.Runtime.Caching.ObjectCache>()
        .To<System.Runtime.Caching.MemoryCache>()
        .ToConstant(MemoryCache.Default);