MemoryCache to optimize database lookups.


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

<div style="font-family:Consolas;font-size:13px;color:gainsboro;background:#1e1e1e;padding:1.5em"><span style="color:#569cd6;">Imports</span> System<br/><span style="color:#569cd6;">Imports</span> System<span style="color:#b4b4b4;">.</span>Runtime<span style="color:#b4b4b4;">.</span>Caching<br/> <br/><span style="color:#569cd6;">Namespace</span> GenericCache<br/>    <span style="color:#569cd6;">Module</span> <span style="color:#4ec9b0;">Caching</span><br/>        <span style="color:#569cd6;">Function</span> GetObjectFromCache(<span style="color:#569cd6;">Of</span> <span style="color:#b8d7a3;">T</span>)(<span style="color:#569cd6;">ByVal</span> cacheItemName <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">String</span>, <span style="color:#569cd6;">ByVal</span> cacheTimeInMinutes <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">Integer</span>, <span style="color:#569cd6;">ByVal</span> objectSettingFunction <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">Func</span>(<span style="color:#569cd6;">Of</span> <span style="color:#b8d7a3;">T</span>), <span style="color:#569cd6;">ByRef</span> isCached <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">Boolean</span>) <span style="color:#569cd6;">As</span> <span style="color:#b8d7a3;">T</span><br/>            <span style="color:#569cd6;">Dim</span> cache <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">ObjectCache</span> <span style="color:#b4b4b4;">=</span> <span style="color:#4ec9b0;">MemoryCache</span><span style="color:#b4b4b4;">.</span>[Default]<br/> <br/>            <span style="color:#569cd6;">Dim</span> cachedObject <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">CType</span>(cache(cacheItemName), <span style="color:#b8d7a3;">T</span>)<br/> <br/>            <span style="color:#569cd6;">If</span> cachedObject <span style="color:#569cd6;">Is</span> <span style="color:#569cd6;">Nothing</span> <span style="color:#569cd6;">Then</span><br/>                <span style="color:#569cd6;">Dim</span> policy <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">CacheItemPolicy</span> <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">New</span> <span style="color:#4ec9b0;">CacheItemPolicy</span>()<br/>                <span style="color:#57a64a;">'policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(cacheTimeInMinutes) ' Fixed Timeout</span><br/>                policy<span style="color:#b4b4b4;">.</span>SlidingExpiration <span style="color:#b4b4b4;">=</span> <span style="color:#4ec9b0;">TimeSpan</span><span style="color:#b4b4b4;">.</span>FromMinutes(cacheTimeInMinutes) <span style="color:#57a64a;">' Sliding timeout</span><br/>                cachedObject <span style="color:#b4b4b4;">=</span> objectSettingFunction()<br/>                isCached <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">False</span><br/>                cache<span style="color:#b4b4b4;">.</span>[Set](cacheItemName, cachedObject, policy)<br/>            <span style="color:#569cd6;">Else</span><br/>                isCached <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">True</span><br/>            <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">If</span><br/> <br/>            <span style="color:#569cd6;">Return</span> cachedObject<br/>        <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">Function</span><br/> <br/>    <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">Module</span><br/> <br/><span style="color:#569cd6;">End</span> <span style="color:#569cd6;">Namespace</span><br/> <br/></div>

Implementing

<div style="font-family:Consolas;font-size:13px;color:gainsboro;background:#1e1e1e;padding:1.5em;"><span style="color:#569cd6;">Dim</span> FetchfromDatabase <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">Func</span>(<span style="color:#569cd6;">Of</span> <span style="color:#4ec9b0;">IPAddressLookup</span>) <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">AddressOf</span> dbFetch<br/> <br/>   <span style="color:#569cd6;">Private</span> <span style="color:#569cd6;">Function</span> dbFetch() <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">IPAddressLookup</span><br/>       <span style="color:#569cd6;">Using</span> db <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">New</span> <span style="color:#4ec9b0;">MyDBDataContext</span><br/>           <span style="color:#569cd6;">Dim</span> result <span style="color:#b4b4b4;">=</span> (<span style="color:#569cd6;">From</span> this <span style="color:#569cd6;">In</span> db<span style="color:#b4b4b4;">.</span>IPAddressLookups <span style="color:#569cd6;">Where</span> this<span style="color:#b4b4b4;">.</span>IPAddress <span style="color:#b4b4b4;">=</span> <span style="color:#d69d85;">"192.168.0.1"</span>)<span style="color:#b4b4b4;">.</span>ToList<br/>           <span style="color:#569cd6;">If</span> result<span style="color:#b4b4b4;">.</span>Count <span style="color:#b4b4b4;">></span> <span style="color:#b5cea8;">0</span> <span style="color:#569cd6;">Then</span><br/>               <span style="color:#569cd6;">Dim</span> rtn <span style="color:#b4b4b4;">=</span> result<span style="color:#b4b4b4;">.</span>First<br/>               rtn<span style="color:#b4b4b4;">.</span>LastAccess <span style="color:#b4b4b4;">=</span> Now<br/>               <span style="color:#569cd6;">Return</span> rtn<br/>           <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">If</span><br/>       <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">Using</span><br/>       <span style="color:#569cd6;">Return</span> <span style="color:#569cd6;">Nothing</span><br/>   <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">Function</span></div>

Fetching

<div style="font-family:Consolas;font-size:13px;color:gainsboro;background:#1e1e1e;padding:1.5em;"><span style="color:#569cd6;">Dim</span> isCached <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">Boolean</span><br/><span style="color:#569cd6;">Dim</span> IPAddressResult <span style="color:#569cd6;">As</span> <span style="color:#4ec9b0;">IPAddressLookup</span> <span style="color:#b4b4b4;">=</span> GetObjectFromCache(<span style="color:#569cd6;">Of</span> <span style="color:#4ec9b0;">IPAddressLookup</span>)(<span style="color:#d69d85;">"IPAddressResult"</span>, <span style="color:#b5cea8;">1</span>, FetchfromDatabase, isCached)</div>

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.

Article Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...