7

我的 Application_Start 中有这个:

var crumbsCache = new MemoryCache("breadCrumbsNames");
var crumbsList = new List<CacheItem>
                    {
                        //list of new CacheItem();
                    };
foreach (var cacheItem in crumbsList)
{
    crumbsCache.Add(cacheItem, new CacheItemPolicy());
}

现在,在我的控制器中,我正在这样做:

var cache = new MemoryCache("breadCrumbsNames");
var cacheItem = cache.GetCacheItem("nameOfCacheItem");

但是 cacheItem 总是为空,我做错了什么?

4

2 回答 2

12

我认为对您来说更好的选择是使用Ninject或其他一些依赖注入框架根据MemoryCache需要将您注入控制器。

您将从向 ASP.NET MVC 项目中添加NinjectNinject.Mvc3(以及任何其他相关位)开始。如果您在 Visual Studio 中工作,则可以使用 NuGet 来执行此操作。它非常轻松且自动化程度很高。

下一步将把你包装MemoryCache到某种界面中,例如:

public interface IMemoryCacheService
{
    MemoryCache MemoryCache
    {
        get;
        set;
    }
}

和:

public class MemoryCacheService : IMemoryCacheService
{
    public MemoryCacheService()
    {
        MemoryCache = new MemoryCache();
    }

    public MemoryCache MemoryCache
    {
        get;
        set;
    }
}

然后在 Ninject 中定义一个绑定,以便 Ninject 知道当您需要某种类型时IMemoryCacheService,它应该为您提供MemoryCacheService.

我将在这里粘贴我自己的 Ninject 配置类。将在您的项目中创建的一个将非常相似,并将位于一个名为的文件夹中App_Start(如果您使用 NuGet,它将自动创建)。Ninject 默认创建的类称为NinjectWebCommon.

public static class NinjectConfig
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();

        kernel.Bind<Func<IKernel>>()
              .ToMethod(context => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>()
              .To<HttpApplicationInitializationHttpModule>();
        kernel.RegisterServices();

        return kernel;
    }

    private static void RegisterServices(this IKernel kernel)
    {
        kernel.Bind<IMemoryCacheService>()
              .To<MemoryCacheService>()
              .InSingletonScope();
              // InSingletonScope() is important so Ninject knows
              // to create only one copy and then reuse it every time
              // it is asked for

        // ignore the stuff below... I have left it in here for illustration
        kernel.Bind<IDbTransactionFactory>()
              .To<DbTransactionFactory>()
              .InRequestScope();
        kernel.Bind<IDbModelContext>()
              .To<DbModelContext>()
              .InRequestScope();
        kernel.Bind<IDbModelChangeContext>()
              .To<DbModelChangeContext>()
              .InRequestScope();
        kernel.Bind<IUserContext>()
              .To<UserContext>()
              .InRequestScope();

        kernel.BindAttributeAndFilter<IgnoreNonAjaxRequestsFilter, IgnoreNonAjaxRequestsAttribute>();
        kernel.BindAttributeAndFilter<ProvideApplicationInfoFilter, ProvideApplicationInfoAttribute>();
        kernel.BindAttributeAndFilter<ProvideSessionInfoFilter, ProvideSessionInfoAttribute>();
        kernel.BindAttributeAndFilter<UseDialogLayoutFilter, UseDialogLayoutAttribute>();
        kernel.BindAttributeAndFilter<CheckResourceAccessFilter, CheckResourceAccessAttribute>();
        kernel.BindAttributeAndFilter<CheckResourceStateFilter, CheckResourceStateAttribute>();
    }

    private static void BindAttributeAndFilter<TFilter, TAttribute>(this IKernel kernel)
    {
        kernel.BindFilter<TFilter>(FilterScope.Action, null)
              .WhenControllerHas<TAttribute>();
        kernel.BindFilter<TFilter>(FilterScope.Action, null)
              .WhenActionMethodHas<TAttribute>();
    }
}

最后,您的控制器将从:

public class HomeController : Controller
{
    public ActionResult Foo()
    {
        ...
    }

    ...
}

至:

public class HomeController : Controller
{
    private IMemoryCacheService memoryCacheService;

    public HomeController(IMemoryCacheService memoryCacheService)
    {
        this.memoryCacheService = memoryCacheService;
    }

    public ActionResult Foo()
    {
        // use this.memoryCacheService in your controller methods...
    }

    ...
}

假设您IEmailService按照上述策略创建了另一项服务,并且您希望IEmailService也可以使用HomeController,那么:

public class HomeController : Controller
{
    private IMemoryCacheService memoryCacheService;
    private IEmailService emailService;

    public HomeController(IMemoryCacheService memoryCacheService, IEmailService emailService)
    {
        this.memoryCacheService = memoryCacheService;
        this.emailService = emailService;
    }

    public ActionResult Foo()
    {
        // use this.memoryCacheService in your controller methods...
        // and also use this.emailService in your controller methods...
    }

    ...
}

Ninject 将更改 ASP.NET MVC 控制器工厂以自动将注入的参数提供给控制器构造函数。

我认为从长远来看,这种方法比保留全局变量等更好。

于 2013-02-15T16:50:33.167 回答
10

MemoryCache您正在每个控制器中创建一个新实例。由于它是新的,因此其中没有任何内容,这就是为什么您的值始终为空的原因。您需要访问您在 中创建的同一实例Application_Start。研究使用MemoryCache.Default.

于 2013-02-15T16:32:34.483 回答