添加后不在缓存中的项目。使用Web Api和内存缓存

时间:2014-08-08 15:14:05

标签: c# asp.net-mvc caching asp.net-web-api2 memorycache

当我向Memory Cache添加项目时,在下一个请求中它是空的。有人可以帮帮我吗?

这是我的缓存服务:

    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?我怎么能阻止这个,并允许我的缓存对象持续存在?

1 个答案:

答案 0 :(得分:0)

它看起来像是错误的:

Stackoverflow: Singleton Scope binding not working as intended
Stackoverflow: Ninject InSingletonScope with Web Api RC

这两个帖子都很旧,但您似乎遇到了同样的问题。也许你应该使用这种方法:

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);