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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Data Engineering
  3. Data
  4. Caching In WCF Services: Part 2 - AppFabric Distributed Cache

Caching In WCF Services: Part 2 - AppFabric Distributed Cache

Pieter De Rycke user avatar by
Pieter De Rycke
·
Aug. 28, 12 · Interview
Like (0)
Save
Tweet
Share
10.69K Views

Join the DZone community and get the full member experience.

Join For Free

introduction

a couple of months ago, i wrote an article about caching in wcf using the in-process memory cache available in the .net 4.0 framework. this component offers a lightweight caching solution that can easily be integrated in various types of .net applications.

there are scenarios where a more advanced caching solution is required. the standard .net 4.0 cache cannot be shared among multiple servers. every server has its own isolated instance of the cache. furthermore, it shares memory with the application. a 32-bit application can allocate at maximum 4 gb of ram (on a 64-bit os; maximum 2gb of ram on a 32-bit os). there can be situations where you want that the cache of a 32-bit app is larger than 2 gb (although these situations are rare).

appfabric

microsoft offers a distributed cache as part of windows server appfabric. this cache can be installed on window server 2008 and higher. it is also available in the cloud on windows azure. the appfabric cache is a distributed out of process cache; this means that the cache can be shared among multiple servers and applications. because it runs outside the application process, it can survive application shut downs and it can be larger then 4 gb for 32-bit applications.

you can deploy appfabric in two ways on your network: on every application server hosting your application or as a separate infrastructure layer independent of your application servers. the lather provides more flexibility in terms of being able to scale your caching and application server layers independent of each other, but it comes at a higher cost of servers and an increased latency to access the cache.

caching_options

configuring the appfabric caching server

appfabric can be download for free from the microsoft download site. at the moment of writing, the latest version available is appfabric 1.1. it requires at least windows vista or windows server 2008. for testing the appfabric cache, i have setup a windows server 2008 r2 vm configured in workgroup modus. i choose to deploy my wcf service, on a separate server.

prior to the installation of appfabric, i created a technical user account called “appfabriccacheuser” that will be used to run the caching service. i also created a network share called “appfabriccache”. if you would use multiple servers, the same user account must be created on all the servers. these user accounts must have the same password on all servers. only one server must host a network share for the appfabric cache. the appfabric user account must have admin rights on the server hosting the share.

please note: the configuration of appfabric is different in case your servers are part of an ad domain. then you don’t need to setup a network share or separate technical accounts on each server. most companies should have an ad domain. please consult the appfabric msdn pages for more information. i recommend to only use appfabric caching in workgroup mode in a sandboxed test environment. otherwise, always use it in ad mode if possible.

appfabric consists of two parts, the hosting features and the distributed cache. because this article is about caching, i only installed the necessary caching features.

setup

after the installation is finished, we must launch the “appfabric server configuration wizard”. we must specify the caching service account we created earlier, the xml caching service configuration provider and the file share we created earlier.

configure_workgroup_modus

after this is done, press “next” to continue to following screen. on this screen, we can change the network ports if needed. i choose to stick with the defaults.

configure_cache_node

now this is done, we can start our cache using the powershell. in order to do this, launch the “caching administration windows powershell” and enter the following command: start-cachecluster .

by default, security is enabled on the cache. so we must grant access to our client. because i run in workgroup modus, i must create an account on my server with the exact same user name and password as will be used by my wcf service on the appfabric caching client. granting access to a user account can be done with the following command: grant-cacheallowedclientaccount -account “youruseraccountname” .

please note: in case of an ad domain is available, the user account must not be created on the server. it is sufficient that the account is known in ad.

powershell_cmdlets

configuring the appfabric caching client

on the client, the client at least the “cache client” of appfabric must be installed. no special configuration is required.

setup_client

developing the wcf service

for the sake of simplicity, i kept the actual programming code to the strict minimum. the sample wcf application consists of two components: a (slow) repository and a wcf service.

first we must add a reference to two dll’s installed by the appfabric installer. they can be found in the installation folder of appfabric.

vs_references

the slow repository is called: slowrepository (very creative naming; i know…)

public class slowrepository
{
    public ienumerable<string> getpizzas()
    {
        thread.sleep(10000);

        return new list<string>() { "hawaii", "pepperoni", "bolognaise" };
    }
}

i kept the wcf service very basic. it creates an instance of the datacache object inside its constructor. this object offers an interface to the distributed cache. in order to create it, we must provide the addresses and network ports of the servers hosting the distributed cache.

public class pizzaservice : ipizzaservice
{
    private const string cacheservername = "win-53k8o7pmtop";
    private const int cacheserverport = 22233;
    private const string cachekey = "availablepizzas";

    private datacache cache;
    private slowrepository repository;

    public pizzaservice()
    {
        // setup appfabric cache
        list<datacacheserverendpoint> servers = new list<datacacheserverendpoint>();
        servers.add(new datacacheserverendpoint(cacheservername, cacheserverport));

        datacachefactoryconfiguration configuration = new datacachefactoryconfiguration();
        configuration.servers = servers;

        //set default properties for local cache (local cache disabled)
        configuration.localcacheproperties = new datacachelocalcacheproperties();
        configuration.securityproperties = 
            new datacachesecurity(datacachesecuritymode.none, datacacheprotectionlevel.none);

        datacachefactory cachefactory = new datacachefactory(configuration);
        this.cache = cachefactory.getcache("default"); // get the default cache

        // create our 'slow' repository
        this.repository = new slowrepository();
    }

    public ienumerable<string> getavailablepizzas()
    {
        ienumerable<string> availablepizzas = null;

        // try to get the item from the cache
        availablepizzas = (ienumerable<string>)cache.get(cachekey);

        if (availablepizzas != null)
            return availablepizzas;
        else
        {
            availablepizzas = repository.getpizzas();

            // store data in the cache
            cache.put(cachekey, availablepizzas);

            return availablepizzas;
        }
    }
}

if you call the service twice, you will notice that the second time the data is taken from the cache instead of from the repository. if we restart the web service, the data is still taken from the cache. if we execute the powershell command “ get-cachestatistics ” we can see that the cache contains our data.

powershell_cachestatistics

Cache (computing) Web Service Distributed cache Windows Communication Foundation application 32-bit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Complete Guide to AngularJS Testing
  • How to Develop a Portrait Retouching Function
  • How To Use Terraform to Provision an AWS EC2 Instance
  • Why Every Fintech Company Needs DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: