Reputation: 3814
I have the following, very interesting class ...
pubilc class Thing{
public Thing()
{
DoSomethingThatTakesALongTime();
}
public boolean CheckSomething(string criteria)
{
return criteria == "something";
} }
In my ASP.Net MVC application, I need to make a call to CheckSomething
very frequently.
As you can see, the constructor takes a long time to load.
What approaches can I use to cache the class? Keep in mind that I want to keep it testable ... and I don't know what that entails!!!!
Cheers,
ETFairfax
Upvotes: 0
Views: 193
Reputation: 65391
You could use the Factory Pattern to create it (Flyweight pattern to share memory)
Create a Factory that returns an instance of the class. The factory would look like this
if instance in cache
if not
For cache you could use HttpContext Cache or Enterprise Library Cache.
EDIT
Interesting discussion below of which pattern this is.
My understanding is as follows:
Upvotes: 2
Reputation: 867
if your thing class does something global to your aplication (not session dependant) just make the class static or add the instance to the cache (HttpContext.Cache)
Upvotes: 0
Reputation:
You can create a static instance of that class.
Somewhere:
public static Thing singleThing = new Thing ();
Now upon the first access of the singleThing variable for a given application domain your constructor will work out. After this is done the object will be kept in memory until the end of the application domain (server restart, update of code, changes in web.config, recycling etc.). It means the once initialized object will be available to all clients of your application.
Upvotes: 2