DragonHoard.InMemory
2.0.15
See the version list below for details.
dotnet add package DragonHoard.InMemory --version 2.0.15
NuGet\Install-Package DragonHoard.InMemory -Version 2.0.15
<PackageReference Include="DragonHoard.InMemory" Version="2.0.15" />
paket add DragonHoard.InMemory --version 2.0.15
#r "nuget: DragonHoard.InMemory, 2.0.15"
// Install DragonHoard.InMemory as a Cake Addin #addin nuget:?package=DragonHoard.InMemory&version=2.0.15 // Install DragonHoard.InMemory as a Cake Tool #tool nuget:?package=DragonHoard.InMemory&version=2.0.15
Dragon Hoard
Dragon Hoard is a fast, thread safe, developer friendly in-memory caching service.
Setting Up the Library
Dragon Hoard can be set up using the ServiceCollection extension depending on the cache you want to set up:
ServiceCollection.AddInMemoryHoard();
The above code adds the faster/lighter in-memory cache while:
ServiceCollection.AddMemoryCacheHoard();
That allows the system to wrap Microsoft.Extensions.Caching.Memory.MemoryCache. Note that they are in separate Nuget packages so you'll have to download the one that you want.
Basic Usage
The main class of interest is the Cache class found in DragonHoard.Core:
public class ExampleClass
{
public ExampleClass(Cache myCache)
{
MyCache = myCache;
}
private Cache MyCache { get; set; }
public void SomeMethod()
{
var Cache = MyCache.GetOrAddCache("CacheName");
}
}
The Cache object has a singleton lifespan and acts as a repository for your various caches. Note that it's generally a good idea to split caches based on their purpose so they can be treated accordingly and to reduce read/write contention. Once you have the ICache object that the GetOrAddCache method returns you have a couple of methods:
Cache.Set(...) // Used to set a key/value pair in the cache using various settings. You can specify absolute expiration, sliding, priority, size, and any tags that should be associated with the entry.
Cache.Remove(...) // Used to remove an entry based on the key.
Cache.TryGetValue(...) // Used to retrieve an entry from the cache based on the key.
Cache.RemoveByTag(...) // Will remove all entries that have been tagged with the corresponding string.
Cache.GetByTag(...) // Returns an array containing all entries that were tagged using the string.
Cache.Compact(...) // Used to remove a percentage of items from the cache.
The cache will clear out invalid items on a scheduled period based on the criteria you set. However the schedule may only kick off after you call one of the above methods depending on the type of cache. Similarly the determination of what gets removed when calling Compact is specific to the individual cache provider. For instance, the in-memory provider uses these steps:
- Remove items that have expired.
- Put items into buckets based on priority and then go off this:
- Remove items where absolute expiration is set going earliest to latest.
- Remove items where sliding expiration is set going earliest to latest.
- Remove items based on last accessed date/time.
Give Me Speed
For those wondering why you'd want to use this over the MemoryCache provided by Microsoft, considering the following using the following setup:
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1256 (1909/November2018Update/19H2)
Intel Core i7-9850H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.102
[Host] : .NET Core 5.0.2 (CoreCLR 5.0.220.61120, CoreFX 5.0.220.61120), X64 RyuJIT
DefaultJob : .NET Core 5.0.2 (CoreCLR 5.0.220.61120, CoreFX 5.0.220.61120), X64 RyuJIT
Adding and removing an item from the cache is about 4 times faster and uses about 1/5 of the memory overhead:
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
InMemory | 422.7 ns | 7.29 ns | 8.96 ns | 420.4 ns | 1.00 | 0.00 | 0.0401 | - | - | 256 B |
MicrosoftMemory | 2,123.0 ns | 179.06 ns | 527.96 ns | 1,906.8 ns | 3.96 | 0.29 | 0.2060 | - | - | 1296 B |
Retrieval of an item from the cache is about 50% faster:
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
InMemory | 153.9 ns | 3.06 ns | 7.39 ns | 151.5 ns | 1.00 | 0.00 | - | - | - | - |
MicrosoftMemory | 214.9 ns | 14.32 ns | 42.22 ns | 200.0 ns | 1.50 | 0.32 | - | - | - | - |
When creating/storing items at high throughput scenarios, the system is about 5x faster with reduced memory overhead even when aggressive cache cleanup is used:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
InMemory | 1.043 μs | 0.0934 μs | 0.2557 μs | 1.00 | 0.00 | 0.0362 | 0.0134 | - | 232 B |
MicrosoftMemory | 4.900 μs | 0.4043 μs | 1.1920 μs | 5.01 | 1.56 | 0.1755 | 0.0458 | - | 1200 B |
And updating an item already in the cache is 8x faster with 12x improvement on memory consumption:
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
InMemory | 257.2 ns | 15.47 ns | 45.12 ns | 241.6 ns | 1.00 | 0.00 | 0.0165 | - | - | 104 B |
MicrosoftMemory | 2,041.9 ns | 204.26 ns | 602.26 ns | 1,785.9 ns | 8.00 | 1.93 | 0.1907 | - | - | 1200 B |
Installation
The library is available via Nuget with the package name "DragonHoard.InMemory" or "DragonHoard.Microsoft.Extensions.Caching.Memory". To install it run the following command in the Package Manager Console:
Install-Package DragonHoard.InMemory
or
Install-Package DragonHoard.Microsoft.Extensions.Caching.Memory
Build Process
In order to build the library you may require the following:
- Visual Studio 2019
Other than that, just clone the project and you should be able to load the solution and build without too much effort.
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. |
-
net6.0
- DragonHoard.Core (>= 2.0.15)
- Microsoft.Extensions.Options (>= 7.0.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on DragonHoard.InMemory:
Package | Downloads |
---|---|
Inflatable
Inflatable is a simple ORM. |
|
sundial.core
Sundial is a profiler library used for both comparison of multiple bits of code and hot spot detection. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.1.0 | 33 | 11/24/2024 |
2.0.45 | 103 | 11/11/2024 |
2.0.44 | 78 | 11/4/2024 |
2.0.43 | 73 | 10/29/2024 |
2.0.42 | 73 | 10/29/2024 |
2.0.41 | 81 | 10/14/2024 |
2.0.40 | 91 | 10/9/2024 |
2.0.39 | 479 | 8/23/2024 |
2.0.38 | 291 | 8/1/2024 |
2.0.37 | 378 | 6/25/2024 |
2.0.36 | 269 | 6/17/2024 |
2.0.35 | 559 | 5/6/2024 |
2.0.34 | 222 | 5/1/2024 |
2.0.33 | 183 | 4/30/2024 |
2.0.32 | 113 | 4/29/2024 |
2.0.31 | 496 | 3/28/2024 |
2.0.30 | 246 | 3/14/2024 |
2.0.29 | 168 | 3/13/2024 |
2.0.28 | 215 | 3/5/2024 |
2.0.27 | 246 | 3/4/2024 |
2.0.26 | 128 | 2/27/2024 |
2.0.25 | 103 | 2/26/2024 |
2.0.24 | 146 | 2/14/2024 |
2.0.23 | 132 | 2/2/2024 |
2.0.22 | 112 | 1/29/2024 |
2.0.21 | 965 | 1/19/2024 |
2.0.20 | 162 | 1/10/2024 |
2.0.19 | 845 | 12/11/2023 |
2.0.18 | 412 | 11/17/2023 |
2.0.17 | 415 | 11/16/2023 |
2.0.16 | 269 | 11/6/2023 |
2.0.15 | 259 | 10/30/2023 |
2.0.14 | 519 | 9/18/2023 |
2.0.13 | 269 | 9/11/2023 |
2.0.12 | 304 | 9/5/2023 |
2.0.11 | 128 | 9/4/2023 |
2.0.10 | 253 | 8/31/2023 |
2.0.9 | 280 | 8/30/2023 |
2.0.8 | 185 | 8/29/2023 |
2.0.7 | 703 | 8/8/2023 |
2.0.6 | 119 | 8/7/2023 |
2.0.5 | 618 | 7/13/2023 |
2.0.4 | 156 | 7/12/2023 |
2.0.3 | 157 | 7/11/2023 |
2.0.2 | 259 | 2/23/2023 |
2.0.1 | 770 | 12/13/2022 |
2.0.0 | 295 | 12/12/2022 |
1.0.17 | 1,749 | 6/10/2022 |
1.0.15 | 1,119 | 1/11/2022 |
1.0.14 | 982 | 1/10/2022 |
1.0.13 | 431 | 12/7/2021 |
1.0.11 | 787 | 6/16/2021 |
1.0.9 | 900 | 5/21/2021 |
1.0.8 | 308 | 5/3/2021 |
1.0.7 | 333 | 2/6/2021 |
1.0.6 | 576 | 1/14/2021 |
1.0.5 | 336 | 1/14/2021 |
1.0.4 | 337 | 1/14/2021 |
1.0.3 | 347 | 1/14/2021 |
1.0.2 | 338 | 1/14/2021 |
1.0.1 | 346 | 1/14/2021 |