CypherPotato.MemoryCacheStorage
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CypherPotato.MemoryCacheStorage --version 1.0.0
NuGet\Install-Package CypherPotato.MemoryCacheStorage -Version 1.0.0
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="CypherPotato.MemoryCacheStorage" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CypherPotato.MemoryCacheStorage --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CypherPotato.MemoryCacheStorage, 1.0.0"
#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 CypherPotato.MemoryCacheStorage as a Cake Addin #addin nuget:?package=CypherPotato.MemoryCacheStorage&version=1.0.0 // Install CypherPotato.MemoryCacheStorage as a Cake Tool #tool nuget:?package=CypherPotato.MemoryCacheStorage&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MemoryCacheStorage
Written by CypherPotato
This package provides the ability to store objects for a period of time and retrieve them later. If the timeout time is exceeded, the file is dereferenced and the GC does the work of collecting the object if it is not referenced later.
There is no timer or recurrence. No objects are copied or serialized. The class stores the reference to the object, and after the time it runs in an asynchronous task, the pointer is released.
Setting objects
// initialize the new cache object
var cache = new MemoryCacheStorage();
cache.DefaultExpirity = TimeSpan.FromSeconds(30);
// Caches the object below for the default time defined above (30 seconds)
// and gets an ID for the stored object.
int id = cache.Set(myCachedObject);
// Caches the object for 30 seconds, sets an tag on it and gets the ID
id = cache.Set(myCachedObject, "my-tag");
// Caches the object for 5 minutes (override the default), set a tag
// and returns the id
id = cache.Set(myCachedObject, TimeSpan.FromMinutes(5), "tag");
// Caches an object using multiple tags
id = cache.Set(myCachedObject, TimeSpan.FromHours(3), new string[] { "tag1", "tag2" });
Retrieves cacheds objects
// tries to retrieve an cached object by their ID
if (!cache.TryGetValue(id, out object? cachedValue)) {
// couldn't find the cached object
} else {
// found the cached object
}
// tries to retrieve an cached object by one of their tags
if (!cache.TryGetValue("tag1", out object? cachedValue)) {
// couldn't find the cached object by the tag
} else {
// found the cached object
}
// tries to get an cached object, and converts it on fly
if (cache.TryGetValue("tag3", out MyClassObject? cachedValue)) {
// cachedValue was found
} else {
// not found
}
// retrieves multiple cached objects by tag
object?[] cachedObjects = cache.GetValuesByTag("tag1").ToArray();
// retrieves multiple cached objects by predicate
object?[] cachedObjects = cache
.GetValuesByTag(tags => tags.Contains("tag1") && tags.Contains("tag2"))
.ToArray();
// gets the count of cached objects
int cachedObjects = cache.Count;
Invalidate objects
// force immediate invalidation of object by one of their tags
int invalidatedObjects = cache.Invalidate("tag1");
// force immediate invalidation of object by one of their ID
int invalidatedObjects = cache.Invalidate(3);
// force immediate invalidation of all objects matching the tag
int invalidatedObjects = cache.Invalidate(tags => tags.Length > 3);
// clears the cache
cache.Clear();
Product | Versions 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.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.