Caching in Class Libraries (c#)

  • Thread starter Thread starter Mat
  • Start date Start date
M

Mat

I have a class library in which I need to access a XML Document often
which is stored as a text xml file. Obviously, I therefore would like
to cache this XML Document as reading this into an XML Document type
each time its required wouldn't be efficient. Does anyone have any
pointers on this.

There is a Cache object in System.Web but by default this isn't
available to class libraries, it it worth adding the reference into
the component?

I've also looked into the Microsoft Blocks and see that there is an
Cache Block which may also help be in my quest.
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/CachingBlock.asp)

However, having never done anything along these lines, any experience
is gratefully welcome.

Cheers in advance

Mat
 
I have a class library in which I need to access a XML Document often
which is stored as a text xml file. Obviously, I therefore would like
to cache this XML Document as reading this into an XML Document type
each time its required wouldn't be efficient. Does anyone have any
pointers on this.

Can the XML source change while the application is running? If not, is
there any reason why you don't simply keep a reference to the
XmlDocument object for as long as you need it?



Mattias
 
Mat said:
I have a class library in which I need to access a XML Document often
which is stored as a text xml file. Obviously, I therefore would like
to cache this XML Document as reading this into an XML Document type
each time its required wouldn't be efficient. Does anyone have any
pointers on this.

There is a Cache object in System.Web but by default this isn't
available to class libraries, it it worth adding the reference into
the component?

Such caches are mostly useful in a stateless environment, such as the web,
where they are used to simulate state. In your case you should probably use
a singleton. A good tutorial on singletons in C#, written by Jon Skeet, can
be found here: http://www.yoda.arachsys.com/csharp/singleton.html

Regards /Magnus
 
Mattias Sjögren said:
Can the XML source change while the application is running? If not, is
there any reason why you don't simply keep a reference to the
XmlDocument object for as long as you need it?

I'd say that's the way to do it even if it may change. If the file in
question
lives on the filesystem you can simply use System.IO.FileSystemWatcher and
basic thread synchronization to keep it up to date. If the document lives
somewhere else you can use a System.Threading.Timer and some custom code to
monitor for updates.

Regards /Magnus
 
Mattias Sjögren said:
Can the XML source change while the application is running? If not, is
there any reason why you don't simply keep a reference to the
XmlDocument object for as long as you need it?

I'd say that's the way to do it even if it may change. If the file in
question
lives on the filesystem you can simply use System.IO.FileSystemWatcher and
basic thread synchronization to keep it up to date. If the document lives
somewhere else you can use a System.Threading.Timer and some custom code to
monitor for updates.

Regards /Magnus
 
Sorry about the double-post. Outlook Express went postal when i clicked
send, complaing about not being able to delete the file, and my kneejerk
reaction was to press send again.

/Magnus
 
Back
Top