Absolute cache expiry corrupts absolutely?

Should you specify absolute expiry of a cache item from the current time or from the current time in the UTC time zone? The answer may not be what you expect.

July 12, 2019 · 5 min · 874 words · Alastair Crabtree

LazyCache 2.0 released

LazyCache is a library that makes it easy for developers to add in-memory caching to dotnet apps. Version 2 is a major rewrite to change from the .Net Framework 4.5 to Netstandard 2.0, and is now available from nuget.org/packages/LazyCache. Getting started Install from nuget: dotnet add package LazyCache --version 2.0.0 And then cache object that are slow or expensive to produce: IAppCache cache = new CachingService(); Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources(); ComplexObjects cachedResults = cache....

March 22, 2019 · 2 min · 414 words · Alastair Crabtree

Stubbing your way to automated end to end testing in an API first world

Building high quality end to end acceptance tests suites is hard, and creating API stubs (aka mock servers) can be surprisingly time consuming. WireMock.net can help speed this up. E2E testing is hard End to end testing is hard for many reasons, but I think one of the most common is that the developers under estimate how much time to allow for it and then rush the job. Most E2E test harnesess I have worked on, (including several I wrote myself!...

December 4, 2017 · 7 min · 1476 words · Alastair Crabtree

Implementing the retry pattern in c sharp using Polly

This post is the third and final installment on the retry pattern following on from implementing a simple retry pattern in c# and the retry pattern for async tasks. In the last two posts we created a retry helper class to allow us to add retry logic to applications without cluttering important application logic with retry code. Now we will solve the same problem using a popular OSS library. Polly is an open source framework for that “allows developers to express transient exception and fault handling policies such as Retry, Retry Forever, Wait and Retry, or Circuit Breaker in a fluent manner”....

December 29, 2016 · 3 min · 611 words · Alastair Crabtree

Implementing the retry pattern for async tasks in c#

This post is a follow on from Implementing a simple retry pattern in c#. Tasks, async and await are rapidly becoming be default API flavours in many dotnet libraries and the performance benefits for IO bound code have been well documented. However if you need to apply the retry pattern to some async or task returning method invocation you need to watch out for subtle errors. I’ll outline the problem and revise my solution from the previous post....

December 2, 2016 · 3 min · 470 words · Alastair Crabtree

Implementing a simple retry pattern in c#

Everything is unstable. You cannot assume that every external service call will succeed on first attempt - the load balancer might be removing a failing host, the database might be being patched, the Ops person might have tripped on the network cable and quickly plugged it back in hoping the boss didn’t notice. If you make an outbound call you probably need an easy way to retry a couple of times before giving up and raising an exception....

December 1, 2016 · 3 min · 629 words · Alastair Crabtree

Cache the result of an async method using LazyCache

The latests release of LazyCache, my open source cache library based on ObjectCache, makes it easy to cache the results of asynchronous or Task returning methods, so now it is simpler for you to speed up your application. Why bother with Async? Async code tends to be more efficient as you release threads while they are waiting for the response from an asynchronous resource, but at a cost of added code complexity....

September 30, 2016 · 3 min · 626 words · Alastair Crabtree

Detecting plurals in dot.net / C# the way a human might

I recently had a requirement to be able to spot pluralised words which may not be real words, but follow English language conventions, and so appear as a plural to any normal reader. So ComplexBusinesObjects is a valid plural of ComplexBusinesObject but series is its own plural. The idea was to write code that would recognise a plural in the same way a human might, even if it was not perfect grammar....

August 16, 2016 · 2 min · 361 words · Alastair Crabtree

Choose the right return type for WebApi controllers

WebApi controller actions can return a variety of response types: HttpResponseMessage, IHttpActionResult, CLR objects and then the Task based variety of each for async actions. But which one is best? And how do we unit test them? Lets look at each in turn and examine why IHttpActionResult is usually the right choice. Synchronous controllers returning objects The simplest web api controller can directly return the object in question. Say you have a ProductController that is concerned with CRUD operations on Products, it might have a method like the below:...

May 30, 2016 · 4 min · 661 words · Alastair Crabtree

LazyCache: The easy way to add caching to your .net app & make it fast

tl;dr If you need to speed up your c# application you probably need to add caching, and the easiest way to do that is use the source library I wrote called LazyCache. Do I need to cache? Lots of apps don’t need caching because web servers and databases are fast. However many apps get to a point where performance becomes an issue, and adding caching is one easy way to get a significant performance boost....

May 23, 2016 · 7 min · 1282 words · Alastair Crabtree