ASP.net缓存+ Singleton模式 [英] ASP.net Cache + Singleton Pattern

查看:80
本文介绍了ASP.net缓存+ Singleton模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我必须分析它产生域对象一个巨大的XML文档。

由于该文件是巨大的,我不希望每一个用户请求的时间,但只有第一次解析,然后所有的对象保存到缓存中。

 公开名单<产品与GT; GetXMLProducts()
{
    如果(HttpRuntime.Cache.Get(ProductsXML)!= NULL)
    {
        回报(名单<产品与GT;)(HttpRuntime.Cache.Get(ProductsXML));
    }    字符串xmlpath中= Path.Combine(AppDomain.CurrentDomain.BaseDirectory,内容\\\\ Products.xml);
    读者的XmlReader = XmlReader.Create(xmlpath中);
    DOC的XDocument = XDocument.Load(读卡器);    清单<产品与GT;产品列表=新的List<产品及GT;();
    //解析产品元素    HttpRuntime.Cache.Insert(ProductsXML产品列表);
    返回产品列表;
}

怎么是我能够做到这个功能在单工作,是线程安全的最佳方式?

固定储蓄的对象为高速缓存方法(是复制粘贴错误)


解决方案

创建一个懒人静态和保存在内存中的应用程序的生命周期。而且不要忘了真实的一部分,这是什么使得它线程安全的。

 公共静态只读懒<名单,LT;产品与GT;> _product =新懒人<名单,LT;产品>>(()=>的GetProducts(),TRUE);

要添加到您的模型,只是让私人和返回_product.Value;

 公开为MyModel
{
    ...一堆方法/属性    私人静态只读懒<名单,LT;产品与GT;> _产品展示=新懒人<名单,LT;产品>>(()=>的GetProducts(),TRUE);    私人静态列表<产品与GT;的GetProducts()
    {
        返回DsLayer.GetProducts();    }    公开名单<产品与GT;产品{{返回_products.Value; }}
}

要创建一个使用懒℃的单;>,使用这个模式

 公共MyClass的
{
    私人静态只读懒< MyClass的> _myClass =新懒人< MyClass的>(()=>新建MyClass的(),TRUE);    私人MyClass的(){}    公共静态MyClass的实例{{返回_myClass.Value; }}
}


  

更新/编辑:


另一个偷懒模式使用的范围内(即会话)

有些型号保存在会话:

 公开为MyModel
{
   私人列表<产品与GT; _currentProducts = NULL;
   公开名单<产品与GT; CurrentProducts
   {
      得到
      {
         返回this._currentProducts? (_currentProducts = ProductDataLayer.GetProducts(this.CurrentCustomer));
      }
   }
}

I have a huge XML document which I have to parse it to generate domain objects.

Because the document is huge, i don't want to parse it every time a user requests it, but only first time, then saving all the objects into cache.

public List<Product> GetXMLProducts()
{
    if (HttpRuntime.Cache.Get("ProductsXML") != null)
    {
        return (List<Product>)(HttpRuntime.Cache.Get("ProductsXML"));
    }

    string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\Products.xml");
    XmlReader reader = XmlReader.Create(xmlPath);
    XDocument doc = XDocument.Load(reader);

    List<Product> productsList = new List<Product>();
    // Parsing the products element

    HttpRuntime.Cache.Insert("ProductsXML", productsList);
    return productsList;
}

How is the best way i can make this function working in singleton and be thread-safe?

Fixed the saving an object into cache method (was a copy-paste mistake)

解决方案

Create a Lazy static and keep in memory for the lifetime of the application. And don't forget the "true" part, that's what makes it thread safe.

public static readonly Lazy<List<Product>> _product = new Lazy<List<Products>>(() => GetProducts(), true);

To add this to your model, just make it private and return _product.Value;

public MyModel
{
    ... bunch of methods/properties

    private static readonly Lazy<List<Product>> _products = new Lazy<List<Products>>(() => GetProducts(), true);

    private static List<Product> GetProducts()
    {
        return DsLayer.GetProducts();

    }

    public List<Product> Products { get { return _products.Value; } }
}

To create a singleton using Lazy<>, use this pattern.

public MyClass
{
    private static readonly Lazy<MyClass> _myClass = new Lazy<MyClass>(() => new MyClass(), true);

    private MyClass(){}

    public static MyClass Instance { get { return _myClass.Value; } }
}

Update/Edit:

Another lazy pattern for use within a context (i.e. Session)

Some Model that is saved in Session:

public MyModel
{
   private List<Product> _currentProducts = null;
   public List<Product> CurrentProducts 
   {
      get
      {
         return this._currentProducts ?? (_currentProducts = ProductDataLayer.GetProducts(this.CurrentCustomer));
      }
   }
}

这篇关于ASP.net缓存+ Singleton模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆