简体   繁体   English

如何在ASP.NET缓存中存储类实例(帮助程序类)?

[英]How to store class instance (helper class) in ASP.NET Cache?

I have a class instance which is created by using Activator.CreateInstance() method. 我有一个使用Activator.CreateInstance()方法创建的类实例。 That class instance contains helper methods which are frequently used throughout the application. 该类实例包含在整个应用程序中经常使用的辅助方法。 In order to avoid creating the instance multiple times, I'm just thinking about implementing an appropriate caching mechanism. 为了避免多次创建实例,我只是在考虑实现适当的缓存机制。

The following consideration should be taken into account: 应考虑以下因素:

1) I can't use static class and methods. 1)我不能使用静态类和方法。

2) There are around 6 instances (1 instance per class) per App Domain. 2)每个App Domain大约有6个实例(每个类1个实例)。

Your suggestion would be much appreciated! 您的建议将不胜感激!

If you want to avoid creating it multiple times, then don't use the ASP.Net cache object. 如果要避免多次创建它,请不要使用ASP.Net缓存对象。 The cache object specifically does not guarantee that anything you put in it will remain there. 缓存对象明确地不保证您放入的任何内容都将保留在那里。 In fact it's one of the first things to be cannibalized if the server needs to free up resources. 实际上,如果服务器需要释放资源,这是首先要被蚕食的事情之一。

A better option would be to use the HttpApplicationState object, which should be used to store objects that need to be globally accessible to all sessions. 更好的选择是使用HttpApplicationState对象,该对象应用于存储所有会话都需要全局访问的对象。 It also has built in thread safety if you access it properly. 如果正确访问它,它也具有内置的线程安全性。

The code to do it is as follows: 要做的代码如下:

HttpContext.Current.Application.Lock();
HttpContext.Current.Application["myObject"] = myObject;
HttpContext.Current.Application.Unlock();

Utilizing it is just 利用它只是

var myObject = (MyObject)HttpContext.Current.Application["myObject"];

Use a singleton pattern: 使用单例模式:

class MySingleton {

     private static MySingleton instance;

     public MySingleton {
          if(instance != null)
              // One already created, the only call to this
              // should come through Activator
              throw...
          instance = this;
     }

     public static MySingleton GetInstance() {
          if(instance == null) instance = new MySingleton();
          return instance;
     }

}

The activator uses the public constructor. 激活器使用公共构造函数。 Then you can still retrieve the instance through GetInstance() . 然后,您仍然可以通过GetInstance()检索实例。

Ad 1) How about a static container for your instance? 广告1)实例的静态容器如何? Along the lines of a singleton pattern? 遵循单例模式?

Ad 2) 6 singletons or one static generic singleton class. 广告2)6个单例或一个静态通用单例类。

PS: I guess the static restriction is meant only for the helper class itself? PS:我猜想静态限制仅适用于助手类本身吗?

PPS: Using HttpContext.Current.Application would be pretty much the same approach, except slower. PPS:使用HttpContext.Current.Application的方法几乎相同,只是速度较慢。

Sounds like a case for a dependency injection container. 听起来像依赖注入容器的情况。 No matter which one you pick, they all have support for caching like a singleton, and it will do the Activator.CreateInstance part for you. 无论您选择哪一个,它们都像单例一样支持缓存,它将为您完成Activator.CreateInstance部分。

I like NInject for it's simplicity. 我喜欢NInject,因为它很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM