ThoughtStuff.Caching.FileSystem
2.1.0
dotnet add package ThoughtStuff.Caching.FileSystem --version 2.1.0
NuGet\Install-Package ThoughtStuff.Caching.FileSystem -Version 2.1.0
<PackageReference Include="ThoughtStuff.Caching.FileSystem" Version="2.1.0" />
<PackageVersion Include="ThoughtStuff.Caching.FileSystem" Version="2.1.0" />
<PackageReference Include="ThoughtStuff.Caching.FileSystem" />
paket add ThoughtStuff.Caching.FileSystem --version 2.1.0
#r "nuget: ThoughtStuff.Caching.FileSystem, 2.1.0"
#:package ThoughtStuff.Caching.FileSystem@2.1.0
#addin nuget:?package=ThoughtStuff.Caching.FileSystem&version=2.1.0
#tool nuget:?package=ThoughtStuff.Caching.FileSystem&version=2.1.0
ThoughtStuff.Caching
Injects look-through cache for configured services
The core, Azure, and file-system packages target .NET Standard 2.0 and 2.1. For applications on modern .NET, the 2.x packages support .NET 8 and later. The example and test projects require the .NET 10 SDK.
Quick Start
// Cache results returned from MySlowService in Mem Cache
services.AddMethodCaching()
.AddTransientWithCaching<IMySlowService, MySlowService, MyResult>();
var app = builder.Build();
// Configure method caching policies
var methodCachePolicies = app.Services.GetRequiredService<IMethodCacheOptionsLookup>();
methodCachePolicies.AddRelativeExpiration<IMySlowService>(TimeSpan.FromSeconds(30));
Details
Suppose you have a slow service that fetches information on books by ISBN.
public interface IBookInfoService
{
BookInfo GetBookInfo(string isbn);
}
class BookInfoService : IBookInfoService
{
public BookInfo GetBookInfo(string isbn) { ... }
}
The service is slow and book information changes infrequently, so you would like to cache it.
Rather than registering BookInfoService with .AddTransient you can use .AddTransientWithCaching.
services.AddMethodCaching()
.AddTransientWithCaching<IBookInfoService, BookInfoService, BookInfo>();
The above will register a caching proxy for IBookInfoService which will wrap BookInfoService.
The first call to get info on a particular book will use BookInfoService,
but subsequent calls to get info on the same book will return the cached result from the first call.
void Example([Inject] IBookInfoService bookInfoService)
{
var info1 = bookInfoService.GetBookInfo("1234"); // Slow. Saves cache entry using key "GetBookInfo(1234)"
var info2 = bookInfoService.GetBookInfo("1234"); // Fast. Returns cache entry found using key "GetBookInfo(1234)"
Assert.Equals(info1, info2);
}
Filesystem cache capacity
You can enable a best-effort size limit when registering the filesystem cache.
services.AddLocalFileTextCache(options =>
{
options.BaseDirectory = cacheDirectory;
options.SoftSizeLimitBytes = 512L * 1024 * 1024;
});
The limit counts published payload and metadata bytes. On a write, the store tries to make room by evicting expired entries first, then the least recently used entries. A value can be returned to the caller without being cached. An existing directory can start over the limit and is trimmed on subsequent writes.
When enabled the store tracks access times in a SQLite catalog. The catalog is built from existing files so the limit can be applied to an existing cache directory.
Use one process per cache directory when limiting is enabled. Instances in that process share accounting and access tracking. The package logs a warning through .NET logging if it detects another process.
Current Limitations
Single Return Type per Interface
Only one return type can be cached per service interface. This works well for services that implement a Single Responsibility, but not for larger classes. Often times simplified interface(s) can be created. The simplified interfaces may only have 1 function each and all be implemented by the large class.
Transient Lifetime
Only the Transient lifetime is implemented.
Cache Key uses Interface Name + Method Name + Arguments
A cache key is automatically generated from the method invocation based on the method name and passed arguments.
For example: "IMyBookService.GetBookInfo('The Great Gatsby')".
Other environment information is not included.
Be careful in multi-user or multi-tenant environments not to cache user-specific or tenant-specific results.
For example, caching a method like GetCurrentUserFavoriteColor() would end up returning the same color for all users.
To make it amenable to method caching change it to GetUserFavoriteColor(userId).
The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.
— Fred P. Brooks
| 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 is compatible. |
| .NET Framework | net461 was computed. 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. |
-
.NETStandard 2.0
- Microsoft.Data.Sqlite (>= 10.0.15)
- Polly (>= 8.9.1)
- ThoughtStuff.Caching (>= 2.1.0)
-
.NETStandard 2.1
- Microsoft.Data.Sqlite (>= 10.0.15)
- Polly (>= 8.9.1)
- ThoughtStuff.Caching (>= 2.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.