Introduction
The following code snippet displays a method of using a memory cache for generic types.
Because there are plenty of examples of using MemoryCache we have included a VB.net example below.
In this example performance is increased by putting the object into cache (which happens to be a row of data from a database), if that data is not accessed within a sliding expiration time (in this example 1 minute) it will be removed from the memory cache and if accessed later will re fetch from the database and once again place it in the cache.
Generic MemoryCache
Implementing
Fetching
In the above example IPAddressResult will be either the cached data row or newly fetched data row depending on whether it was recently accessed within the sliding expiry period or not.
The example can be used in asp.net or Windows Forms applications.