5

Caching data in asp.net is very easy. So I just want to know how could I cache data in win application.

Suppose that I want to fetch & cache data from employee table and when ever any records will be inserted or updated in employee table then a event will fire in my form and from there I will reload those data from employee table again and cache it. It is very easy to implement in asp.net apps but how to implement this concept in win application. I don't want to use timer. please help me with concept ?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Thomas
  • 33,544
  • 126
  • 357
  • 626

3 Answers3

8

If you can target .NET 4 then you should take a look at the System.Runtime.Caching namespace, and the MemoryCache class in particular:

The MemoryCache class is similar to the ASP.NET Cache class. The MemoryCache class has many properties and methods for accessing the cache that will be familiar to you if you have used the ASP.NET Cache class. The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed to make it usable by .NET Framework applications that are not ASP.NET applications. For example, the MemoryCache class has no dependencies on the System.Web assembly. Another difference is that you can create multiple instances of the MemoryCache class for use in the same application and in the same AppDomain instance.

LukeH
  • 263,068
  • 57
  • 365
  • 409
2

This would be easy if you use Microsoft application block, you may want to explore more Caching Application Block http://msdn.microsoft.com/en-us/library/ff647280.aspx

You may want to look into specifically - Memcached with Windows and .NET

Community
  • 1
  • 1
Kumar
  • 997
  • 5
  • 8
  • I'm not going to down vote the answer, but I hate the application block code. I've looked at it before but chose to write my own in the past. – RQDQ Mar 01 '11 at 14:13
1

Memcache is a popular solution. There are some .NET ports:

Is there a port of memcache to .Net?

Community
  • 1
  • 1
RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • it will be possible to reload the cache if underlying data changes with the help of memcache? please help. – Thomas Mar 01 '11 at 14:18
  • 2
    This is not a good idea to use the memcached in desktop application – Viacheslav Smityukh Mar 01 '11 at 14:24
  • 1
    @Viacheslav Smityukh - why? You might have a good point, but it's hard to tell without supporting information. – RQDQ Mar 01 '11 at 14:33
  • @Thomas - You will have to build in the logic for either notifying clients that the underlying data has changed, or you can just depend on an expiration time for each cached value. This is the same for just about any caching provider. – RQDQ Mar 01 '11 at 14:35