-
Notifications
You must be signed in to change notification settings - Fork 10
Configuration
Joakim Skoog edited this page Jun 8, 2017
·
2 revisions
It's super easy to configure Cashew, all you need to do is create an HttpCachingHandler
and then provide it as an argument to HttpClient
. The following example is done by creating an HttpClient
and all its dependencies by hand, note that this is much easier if you use an IoC-container.
//We like CacheManager so much that we want to use it as our cache library.
var config = new ConfigurationBuilder()
.WithSystemRuntimeCacheHandle()
.Build();
//We now have our cache which we will use
var cache = new BaseCacheManager<object>(config);
//But seeing as Cashew has its own simple interface, called ICache, we need to make use of the adapter pattern.
var cacheAdapter = new CacheManagerAdapter(cache);
//We use the default HTTP compliant strategy for creating keys (we need the cache for vary-header support)
var keyStrategy = new HttpStandardKeyStrategy(cacheAdapter);
//We also want to use the HTTP compliant strategy for creating cache keys, making this super easy
var httpCachingHandler = new HttpCachingHandler(cacheAdapter, keyStrategy);
//We're now ready to create our HttpClient and start sending requests with awesome built-in caching functionality!
var httpClient = new HttpClient(httpCachingHandler);
As mentioned above this process can be simplified if you use an IoC-container or don't use CacheManager
.
For more information on how to configure CacheManager, please refer to this.