Repository Pattern with Caching and Hangfire in ASP.NET Core

Repository Pattern with Caching and Hangfire in ASP.NET Core

In this article, we will learn ASP.NET Core 3.1 WebAPI. Entity Framework Core. Generic Repository Pattern. Generic Caching to support various caching techs. Hangfire to process Caching Jobs in the background. Single Interface with Multiple Implementations.

In this article, we will learn about an approach with which we will build a SUPER-FAST Repository Implementation using all the latest libraries and concepts. We will be implementing Repository Pattern with Caching and Hangfire in ASP.NET Core along with Onion Architecture just to make sure our code is well organized and be readily used in any random project. You can find the source code of this implementation here.

The Requirement

Let’s have a simple ASP.NET Core 3.1 WebAPI that does CRUD operations over a Customer Entity. That’s quite simple, yeah? But we will try to make this API return much faster than it would do traditionally. How can we achieve this? Caching, of-course. The first question is where would you implement caching? The ideal layer would be to couple it somehow with the Repository layer, so that every time you work with the Repository Layer, your results are being cached.

The other requirement is to implement a generic caching where-in the user can have the flexibility to use different caching techs like In-Memory, Redis, and so on. Finally, let’s integrate caching with Hangfire so that it runs in the background at specific triggers.

So, the idea is simple. We will build a traditional Repository Pattern along with the DBContext of Entity Framework Core. Every time a user requests for Customer data, we need to check if it already exists in the cache, else retrieve it from DB and cache it. And whenever someone deletes or modifies a record, the cached data should get invalidated and recached. Caching again may take some time. Thus, we set this caching process as a background job. Simple? Let’s get started!

aspdotnet aspdotnet core

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Set start URL in ASP.NET Core - Quick & Easy ways

This article will cover the ways to set start URL in ASP.NET Core applications i.e. change the default URL (http://localhost:5000) in ASP.NET Core applications. Set start URL in ASP.NET Core - Quick & Easy ways.

ASP.NET Core Identity Roles based Authorization

ASP.NET Core Identity Roles based Authorization. This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization.

ASP.NET Core Identity Roles based Authorization

ASP.NET Core Identity Roles based Authorization. This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization.

How to Send Emails in ASP.NET Core C# - Using SMTP with MailKit

How to Send Emails in ASP.NET Core C# - Using SMTP with MailKit. In this article, we will see a demo about how to send emails in ASP.NET Core using the open-source, cross-platform MailKit library in quick & easy steps.