代码之家  ›  专栏  ›  技术社区  ›  Ron Klein Noa Kuperberg

如何在控制台应用程序中使用system.web.caching.cache?

  •  29
  • Ron Klein Noa Kuperberg  · 技术社区  · 16 年前

    上下文:.net 3.5,c#
    我希望在控制台应用程序中有缓存机制。
    我不想重新发明轮子,而是想用 System.Web.Caching.Cache (这是最后的决定,我不能使用其他缓存框架,不要问为什么)。
    不过,看起来 system.web.caching.cache缓存 应该只在有效的HTTP上下文中运行。我非常简单的一段代码如下:

    using System;
    using System.Web.Caching;
    using System.Web;
    
    Cache c = new Cache();
    
    try
    {
        c.Insert("a", 123);
    }
    catch (Exception ex)
    {
        Console.WriteLine("cannot insert to cache, exception:");
        Console.WriteLine(ex);
    }
    

    结果是:

    cannot insert to cache, exception:
    System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Web.Caching.Cache.Insert(String key, Object value)
       at MyClass.RunSnippet()
    

    所以很明显,我在这里做错事。有什么想法吗?


    更新 :+1对于大多数答案,通过静态方法获取缓存是正确的用法,即 HttpRuntime.Cache HttpContext.Current.Cache . 谢谢大家!

    6 回复  |  直到 14 年前
        1
  •  56
  •   Amit G    16 年前

    缓存构造函数的文档说明它仅供内部使用。要获取缓存对象,请调用httpruntime.cache,而不是通过构造函数创建实例。

        2
  •  28
  •   Joel Fillmore    16 年前

    虽然OP指定了v3.5,但在v4发布之前就提出了这个问题。帮助任何发现这个问题的人 框架团队可以使用v4依赖项,为这种类型的场景创建了一个新的通用缓存。它在system.runtime.caching命名空间中: http://msdn.microsoft.com/en-us/library/dd997357%28v=VS.100%29.aspx

    对默认缓存实例的静态引用为:memorycache.default

        3
  •  9
  •   RichardOD    16 年前

    只使用 Caching Application Block 如果你不想重新发明轮子。如果仍要使用ASP.NET缓存- see here . 不过,我很确定这只适用于.NET 2.0及更高版本。在.NET 1中,不可能使用ASP.NET外部的缓存。

    对于缓存文档,msdn在页面上也有一个很好的大警告:

    缓存类不适用于 在ASP.NET应用程序之外使用。 它设计和测试用于 ASP.NET为Web提供缓存 应用。其他类型的 应用程序,如控制台 应用程序或Windows窗体 应用程序,ASP.NET缓存可能 工作不正常。

    对于非常轻量级的解决方案,不必担心过期等问题,那么字典对象就足够了。

        4
  •  4
  •   stealthyninja michkra    14 年前

    我在这一页结束时也在想同样的事情。以下是我正在做的(我不喜欢,但似乎工作得很好):

    HttpContext context = HttpContext.Current;
    if (context == null)
    {
        HttpRequest request = new HttpRequest(string.Empty, "http://tempuri.org", string.Empty);
        HttpResponse response = new HttpResponse(new StreamWriter(new MemoryStream()));
        context = new HttpContext(request, response);
        HttpContext.Current = context;
    }
    this.cache = context.Cache;
    
        5
  •  1
  •   Hans Malherbe    16 年前

    尝试

    public class AspnetDataCache : IDataCache
    {
        private readonly Cache _cache;
    
        public AspnetDataCache(Cache cache)
        {
            _cache = cache;
        }
    
        public AspnetDataCache()
            : this(HttpRuntime.Cache)
        {
    
        }
        public void Put(string key, object obj, TimeSpan expireNext)
        {
            if (key == null || obj == null)
                return;
            _cache.Insert(key, obj, null, DateTime.Now.Add(expireNext), TimeSpan.Zero);
        }
    
        public object Get(string key)
        {
            return _cache.Get(key);
        }
    
    
        6
  •  1
  •   d4nt    16 年前

    system.web.caching.cache类依赖于由httpruntime对象设置其成员“cacheInternal”。

    要使用system.web.caching类,您必须创建一个httpruntime对象并设置httpruntime.cache属性。实际上,你必须模仿IIS。

    最好使用其他缓存框架,如: