CacheProviders 1.0.1

.NET 5.0
Install-Package CacheProviders -Version 1.0.1
dotnet add package CacheProviders --version 1.0.1
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CacheProviders, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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 153 10/19/2021
1.0.0 121 10/19/2021