Caching With WCF Services
Join the DZone community and get the full member experience.
Join For FreeThis is the first part of a two part article about caching in WCF services. In this part I will explain the in-process memory cache available in .NET 4.0. In the second part I will describe the Windows AppFabric distributed memory cache.
The .NET framework has provided a cache for ASP.NET applications since version 1.0. For other types of applications like WPF applications or console application, caching was never possible out of the box. Only WCF services were able to use the ASP.NET cache if they were configured to run in ASP.NET compatibility mode. But this mode has some performance drawbacks and only works when the WCF service is hosted inside IIS and uses an HTTP-based binding.
With the release of the .NET 4.0 framework this has luckily changed. Microsoft has now developed an in-process memory cache that does not rely on the ASP.NET framework. This cache can be found in the “System.Runtime.Caching.dll” assembly.
In order to explain the working of the cache, I have a created a simple sample application. It consists of a very slow repository called “SlowRepository”.
public class SlowRepository { public IEnumerable GetPizzas() { Thread.Sleep(10000); return new List() { "Hawaii", "Pepperoni", "Bolognaise" }; } }
This repository is used by my sample WCF service to gets its data.
public class PizzaService : IPizzaService { private const string CacheKey = "availablePizzas"; private SlowRepository repository; public PizzaService() { this.repository = new SlowRepository(); } public IEnumerable GetAvailablePizzas() { ObjectCache cache = MemoryCache.Default; if(cache.Contains(CacheKey)) return (IEnumerable)cache.Get(CacheKey); else { IEnumerable availablePizzas = repository.GetPizzas(); // Store data in the cache CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0); cache.Add(CacheKey, availablePizzas, cacheItemPolicy); return availablePizzas; } } }
When the WCF service method GetAvailablePizzas is called, the service first retrieves the default memory cache instance
ObjectCache cache = MemoryCache.Default;
Next, it checks if the data is already available in the cache. If so, the cached data is used. If not, the repository is called to get the data and afterwards the data is stored in the cache.
For my sample service, I also choose to restrict the maximum memory to 20% of the total physical memory. This can be done in the web.config.
<system.runtime.caching> <memoryCache> <namedCaches> <add name="Default" physicalMemoryLimitPercentage="20"/> </namedCaches> </memoryCache> </system.runtime.caching>
Opinions expressed by DZone contributors are their own.
Comments