Link to home
Start Free TrialLog in
Avatar of ravisrivastava16
ravisrivastava16Flag for India

asked on

Caching file information in C# windows application

I have to implement caching in C# windows application. I have to cache some file information till a particular session and in between invalidate caching value with user input.

Please guide that what can be the best way to implement cache in for this type of C# windows application.
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you rephrase your question, it doesn't read too clearly.

Thanks
Avatar of ravisrivastava16

ASKER

Basically i have to cache some File information (header and data) in a C# windows based application.
i.e. to store some file information in Cache for a particualr time.
its a windows based application then i am assuming that you are going to use MDI forms...
if yes then create a class in which you can cache your file information and create the object of the same class in the MDI form..
when the file is read you can code to save the information to the object in the MDI form and later when you need to invalidate the information that can also be done easily
I'm sorry, but I still don't understand what you mean.  You mentioned sessions and windows application, but WinForms applications do not have Session State.

Can you explain with more details what it is that you want to cache and what do you mean by "cache"--do you mean you want the application to store data so that when the application is closed and then re-opened, it continues? Or do you want to store temporary data in memory so that it doesn't have to be computed or retrieved again (say, from a database)?

Please be more specific and we'll do our best to assist you.

     -dZ.
Avatar of MuhammadAdil
Hi,
    May be these links can solve your problem.

http://netcode.ru/dotnet/?lang=&katID=30&skatID=283&artID=7851
http://www.codeproject.com/KB/cs/cacheinwinformapps.aspx

Regards,
Adil Fazal
Relax Solutions
I want to store temporary data in memory so that it doesn't have to be computed or retrieved again. But there is not any database to store it. However, I can use XML.
Just store it in a variable whose scope is great enogh for your programs needs.
The links that MuhammadAdil offered seem promising for what you want to do.

     -dZ.
Avatar of marufdolani
marufdolani

There are two ways to implement it -
1. If you need a simple cache, you can have a static Hashtable and store your object against a key (on application startup). If there is an event, when the data changes, you update the value of that particular key. This is the most simple scenario for caching.

2. You can also use Microsoft Enterprise Library Caching Application Block - It contains code samples as well and provides advanced features time cache duration,invalidating routine etc.

I think option #1 should suffice for you. Let me know if you need code sample for above.

Thanks,
Maruf
private static Hashtable _cache = new Hashtable();
 
#region Cache Lookup
public static object GetCachedObject(string key)
{
   object retVal;
   if(!string.IsNullOrEmpty(key)
   {
      if(_cache.ContainsKey(key)
      {
          retVal = _cache[key];
      }
      else
      {
         //Not in cache. Get from physical location (computation) and Add to cache
          retVal = LoadCache(key);
      }
 
   }
   
   return retVal;
}
#endregion
 
#region Initialize Cache
  private static object LoadCache(string key)
  {     
    //If you have multiple items to be cached, call approptiate data
    // load logic and get the object in cache.
    switch(key)
    {
      case "CustomerData": 
              DataSet ds = new DataSet();
              ds = GetCustomerDataFromXml();
              if(_cache.ContainsKey(key)
              {
                 _cache[key] = ds;
              }
              else
              {
                 _cache.Add(key, ds);
              }
              break;
    }
 
    return _cache[key];
  }
#endregion

Open in new window

I want to use Microsoft Enterprise Library Caching Application Block . How can i use it to store file information. Plaes provide some sample code.
ASKER CERTIFIED SOLUTION
Avatar of marufdolani
marufdolani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
please let us know clearly whether its a windows application or a web application and the caching requirements depend on the type of application
if its a web applicaiton then definitely the cache is a very good idea if - the data has to be shared among all the web application users
if its a windows applicaion then the same can be achieved without using caching applicaion block as it can be easily done with a variable
It is a windows application, and it is required to implement cache.
If you wanted you could code a singleton and build in your own time based expiry.

Or is this any good:

http://www.codeproject.com/KB/cs/cacheinwinformapps.aspx
personally after designed so many windows applications i have never come across a scenario where i will be implementing cache in the applicaiton where i can achieve the same by just using the object as i defined before - the reason is becasue the windows application is a stand alone application where the application is not sharing any kind of information wiht the other instances of the application

in case of a web application it makes a lot fo sense because the cache is maintained on the server and multiple users across the world can access the same information - from the cache.
I agree raq but he said it has to implement a cache:
"It is a windows application, and it is required to implement cache. "
 
I have to read File information from a smart card and store it in cache then show it to user in form of the tree structure.