Caching In WCF Services: Part 2 - AppFabric Distributed Cache
Join the DZone community and get the full member experience.
Join For Freeintroduction
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.
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.
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.
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.
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.
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.
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.
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.
Opinions expressed by DZone contributors are their own.
Trending
-
What Is JHipster?
-
Developers Are Scaling Faster Than Ever: Here’s How Security Can Keep Up
-
Java String Templates Today
-
What Is TTS and How Is It Implemented in Apps?
Comments