Skip to content

Cache abstraction for caching with OnMemory, Redis, Storage Blobs

License

Notifications You must be signed in to change notification settings

ERNI-Academy/assets-cache-abstraction

Repository files navigation

About Cache Abstraction

Define a contract and many implementations (OnMemory, Azure Storage Blobs, Azure Redis) for caching

ERNI Academy StarterKit, PoC, or Gidelines. This is an about description of your repository.

Built With

This section should list any major frameworks that you built your project using. Leave any add-ons/plugins for the acknowledgements section. Here are a few examples.

Features

Get GetAsync Set SetAsync Exists ExistsAsync Remove RemoveAsync

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Prerequisites

.net 6 Visual Studio or Visual Studio Code

Installation

Installation instructions Cache Abstraction by running:

  1. Clone the repo
git clone --recurse-submodules https://github.com/ERNI-Academy/assets-cache-abstraction.git

Important Note
All implementations heavly depends on Microsoft Options Pattern for configurations. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0 So it is expected a proper configuration in order to work take a look at the samples to see how to configure each All implementatins also depends on Microsoft logging. See https://docs.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line

  1. Cache Basic use
class MyItem
{
	public string MyCustomProperty { get; set; }
}

var item = new MyItem { MyCustomProperty = "hi" };

//you can choose between many impl
ICacheManager cacheManager = new ErniAcademy.Cache.OnMemory.OnMemoryCacheManager();//args ommited for simplicity
ICacheManager cacheManager = new ErniAcademy.Cache.Redis.RedisCacheManager();//args ommited for simplicity
ICacheManager cacheManager = new ErniAcademy.Cache.StorageBlobs.StorageBlobsCacheManager();//args ommited for simplicity

//set an Item into cache
await cacheManager.SetAsync("key1", item);

//get an Item from cache
var cachedItem = await cacheManager.GetAsync<MyItem>("key1");
  1. Cache Depency injection (ServiceCollection)
class MyItem
{
	public string MyCustomProperty { get; set; }
}

//when configuring your ServiceCollection use the extension methods defined in each library for easy of use. 
//This sample is provided with no arguments, take a look on the extensions to see the rest of the arguments, like IConfiguration, ISerializer etc.
services.AddCacheOnMemory();//args ommited for simplicity
services.AddCacheRedis();//args ommited for simplicity
services.AddCacheStorageBlobs();//args ommited for simplicity

//then just inject ICacheManager directly in your classes

class MyService
{
  private readonly ICacheManager _cacheManager;

  public MyService(ICacheManager cacheManager)
  {
    _cacheManager = cacheManager;
  }

  public async Task SomeMethod()
  {
      //... some logic
      
     var item = new MyItem { MyCustomProperty = "hi" };

     //set an Item into cache
     await _cacheManager.SetAsync("key1", item);

     //get an Item from cache
     var cachedItem = await _cacheManager.GetAsync<MyItem>("key1");
  }
}

Contributing

Please see our Contribution Guide to learn how to contribute.

License

MIT

(LICENSE) © 2022 ERNI - Swiss Software Engineering

Code of conduct

Please see our Code of Conduct

Stats

Alt

Follow us

Twitter Follow Twitch Status YouTube Channel Views Linkedin

Contact

📧 esp-services@betterask.erni

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!