DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Caching With WCF Services

Pieter De Rycke user avatar by
Pieter De Rycke
·
Apr. 13, 12 · · Interview
Like (1)
Save
Tweet
20.07K Views

Join the DZone community and get the full member experience.

Join For Free

This 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>
Windows Communication Foundation Cache (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Enough Already With ‘Event Streaming’
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • SDLC Vs STLC: What's the Difference?
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo