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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • When Caches Collide: Solving Race Conditions in Fare Updates
  • When Caching Goes Wrong: How One Misconfigured Cache Took Down an Entire System
  • Creating a Web Project: Caching for Performance Optimization
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load

Trending

  • The Death of REST? Why gRPC and GraphQL Are Taking Over
  • Secret Recipe of the Template Method: Po Learns the Art of Structured Cooking
  • The Shift to Open Industrial IoT Architectures With Data Streaming
  • Event Storming Workshops: A Closer Look at Different Approaches
  1. DZone
  2. Data Engineering
  3. Data
  4. Caching With WCF Services

Caching With WCF Services

By 
Pieter De Rycke user avatar
Pieter De Rycke
·
Apr. 13, 12 · Interview
Likes (1)
Comment
Save
Tweet
Share
21.5K 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.

Related

  • When Caches Collide: Solving Race Conditions in Fare Updates
  • When Caching Goes Wrong: How One Misconfigured Cache Took Down an Entire System
  • Creating a Web Project: Caching for Performance Optimization
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: