Skip to content
AnthonySteele edited this page Oct 25, 2014 · 3 revisions

The 7digital Api Wrapper supports response caching via the IResponseCache interface. You will want to do this for performance reasons, so that repeated requests to the same Api url get responses from cache rather than incurring another network round trip.

We assume that you have already chosen a caching technology. This could be Memcached, redis etc, but in the example we will use the simplest one, MemoryCache.

In order to cache responses you must supply an implementation of IResponseCache that stores and retrieves data in your cache. Then you attach an instance of the interface to the IFluentApi with the UsingCache method.

The IResponseCache interface has only 2 methods: Get and Set. Here is a sample implementation of a IResponseCache that stores values in a MemoryCache. This can be adapted to use a different underlying cache.

using System;
using System.Runtime.Caching;
using SevenDigital.Api.Wrapper.Requests;
using SevenDigital.Api.Wrapper.Responses;

namespace SevenDigital.Api.Wrapper.ExampleUsage
{
	public class MemoryResponseCache : IResponseCache
	{
		private readonly MemoryCache _memoryCache;

		public MemoryResponseCache(MemoryCache memoryCache)
		{
			_memoryCache = memoryCache;
		}

		public void Set(RequestData key, object value)
		{
			var memoryCacheKey = MakeCacheKey(key);
			_memoryCache.Set(memoryCacheKey, value, DefaultExpiration());
		}

		public bool TryGet<T>(RequestData key, out T value)
		{
			var cacheKey = MakeCacheKey(key);
			var cacheValue = _memoryCache.Get(cacheKey);

			if (cacheValue is T)
			{
				value = (T)cacheValue;
				return true;
			}

			value = default(T);
			return false;
		}

		private string MakeCacheKey(RequestData key)
		{
			var paramValues = string.Join("_", key.Parameters);
			return "7digital_" + key.HttpMethod + "_" + key.Endpoint + "_" + paramValues;
		}

		private DateTimeOffset DefaultExpiration()
		{
			return DateTimeOffset.Now.AddMinutes(10);
		}
	}
}

An instance of this IResponseCache should then be attached to every IFluentApi instance. A good place to do this is in the code that creates them, i.e. in a customised IApi class in the example below.

For simplicity's sake, this example wraps the default IApi implementation and just adds the cache:

	public class ExampleApi : IApi
	{
		private readonly IApi _standardApi;
		private readonly MemoryResponseCache _cache;

		public ExampleApi()
		{
			var apiUri = new ApiUri();
			var oauthCredentials = new AppSettingsCredentials();
			_standardApi = new ApiFactory(apiUri, oauthCredentials);

			_cache = new MemoryResponseCache(MemoryCache.Default);
		}

		public IFluentApi<T> Create<T>() where T : class, new()
		{
			return _standardApi.Create<T>()
				.UsingCache(_cache);
		}
	}
Clone this wiki locally