32

I understand the .NET 4 Framework has caching support built into it. Does anyone have any experience with this, or could provide good resources to learn more about this?

I am referring to the caching of objects (entities primarily) in memory, and probably the use of System.Runtime.Caching.

Hosea146
  • 7,412
  • 21
  • 60
  • 81
  • 10
    Caching of what? Are you referring to Entity Framework, WCF or an application server? – weismat May 12 '11 at 13:14
  • `System.Runtime.Caching` obviously ;-) – Jodrell May 12 '11 at 13:30
  • You should be aware of the following bug if you haven't yet upgraded to .NET 4.5: http://stackoverflow.com/a/15715990/13087 – Joe Oct 11 '13 at 18:13
  • [Robust .NET caching](http://www.codeducky.org/robust-net-caching/) explains how to avoid common pitfalls when caching and specifically uses [Memory Cache](http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache(v=vs.110).aspx) in the example it provides. – Steven Wexler May 16 '14 at 03:20

5 Answers5

27

I assume you are getting at this, System.Runtime.Caching, similar to the System.Web.Caching and in a more general namespace.

See http://deanhume.com/Home/BlogPost/object-caching----net-4/37

and on the stack,

is-there-some-sort-of-cachedependency-in-system-runtime-caching and,

performance-of-system-runtime-caching.

Could be useful.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124
15

I've not made use of it myself, but if you're just caching simple objects in memory, you're probably referring to the MemoryCache class, in the System.Runtime.Caching namespace. There is a little example of how to use it at the end of the page.

Edit: To make it look like I've actually done some work for this answer, here's the sample from that page! :)

private void btnGet_Click(object sender, EventArgs e)
{
    ObjectCache cache = MemoryCache.Default;
    string fileContents = cache["filecontents"] as string;

    if (fileContents == null)
    {
        CacheItemPolicy policy = new CacheItemPolicy();

        List<string> filePaths = new List<string>();
        filePaths.Add("c:\\cache\\example.txt");

        policy.ChangeMonitors.Add(new 
        HostFileChangeMonitor(filePaths));

        // Fetch the file contents.
        fileContents = 
            File.ReadAllText("c:\\cache\\example.txt");

        cache.Set("filecontents", fileContents, policy);
    }

    Label1.Text = fileContents;
}

It's interesting because it shows that you can apply dependencies to the cache, much like in the classic ASP.NET cache. The big difference here is that you don't have a dependency on the System.Web assembly.

Mike Goatly
  • 7,380
  • 2
  • 32
  • 33
  • 1
    I have used this and found the documentation to be lacking. It's my understanding that you can use it basically the same as you would System.Web caching. – goalie7960 May 12 '11 at 13:41
5

MemoryCache in the framework is a good place to start, but you might also like to consider LazyCache because it has a simpler API than memory cache and has built in locking as well as some other nice features. It is available on nuget: PM> Install-Package LazyCache

// Create our cache service using the defaults (Dependency injection ready).
// Uses MemoryCache.Default as default so cache is shared between instances
IAppCache cache = new CachingService();

// Declare (but don't execute) a func/delegate whose result we want to cache
Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources();

// Get our ComplexObjects from the cache, or build them in the factory func 
// and cache the results for next time under the given key
ComplexObject cachedResults = cache.GetOrAdd("uniqueKey", complexObjectFactory);

I recently wrote this article about getting started with caching in dot net that you may find useful.

(Disclaimer: I am the author of LazyCache)

alastairtree
  • 3,960
  • 32
  • 49
2

Hope you are referring to System.Runtime.Caching of .Netframework 4.0

Below link is the good starting point: Here

Kishore Borra
  • 269
  • 3
  • 15
0

MSDN article "ASP.NET Caching: Techniques and Best Practices" is a great start.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
AD.Net
  • 13,352
  • 2
  • 28
  • 47