CodingCat.Cache
0.1.1
See the version list below for details.
dotnet add package CodingCat.Cache --version 0.1.1
NuGet\Install-Package CodingCat.Cache -Version 0.1.1
<PackageReference Include="CodingCat.Cache" Version="0.1.1" />
<PackageVersion Include="CodingCat.Cache" Version="0.1.1" />
<PackageReference Include="CodingCat.Cache" />
paket add CodingCat.Cache --version 0.1.1
#r "nuget: CodingCat.Cache, 0.1.1"
#:package CodingCat.Cache@0.1.1
#addin nuget:?package=CodingCat.Cache&version=0.1.1
#tool nuget:?package=CodingCat.Cache&version=0.1.1
CodingCat.Cache
Provides handy API to manage caches across different sources.
Using the IKeyBuilder
When doing the caches, sometimes the key to the cache is quite troubling us when a programmer uses contact-email to represent the company's contact email, but the other programmer uses the same key for the CS team email.
We then figured out the better solution maybe enforce all keys require a prefix such as var key = "PageAboutUs-contact-email"; or var key = "PageContactUs-contact-email";
Moreover, when a project needs a distributed cache server (e.g. Redis), but its budget cannot affort both the prod & staging, we would also like to prepend the environment to the keys.
With the IKeyBuilder, we could do this almost without the keyboard.
var environment = Environment.Current.ToString(); // -- PRODUCTION or STAGING or DEBUG
var usingKey = new KeyBuilder<Product>(environment) // -- or new KeyBuilder(this.GetType(), environment);
.UseKey("mobile")
.AddSegments("Apple", "iPhone XR"); // -- CodingCat.Demo.Models.Product-DEBUG-Apple-iPhone XR
or
var usingKey = new KeyBuilder<Product>("DEV")
.UseKey("GetProductById")
.AddSegment(productId);
or
var usingKey = new KeyBuilder<Product>(environment)
.UseKey(nameof(GetAllFeaturedProducts)); // -- only the UseKey is required
Using the IStorage
To allow easier dependency injection, we would like to wrap the caching classes into an interface thus we could replace the dependencies without any worries. Currently we support MemoryCache and StackExchange.Redis
var storage = new Storage(TimeSpan.FromSeconds(60)); // -- Memory.Storage
storage.Add(usingKey, JsonConvert.SerializeObject(product));
var product = JsonConvert.DeserializeObject<Product>(
storage.Get(usingKey)
);
storage.Delete(usingKey);
/// ---- ////
var usingKey = this.keyBuilder
.UseKey(nameof(GetProductByProductId))
.AddSegment(productId);
var product = JsonConvert.DeserializedObject<Product>(
storage.Get(
usingKey,
() => JsonConvert.SerializeObject(
this.Database.Products.GetById(productId)
) // -- callback if not found
)
);
What if micro-services?
When we are coding for some micro-services, one case is the cache is already exists on the distributed cache server (e.g. Redis), however, we would also like to cache the data within the service itself, in order to avoid high traffic to the cache server when we have a million instances of the services running.
In this case, there is a IStorageManager which allows developers to consume more than 1 cache sources and control its behavior.
using MemoryStorage = CodingCat.Cache.Memory.Storage;
using RedisStorage = CodingCat.Cache.Redis.Storage;
....
public IStorageManager StorageManager { get; }
public Constructor() {
var expiry = TimeSpan.FromSeconds(60);
var memoryStorage = new MemoryStorage(expiry);
var redisStorage = new RedisStorage(
this.redisConnection.GetDatabase(),
expiry
);
// -- Default: if the cache is found in fallback storages but not the default storage, only return the value but not saving into the default storage
// -- SaveFromFallback: if the cache is found in the fallback storages but not the default storage, the manager will save the value from fallback to the default storage
this.StorageManager = new StorageManager(FallbackPolicy.SaveFromFallback)
.SetDefault(memoryStorage)
.AddFallback(redisStorage);
}
...
var product = this.StorageManager.Get(usingKey); // -- Serialized JSON
var featuredProducts = this.StorageManager
.Get(otherUsingKey, FallbackPolicy.Default); // -- Support override the behavior!
var comments = this.StorageManager
.Get(
commentsUsingKey,
() => this.SocialNetworkProvider.GetCommentsOf(id) // -- Need to deserialize from JSON before retun
);
Target Frameworks
- .Net 4.6.1
- .Net Standard 2.0
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.1
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on CodingCat.Cache:
| Package | Downloads |
|---|---|
|
CodingCat.Cache.Redis
Package Description |
|
|
CodingCat.Cache.Memory
Package Description |
|
|
CodingCat.Cache.Extensions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.