CacheProviders 1.0.1

dotnet add package CacheProviders --version 1.0.1
NuGet\Install-Package CacheProviders -Version 1.0.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="CacheProviders" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CacheProviders --version 1.0.1
#r "nuget: CacheProviders, 1.0.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 CacheProviders as a Cake Addin
#addin nuget:?package=CacheProviders&version=1.0.1

// Install CacheProviders as a Cake Tool
#tool nuget:?package=CacheProviders&version=1.0.1

Cache Providers

Helper functions for IMemoryCache & IDistributedCache, to reduce duplicate code.

Install

Package Manager

Install-Package CacheProviders -Version 1.0.0

.NET CLI

dotnet add package CacheProviders --version 1.0.0

Introduction

The purpose of these functions is to validate there is a value within the cache, if not a delegate function is fired off which will automatically add the data to the cache.

All functions are built with a generic type, so complex objects can be used.

Example

There is a service collection extension to add both Memory and Distributed providers

services.AddCacheProviders()

Memory Cache

        private MemoryCacheProvider _memoryCacheProvider;

        public MemoryCacheTests(MemoryCacheProvider memoryCacheProvider)
        {
            _memoryCacheProvider = memoryCacheProvider;
        }

        public object BasicUsage()
        {
            return _memoryCacheProvider.TryGetValue("My Key", () =>
            {
                // Get Actual Value if key doesn't exist.
            });
        }
        
        // Example of getting a string value
        public string GetName()
        {
            return _memoryCacheProvider.TryGetValue("_MyName", () => "Engelbert Humperdinck");
        }
        
        // Each function has an async equivilent
        public Task<string> GetNameAsync()
        {
            return _memoryCacheProvider.TryGetValueAsync("_MyName", () => Task.FromResult("Engelbert Humperdinck"));
        }
        
        // You can pass in the cache options
        public string GetNameWithOptions()
        {
            var options = new MemoryCacheEntryOptions()
                .SetSlidingExpiration(TimeSpan.FromMinutes(15));
            
            return _memoryCacheProvider.TryGetValue("_MyName", () => "Engelbert Humperdinck", options);
        }

Distributed Cache

        private DistributedCacheProvider _distributedCacheProvider;

        public MemoryCacheTests(DistributedCacheProvider distributedCacheProvider)
        {
            _distributedCacheProvider = distributedCacheProvider;
        }
        
        public object BasicUsage()
        {
            return _distributedCacheProvider.TryGetValue("My Key", () =>
            {
                // Get Actual Value if key doesn't exist.
            });
        }
        
        // Example of getting a string value
        public string GetName()
        {
            return _distributedCacheProvider.TryGetValue("_MyName", () => "Engelbert Humperdinck");
        }
        
        // Each function has an async equivilent
        public Task<string> GetNameAsync()
        {
            return _distributedCacheProvider.TryGetValueAsync("_MyName", () => Task.FromResult("Engelbert Humperdinck"));
        }

        // You can pass in the cache options
        public string GetNameWithOptions()
        {
            var options = new DistributedCacheEntryOptions()
                .SetSlidingExpiration(TimeSpan.FromMinutes(15));
            
            return _distributedCacheProvider.TryGetValue("_MyName", () => "Engelbert Humperdinck", options);
        }

Todo

[ ] Create generic options for both types caching.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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
1.0.1 328 10/19/2021
1.0.0 283 10/19/2021