SolTechnology.Core.Cache 0.2.1

dotnet add package SolTechnology.Core.Cache --version 0.2.1                
NuGet\Install-Package SolTechnology.Core.Cache -Version 0.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SolTechnology.Core.Cache" Version="0.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SolTechnology.Core.Cache --version 0.2.1                
#r "nuget: SolTechnology.Core.Cache, 0.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SolTechnology.Core.Cache as a Cake Addin
#addin nuget:?package=SolTechnology.Core.Cache&version=0.2.1

// Install SolTechnology.Core.Cache as a Cake Tool
#tool nuget:?package=SolTechnology.Core.Cache&version=0.2.1                

Overview

The SolTechnology.Core.Cache library provides functionality needed for task caching. It handles needed services registration and configuration. It relies on IMemoryCache. The use case is to cache parts of the code which are time consuming and the result does not change. Ex: external http calls for non-changing data.

Registration

For installing the library, reference SolTechnology.Core.Cache nuget package and invoke AddCache() service collection extension method:

services.AddCache();

Configuration

  1. The first option is to create an appsettings.json section:
  "Configuration": {
    "CacheConfiguration": {
        "ExpirationMode": "Absolute or Sliding",
        "ExpirationSeconds": 300
     }
  }
  1. Alternatevely the same settings can be provided by optional parameter during registration:
var cacheConfiguration = new CacheConfiguration
{
    ExpirationMode = "Absolute",
	ExpirationSeconds= 300
};

builder.Services.AddCache(cacheConfiguration);
  1. If not provided, the default configuration will be applied, which is Sliding ExpirationMode and 300 seconds expiration time. That means if the cache was not hit in 5 mins, it will be cleared.

Usage

  1. Inject ILazyTaskCache into your service
        public SyncPlayer(
            IFootballDataApiClient footballDataApiClient,
            IPlayerRepository playerRepository,
            IApiFootballApiClient apiFootballApiClient,
            ILazyTaskCache lazyTaskCache)
        {
            _footballDataApiClient = footballDataApiClient;
            _playerRepository = playerRepository;
            _apiFootballApiClient = apiFootballApiClient;
            _lazyTaskCache = lazyTaskCache;
        }
  1. Cache repeatable operation and it's key
         var clientPlayer = await _lazyTaskCache.GetOrAdd(playerIdMap.FootballDataId, _footballDataApiClient.GetPlayerById);
  1. The key is supposed to be a complex object (ex: command or query), to avoid returning incorrect cache item
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 179 8/7/2023
0.2.0 218 12/28/2022