Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern - Caching

I'm not sure where I should implement the caching in my repository pattern.

Should I implement it in the service-logic or in the repository?

GUI -> BusinessLogic (Services) -> DataAccess (Repositories)

like image 798
Beni Avatar asked Aug 09 '10 16:08

Beni


People also ask

What is repository cache?

A cache repository is a storage location where Veeam Backup & Replication keeps temporary cached metadata for the data backed up by the file share backup jobs. For more information about cache repository, see NAS Backup Support.

What is a repository pattern?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What problem does repository pattern solve?

They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources.

How does repository pattern work?

The idea behind the Repository pattern is to decouple the data access layer from the business access layer of the application so that the operations (such as adding, updating, deleting, and selecting items from the collection) is done through straightforward methods without dealing with database concerns such as ...


2 Answers

It's a good idea not to put the caching logic directly into your repository, as that violates the Single Responsibility Principle (SRP) and Separation of Concerns. SRP essentially states that your classes should only have one reason to change. If you conflate the concerns of data access and caching policy in the same class, then if either of these needs to change you'll need to touch the class. You'll also probably find that you're violating the DRY principle, since it's easy to have caching logic spread out among many different repository methods, and if any of it needs to change, you end up having to change many methods.

The better approach is to use the Proxy or Strategy pattern to apply the caching logic in a separate type, for instance a CachedRepository, which then uses the actual db-centric repository as needed when the cache is empty. I've written two articles which demonstrate how to implement this using .NET/C#, which you will find on my blog, here:

  • http://ardalis.com/introducing-the-cachedrepository-pattern
  • http://ardalis.com/building-a-cachedrepository-via-strategy-pattern

If you prefer video, I also describe the pattern in the Proxy Design Pattern on Pluralsight, here:

  • https://app.pluralsight.com/library/courses/patterns-library/table-of-contents
like image 151
ssmith Avatar answered Sep 20 '22 15:09

ssmith


I would handle it in the repository/data access layer. The reasoning is because it isn't up to the business layer on where to get the data from, that is the job of the repository. The repository will then decide where to get the data from, the cache (if it's not too old) or from the live data source based on the circumstances of the data access logic.

It's a data access concern more than a business logic issue.

like image 40
KallDrexx Avatar answered Sep 20 '22 15:09

KallDrexx