Caching In Web API

In this article we will are going to learn how to use caching in Web API.

Normally caching is the process of storing data somewhere for  future requests, in our case we can avoid the unwanted hit to the database to get the data if we cache the data somewhere, this way we can make sure that the data is served in a faster manner. Here we are going to see the caching in our Web API controller. If you are new to caching, I have written an article about caching in MVC, please read it here: Caching In MVC.

 We will explain caching with the help of System.Runtime.Caching which is given by Microsoft. This DLL is not available in the default references, you may need to add that separately. I will show you how. I am creating this application in Visual Studio 2015. You can always get the tips/tricks/blogs about these mentioned technologies from the links given below.

Now we will go and create our application. I hope you will like this.

Download the source code: You can always download the source code here.

Background

For the past few days I have been working with Web API. Here we are going to see a demo of how to use Caching in Web API in our MVC application. We are going to use the DLL System.Runtime.Caching.dll, which you need to add as reference.

Create an MVC application

Click File, New, Project then select MVC application. From the following pop up we will select the template as empty and select the core references and folders for MVC.

Empty Template With MVC And Web API Folders

Figure: Empty Template With MVC And Web API Folders

Once you click OK, a project with MVC like folder structure with core references will be created for you.

Folder Structure And References For Empty MVC Project

Figure: Folder Structure And References For Empty MVC Project

Once your application is ready, we can add the reference for System.Runtime.Caching

Add reference for System.Runtime.Caching

To add the reference, right click on References and click Add Reference.

Add_References

Figure:  Add_References

Now click on the browse button, and search System.Runtime.Caching

References_Found_

Figure: References_Found_

And  clicking OK, the DLL will be added to your references now.

Using the code

We will set up our database first so that we can create Entity Model for our application later.

Create a database

The following query can be used to create a database in your SQL Server.

  1. USE [master]  
  2. GO  
  3. /****** Object: Database [TrialsDB]  
  4. CREATE DATABASE [TrialsDB]  
  5. CONTAINMENT = NONE  
  6. ON PRIMARY  
  7. NAME = N'TrialsDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  8. LOG ON  
  9. NAME = N'TrialsDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TrialsDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  10. GO  
  11. ALTER DATABASE [TrialsDB] SET COMPATIBILITY_LEVEL = 110  
  12. GO  
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  14. begin  
  15. EXEC [TrialsDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  16. end  
  17. GO  
  18. ALTER DATABASE [TrialsDB] SET ANSI_NULL_DEFAULT OFF  
  19. GO  
  20. ALTER DATABASE [TrialsDB] SET ANSI_NULLS OFF  
  21. GO  
  22. ALTER DATABASE [TrialsDB] SET ANSI_PADDING OFF  
  23. GO  
  24. ALTER DATABASE [TrialsDB] SET ANSI_WARNINGS OFF  
  25. GO  
  26. ALTER DATABASE [TrialsDB] SET ARITHABORT OFF  
  27. GO  
  28. ALTER DATABASE [TrialsDB] SET AUTO_CLOSE OFF  
  29. GO  
  30. ALTER DATABASE [TrialsDB] SET AUTO_CREATE_STATISTICS ON  
  31. GO  
  32. ALTER DATABASE [TrialsDB] SET AUTO_SHRINK OFF  
  33. GO  
  34. ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS ON  
  35. GO  
  36. ALTER DATABASE [TrialsDB] SET CURSOR_CLOSE_ON_COMMIT OFF  
  37. GO  
  38. ALTER DATABASE [TrialsDB] SET CURSOR_DEFAULT GLOBAL  
  39. GO  
  40. ALTER DATABASE [TrialsDB] SET CONCAT_NULL_YIELDS_NULL OFF  
  41. GO  
  42. ALTER DATABASE [TrialsDB] SET NUMERIC_ROUNDABORT OFF  
  43. GO  
  44. ALTER DATABASE [TrialsDB] SET QUOTED_IDENTIFIER OFF  
  45. GO  
  46. ALTER DATABASE [TrialsDB] SET RECURSIVE_TRIGGERS OFF  
  47. GO  
  48. ALTER DATABASE [TrialsDB] SET DISABLE_BROKER  
  49. GO  
  50. ALTER DATABASE [TrialsDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF  
  51. GO  
  52. ALTER DATABASE [TrialsDB] SET DATE_CORRELATION_OPTIMIZATION OFF  
  53. GO  
  54. ALTER DATABASE [TrialsDB] SET TRUSTWORTHY OFF  
  55. GO  
  56. ALTER DATABASE [TrialsDB] SET ALLOW_SNAPSHOT_ISOLATION OFF  
  57. GO  
  58. ALTER DATABASE [TrialsDB] SET PARAMETERIZATION SIMPLE  
  59. GO  
  60. ALTER DATABASE [TrialsDB] SET READ_COMMITTED_SNAPSHOT OFF  
  61. GO  
  62. ALTER DATABASE [TrialsDB] SET HONOR_BROKER_PRIORITY OFF  
  63. GO  
  64. ALTER DATABASE [TrialsDB] SET RECOVERY FULL  
  65. GO  
  66. ALTER DATABASE [TrialsDB] SET MULTI_USER  
  67. GO  
  68. ALTER DATABASE [TrialsDB] SET PAGE_VERIFY CHECKSUM  
  69. GO  
  70. ALTER DATABASE [TrialsDB] SET DB_CHAINING OFF  
  71. GO  
  72. ALTER DATABASE [TrialsDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )  
  73. GO  
  74. ALTER DATABASE [TrialsDB] SET TARGET_RECOVERY_TIME = 0 SECONDS  
  75. GO  
  76. ALTER DATABASE [TrialsDB] SET READ_WRITE  
  77. GO   

Now we will create the table we needed. As of now I am going to create the table tblTags

Create tables in database

The following is the query to create the table tblTags.

  1. USE [TrialsDB]  
  2. GO  
  3. /****** Object: Table [dbo].[tblTags] Script Date: 23-Mar-16 5:01:22 PM ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. CREATE TABLE [dbo].[tblTags](  
  9. [tagId] [int] IDENTITY(1,1) NOT NULL,  
  10. [tagName] [nvarchar](50) NOT NULL,  
  11. [tagDescription] [nvarchar](maxNULL,  
  12. CONSTRAINT [PK_tblTags] PRIMARY KEY CLUSTERED  
  13. (  
  14. [tagId] ASC  
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  16. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  17. GO   

Can we insert some data to the tables now?

Insert data to table

You can use the following query to insert the data to the table tblTags

  1. USE [TrialsDB]  
  2. GO  
  3. INSERT INTO [dbo].[tblTags]  
  4. ([tagName]  
  5. ,[tagDescription])  
  6. VALUES  
  7. (<tagName, nvarchar(50),>  
  8. ,<tagDescription, nvarchar(max),>)  
  9. GO   

Next thing we are going to do is creating an ADO.NET Entity Data Model.

Create Entity Data Model

Right click on your model folder and click new, select ADO.NET Entity Data Model. Follow the steps given. Once you have done the processes, you can see the edmx file and other files in your model folder. Here I gave Dashboard for our Entity data model name. Now you can see a file with edmx extension have been created.

Now will create our Web API controller.

Create Web API Controller

To create a Web API controller, just right click on your controller folder and click Add, Controller, then select Web API 2 controller with actions, using Entity Framework.

Web API 2 Controller With Actions Using Entity Framework

Figure: Web API 2 Controller With Actions Using Entity Framework

Now select tblTag (CachingInWebAPI.Models) as our Model class and TrialsDBEntities (CachingInWebAPI.Models) as data context class.

As you can see it has been given the name of our controller as tblTags. Here, I am not going to change that, if you wish to change, you can do that.

Now you will be given the following codes in our new Web API controller.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Data.Entity.Infrastructure;  
  6. using System.Linq;  
  7. using System.Net;  
  8. using System.Net.Http;  
  9. using System.Web.Http;  
  10. using System.Web.Http.Description;  
  11. using CachingInWebAPI.Models;  
  12. namespace CachingInWebAPI.Controllers  
  13. {  
  14.     public class tblTagsController: ApiController  
  15.     {  
  16.         private TrialsDBEntities db = new TrialsDBEntities();  
  17.         // GET: api/tblTags  
  18.         public IQueryable < tblTag > GettblTags()  
  19.         {  
  20.             return db.tblTags;  
  21.         }  
  22.         // GET: api/tblTags/5  
  23.         [ResponseType(typeof(tblTag))]  
  24.         public IHttpActionResult GettblTag(int id)  
  25.         {  
  26.             tblTag tblTag = db.tblTags.Find(id);  
  27.             if (tblTag == null)  
  28.             {  
  29.                 return NotFound();  
  30.             }  
  31.             return Ok(tblTag);  
  32.         }  
  33.         // PUT: api/tblTags/5  
  34.         [ResponseType(typeof(void))]  
  35.         public IHttpActionResult PuttblTag(int id, tblTag tblTag)  
  36.         {  
  37.             if (!ModelState.IsValid)  
  38.             {  
  39.                 return BadRequest(ModelState);  
  40.             }  
  41.             if (id != tblTag.tagId)  
  42.             {  
  43.                 return BadRequest();  
  44.             }  
  45.             db.Entry(tblTag).State = EntityState.Modified;  
  46.             try  
  47.             {  
  48.                 db.SaveChanges();  
  49.             }  
  50.             catch (DbUpdateConcurrencyException)  
  51.             {  
  52.                 if (!tblTagExists(id))  
  53.                 {  
  54.                     return NotFound();  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     throw;  
  59.                 }  
  60.             }  
  61.             return StatusCode(HttpStatusCode.NoContent);  
  62.         }  
  63.         // POST: api/tblTags  
  64.         [ResponseType(typeof(tblTag))]  
  65.         public IHttpActionResult PosttblTag(tblTag tblTag)  
  66.         {  
  67.             if (!ModelState.IsValid)  
  68.             {  
  69.                 return BadRequest(ModelState);  
  70.             }  
  71.             db.tblTags.Add(tblTag);  
  72.             db.SaveChanges();  
  73.             return CreatedAtRoute("DefaultApi"new  
  74.             {  
  75.                 id = tblTag.tagId  
  76.             }, tblTag);  
  77.         }  
  78.         // DELETE: api/tblTags/5  
  79.         [ResponseType(typeof(tblTag))]  
  80.         public IHttpActionResult DeletetblTag(int id)  
  81.         {  
  82.             tblTag tblTag = db.tblTags.Find(id);  
  83.             if (tblTag == null)  
  84.             {  
  85.                 return NotFound();  
  86.             }  
  87.             db.tblTags.Remove(tblTag);  
  88.             db.SaveChanges();  
  89.             return Ok(tblTag);  
  90.         }  
  91.         protected override void Dispose(bool disposing)  
  92.         {  
  93.             if (disposing)  
  94.             {  
  95.                 db.Dispose();  
  96.             }  
  97.             base.Dispose(disposing);  
  98.         }  
  99.         private bool tblTagExists(int id)  
  100.         {  
  101.             return db.tblTags.Count(e => e.tagId == id) > 0;  
  102.         }  
  103.     }  
  104. }   

As we are not going to use only read operation, you can remove other functionalities and keep only Getmethods for now.

  1. // GET: api/tblTags  
  2. public IQueryable<tblTag> GettblTags()  
  3. {  
  4.    return db.tblTags;  
  5. }   

So the coding part to fetch the data from database is ready, now we need to check whether our Web API is ready for action! To check that, you just need to run the URL http://localhost:4832/api/tblTags. HeretblTags is our Web API controller name. I hope you get the data as a result.

Web_API_Result

Figure: Web_API_Result

Now we will start testing our caching. For that please import the namespace System.Runtime.Caching;

  1. using System.Runtime.Caching;  

Next we will create an instance of MemoryCache class.

MemoryCache_Tooltip_

Figure: MemoryCache_Tooltip_

As you can see there are four possible settings we can set in MemoryCache class.

MemoryCache_Settings

Figure: MemoryCache_Settings

Now we can add MemoryCache as follows.

  1. public IQueryable < tblTag > GettblTags()  
  2. {  
  3.     var ca = db.tblTags;  
  4.     memCache.Add("tag", ca, DateTimeOffset.UtcNow.AddMinutes(5));  
  5.     return db.tblTags;  
  6. }   
MemoryCache_Add

Figure: MemoryCache_Add

Here “tag” is my key and “ca” is my values and DateTimeOffset.UtcNow.AddMinutes(5) is for setting the cache for five minutes from now.

Shall we check how it works now? To check whether the content has been added to the cache we need to use the Get method, please see the code block below.

  1. var res = memCache.Get("tag");  
  2. if (res != null)  
  3. {  
  4.     return res;  
  5. }  
  6. else  
  7. {  
  8.     var ca = db.tblTags;  
  9.     memCache.Add("tag", ca, DateTimeOffset.UtcNow.AddMinutes(5));  
  10.     return db.tblTags;  
  11. }   

We will get the cache values in the variable res, remember this value will be there only for five minutes. You can always change that as per your need. If the value is not null, we will just return it and do the manipulation and if it is null we will go ahead and fetch the data from database and add the value to cache. Now please run your API by running the URL http://localhost:4832/api/tblTags.

Cache_Value_Null

Figure: Cache_Value_Null

You can see that we are setting the value to memCache as the memCache.Get(“tag”) is null.

Cache_Value_Not_Null

Figure: Cache_Value_Not_Null

So we just tried to load the same Web API URL within five minutes, so we get values frommemCache.Get(“tag”). Sounds good?

There is an option to remove our cache too, we will see that now. We will use the Remove function for the same. Firstly, we will check whether the key is available in the MemoryCache, if it is available we will remove that.

  1. //This is to remove the MemoryCache - start  
  2. if (memCache.Contains("tag"))  
  3. {  
  4.    memCache.Remove("tag");  
  5. }  
  6. //This is to remove the MemoryCache - end   
MemoryCache_Remove

Figure: MemoryCache_Remove

We have done everything! That’s fantastic right? Happy coding.

Conclusion

Did I miss anything that you may think is needed? Did you try Web API yet? Have you ever wanted to do caching in Web API? Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Please see this article in my blog here.

Read more articles on ASP.NET: