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.
.NET ProgrammingC#

Avatar of undefined
Last Comment
ravisrivastava16

8/22/2022 - Mon
daveamour

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

Thanks
ASKER
ravisrivastava16

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.
Anurag Thakur

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
DropZone

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.
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
ASKER
ravisrivastava16

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
daveamour

Just store it in a variable whose scope is great enogh for your programs needs.
DropZone

The links that MuhammadAdil offered seem promising for what you want to do.

     -dZ.
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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
ravisrivastava16

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
marufdolani

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Anurag Thakur

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
ASKER
ravisrivastava16

It is a windows application, and it is required to implement cache.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
daveamour

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
Anurag Thakur

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.
daveamour

I agree raq but he said it has to implement a cache:
"It is a windows application, and it is required to implement cache. "
 
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
ravisrivastava16

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.