Implementing a data cache

We are going to implement a cache for the questions using the memory cache in ASP.NET Core:

  1. First, let's create an interface in the Data folder called IQuestionCache:
using QandA.Data.Models;

namespace QandA.Data
{
public interface IQuestionCache
{
QuestionGetSingleResponse Get(int questionId);
void Remove(int questionId);
void Set(QuestionGetSingleResponse question);
}
}

So, we need the cache implementation to have methods for getting, removing, and updating an item in the cache.

  1. Now, we can create a class in the Data folder called QuestionCache:
using Microsoft.Extensions.Caching.Memory; 
using QandA.Data.Models;

namespace QandA.Data
{
public class QuestionCache: IQuestionCache
{
// TODO - create a memory cache
// TODO - method to get a cached question
// TODO - method to add a cached question
// TODO - method to remove a cached question
}
}

Notice that we have referenced Microsoft.Extensions.Caching.Memory so that we can use the standard ASP.NET Core memory cache.

  1. Let's create a constructor that creates an instance of the memory cache:
public class QuestionCache: IQuestionCache
{
private MemoryCache _cache { get; set; }
public QuestionCache()
{
_cache = new MemoryCache(new MemoryCacheOptions
{
SizeLimit = 100
});
}

// TODO - method to get a cached question
// TODO - method to add a cached question
// TODO - method to remove a cached question
}

Notice that we have set the cache limit to be 100 items. This is to limit the amount of memory the cache takes up on our web server.

  1. Let's implement a method to get a question from the cache:
public class QuestionCache: IQuestionCache
{
...

private string GetCacheKey(int questionId) =>
$"Question-{questionId}";


public QuestionGetSingleResponse Get(int questionId)
{
QuestionGetSingleResponse question;
_cache.TryGetValue(
GetCacheKey(questionId),
out question);

return question;
}

// TODO - method to add a cached question
// TODO - method to remove a cached question
}

We have created an expression to give us a key for a cache item, which is the word Question with a hyphen, followed by the question ID.

We use the TryGetValue method within the memory cache to retrieve the cached question. So, null will be returned from our method if the question doesn't exist in the cache.

  1. Now, we can implement a method to add a question to the cache. We can add an item to the cache using the Set method in the ASP.NET Core memory cache:
public class QuestionCache: IQuestionCache
{
...

public void Set(QuestionGetSingleResponse question)
{
var cacheEntryOptions =
new MemoryCacheEntryOptions().SetSize(1);

_cache.Set(
GetCacheKey(question.QuestionId),
question,
cacheEntryOptions);

}

// TODO - method to remove a cached question
}

Notice that we specify the size of the question in the options when setting the cache value. This ties in with the size limit we set on the cache so that the cache will start to remove questions from the cache when there are 100 questions in it.

  1. The last method we need to implement is a method to remove questions from the cache:
public class QuestionCache: IQuestionCache
{
...

public void Remove(int questionId)
{
_cache.Remove(GetCacheKey(questionId));
}
}

Note that if the question doesn't exist in the cache, nothing will happen and no exception will be thrown.

That completes the implementation of our question cache.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.7