L1CacheSystem 10.0.0
We lost the Task. What we saved in the variable has already been completed, and it's not what we expected.
See the version list below for details.
dotnet add package L1CacheSystem --version 10.0.0
NuGet\Install-Package L1CacheSystem -Version 10.0.0
<PackageReference Include="L1CacheSystem" Version="10.0.0" />
<PackageVersion Include="L1CacheSystem" Version="10.0.0" />
<PackageReference Include="L1CacheSystem" />
paket add L1CacheSystem --version 10.0.0
#r "nuget: L1CacheSystem, 10.0.0"
#:package L1CacheSystem@10.0.0
#addin nuget:?package=L1CacheSystem&version=10.0.0
#tool nuget:?package=L1CacheSystem&version=10.0.0
<h1 align="center"> <a>L1CacheSystem</a> </h1>
<h3 align="center">
</h3>
L1CacheSystem is the application’s internal cache. It was created to solve two problems that IMemoryCache couldn’t address:
- The ability to filter by all cache entities
- Blocking a specific resource by key
<h1 align="left"> <a>Usage</a> </h1>
The first thing you need to do is to import the packages:
- L1CacheSystem
- L1CacheSystem.Attributes;
- L1CacheSystem.Generator
Then, in the code, you need to declare your cache. Let’s say that, for example, we will cache the
MyValueclass.
public class MyValue
{
public long Id { get; set; }
public double Value { get; set; }
// other properties
public MyValue Copy()
{
return new MyValue
{
Id = this.Id,
Value = this.Value
};
}
}
public partial interface IServiceCacheSystem
{
}
// You can have as many EntityCacheType as you like, the number of different types is not limited.
[EntityCacheType(
cacheType: typeof(MyValue),
interfaceType: typeof(IServiceCacheSystem),
cacheName: "YouCacheName",
propAsKey: nameof(MyValue.Id),
metricCollectFlags: MetricCollectBits.AllBits, // what metrics for this specific cache need to collect
copyMethodName: nameof(MyValue.Copy),
copyMethodType: CopyMethodType.CopyMethodOnType)] // we also support: static method and copy-constructor
public partial class ServiceCacheSystem : L1CacheSystemBase, IServiceCacheSystem
{
public ServiceCacheSystem(
ILogger<ServiceCacheSystem> logger,
TimeProvider timeProvider,
IMeterFactory meterFactory)
: base(
logger,
timeProvider,
meterFactory)
{
}
}
Now you just need create and run system. For example in asp.net we can do this with that way:
public class CacheSystemHostedService : IHostedService
{
private readonly IL1CacheSystemBase _cacheSystem;
public CacheSystemHostedService(IL1CacheSystemBase cacheSystem)
{
_cacheSystem = cacheSystem;
}
public Task StartAsync(CancellationToken cancellationToken)
{
return _cacheSystem.StartAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return _cacheSystem.StopAsync(cancellationToken);
}
}
services.AddSingleton<ServiceCacheSystem>();
services.AddSingleton<IL1CacheSystemBase>((provider) => provider.GetRequiredService<ServiceCacheSystem>()); // IL1CacheSystemBase interface providet by L1CacheSystem nuget
services.AddSingleton<IServiceCacheSystem>((provider) => provider.GetRequiredService<ServiceCacheSystem>());
Settings is done. Now usage case:
public class MyValueService
{
private readonly ILogger<MyValueService> _logger;
private readonly IServiceCacheSystem _casheSystem;
public MyValueService(ILogger<MyValueService> logger, IServiceCacheSystem casheSystem)
{
_logger = logger;
_casheSystem = casheSystem;
}
public CreateValue(long id)
{
await using var lockGuard = new LockGuard(logger: _logger, parent: null);
// Since we are at the root of the call (parent == null), we will either gain ownership of the resource immediately,
// or at the moment when other calls stop owning it. If the waiting should not be infinite, pass a CancellationToken.
await _cacheSystem.GetYouCacheNameLockAsync(id, lockGuard, default);
// do whatever you want: your custom logic
// YouCacheName with Id = id nobody (except you) can change until lockGuard.Dispose not called
var value = new MyValue() { Id = id, Value = 7114 };
// If you need the value and plan to change it after this call, then pass it as `NotTransferred`
// Then CacheSystem will save a copy of it during the call (by copying it using the method you specified earlier, at the configuration stage).
_cacheSystem.SetYouCacheName(value.Id, value, valueOwnership: ValueOwnership.Transferred, expiry: null);
// if "expiry" is provided then casheSystem remove cache value from cache when key is expire
// The Set method is thread‑safe on its own; the lockGuard is only needed if you’re using something more complex than a simple set/get.
}
public DoRelatedWork(long id, ILockGuard? lockGuard = null)
{
// in this method we not shure what poinc its called
// maybe this is root call or maybe recudsive
await using var lockGuard = new LockGuard(_logger, lockGuard);
// If this is not the root (lockGuard != null) and the parent already has a lock on this key,
// then the lock counter inside lockGuard will simply be increased.
await _cacheSystem.GetYouCacheNameLockAsync(id, lockGuard, default);
// your custom logic
// On dispose lockGuard release all locks: decrease lock counter if this lockGuard not the owner of lock or release if is the owner
}
public MyValue GetMyValue(long id)
{
return _cacheSystem.GetYouCacheName(id, ReturnType.Reference); // or ReturnType.Copy
}
public MyValue[] GetAllMyValue(long id, Func<MyValue, bool>? predicate = null)
{
return _cacheSystem.GetAllYouCacheName(predicate, ReturnType.Reference); // or ReturnType.Copy
}
}
What metrics look like, using Grafana visualization as an example: <img width="2058" height="1147" alt="image" src="https://github.com/user-attachments/assets/96e0eff2-92b0-4958-a7b4-0268a9091a47" /> <img width="2062" height="1114" alt="image" src="https://github.com/user-attachments/assets/8fac71c5-ead0-412a-8b98-9d67df48cdc6" /> <img width="2068" height="1099" alt="image" src="https://github.com/user-attachments/assets/671f238c-366b-4b32-b590-780e317c7c5e" /> <img width="2066" height="1112" alt="image" src="https://github.com/user-attachments/assets/e916b4fa-e05f-4b91-81d7-4d13d0fee8af" /> <img width="2071" height="604" alt="image" src="https://github.com/user-attachments/assets/b39d5efd-5a2d-4427-9383-4f84d4bcd15d" /> <img width="2067" height="613" alt="image" src="https://github.com/user-attachments/assets/0ad45a57-2dd1-4087-aaf0-ce8731d3dc1b" />
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 is compatible. 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.14)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.14)
-
net9.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.