Skip to content

Instantly share code, notes, and snippets.

@mykeels
Last active December 18, 2016 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykeels/fe33b34976199c4d68125e02b8d44dd7 to your computer and use it in GitHub Desktop.
Save mykeels/fe33b34976199c4d68125e02b8d44dd7 to your computer and use it in GitHub Desktop.
Easy way to work with `System.Web.Caching` when building .NET C# Web Applications

While Caching in .NET is a thing of beauty, using it can be a bit cumbersome, and makes my code look clunky. I'm talking about usage like:

  if (Cache.Get("myKey") == null) {
    Cache.Insert("myKey", getValue());
    return Cache.Get("myKey");
  }

The class in Cache.cs contains helper methods that make this much easier and cleaner to do

  Cache.Get<object>("myKey", () => getValue());

This simply means, return the value in the myKey key in cache if it exists. If not, execute getValue() and insert it as myKey key in the cache.

Also, Cache.Clear() should come in handy too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment