├── .gitattributes ├── .gitignore ├── Licence.txt ├── ReadMe.md ├── Source ├── PersistentMemoryCache.Tests │ ├── PersistentMemoryCache.Tests.csproj │ └── Tests.cs ├── PersistentMemoryCache.sln └── PersistentMemoryCache │ ├── IPersistentStore.cs │ ├── Infrastructure │ └── GcNotification.cs │ ├── Internal │ ├── LiteDbCacheEntry.cs │ └── PersistentLiteDatabase.cs │ ├── LiteDbOptions.cs │ ├── LiteDbStore.cs │ ├── PersistentCacheEntry.cs │ ├── PersistentMemoryCache.cs │ ├── PersistentMemoryCache.csproj │ └── PersistentMemoryCacheOptions.cs └── appveyor.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | 217 | */packages/* 218 | !*/packages/repositories.config 219 | */.vs/* 220 | 221 | project.lock.json -------------------------------------------------------------------------------- /Licence.txt: -------------------------------------------------------------------------------- 1 | Copyright 2016 Joel Weiss 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | [![Build status](https://ci.appveyor.com/api/projects/status/0sfvtqnag0x50pax?svg=true)](https://ci.appveyor.com/project/joelweiss/persistentmemorycache) 2 | [![NuGet Badge](https://buildstats.info/nuget/PersistentMemoryCache?includePreReleases=true)](https://www.nuget.org/packages/PersistentMemoryCache/) 3 | 4 | # Persistent Memory Cache 5 | 6 | Caches in Memory but also to Disk, so cache is persistent across restarts, build on Top of [Microsoft.Extensions.Caching.Abstractions](https://github.com/aspnet/Caching) and [LiteDb](https://github.com/mbdavid/LiteDB) 7 | 8 | # Installation 9 | ```powershell 10 | PM> Install-Package PersistentMemoryCache -Pre 11 | ``` 12 | # Example 13 | ```csharp 14 | IMemoryCache cache = new PersistentMemoryCache(new PersistentMemoryCacheOptions("Test", new LiteDbStore(new LiteDbOptions("Test.db")))); 15 | 16 | string key = "TestKey"; 17 | string value = "TestValue"; 18 | cache.Set(key, value); 19 | 20 | var retrieve = cache.Get(key); 21 | ``` 22 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache.Tests/PersistentMemoryCache.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | netcoreapp2.0 4 | 5 | Joel Weiss 6 | PersistentMemoryCache.Tests 7 | 8 | PersistentMemoryCache.Tests 9 | PersistentMemoryCache.Tests 10 | PersistentMemoryCache.Tests 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache.Tests/Tests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Caching.Memory; 2 | using System.Collections.Generic; 3 | using Xunit; 4 | using FluentAssertions; 5 | 6 | namespace PersistentMemoryCache.Tests 7 | { 8 | public class Tests 9 | { 10 | private static PersistentMemoryCache GetCache() => new PersistentMemoryCache(new PersistentMemoryCacheOptions("Test", new LiteDbStore(new LiteDbOptions("Test.db")))); 11 | 12 | [Fact] 13 | public void InsertAndRetrieveString() 14 | { 15 | IMemoryCache cache = GetCache(); 16 | string key = "TestKey"; 17 | string value = "TestValue"; 18 | cache.Set(key, value); 19 | cache.Dispose(); 20 | cache = null; 21 | cache = GetCache(); 22 | 23 | string result = cache.Get(key); 24 | result.Should().NotBeNull(); 25 | result.Should().Equals("TestValue"); 26 | } 27 | 28 | 29 | [Fact] 30 | public void InsertAndRetrieveListOfStrings() 31 | { 32 | IMemoryCache cache = GetCache(); 33 | string key = "TestListKey"; 34 | List value = new List { "Value1", "Value2" }; 35 | cache.Set(key, value); 36 | cache.Dispose(); 37 | cache = null; 38 | cache = GetCache(); 39 | 40 | var result = cache.Get>(key); 41 | result.Should().NotBeNull(); 42 | result.ShouldBeEquivalentTo(value); 43 | } 44 | 45 | [Fact] 46 | public void InsertAndRetrieveEmptyList() 47 | { 48 | IMemoryCache cache = GetCache(); 49 | string key = "TestEmptyListKey"; 50 | List value = new List(); 51 | cache.Set(key, value); 52 | cache.Dispose(); 53 | cache = null; 54 | cache = GetCache(); 55 | 56 | var result = cache.Get>(key); 57 | result.Should().NotBeNull(); 58 | result.ShouldBeEquivalentTo(value); 59 | } 60 | 61 | [Fact] 62 | public void InsertAndRetrieveCustomType() 63 | { 64 | IMemoryCache cache = GetCache(); 65 | string key = "TestCustomTypeKey"; 66 | Customer value = new Customer { CustomerId = 1, Name = "Foo" }; 67 | cache.Set(key, value); 68 | cache.Dispose(); 69 | cache = null; 70 | cache = GetCache(); 71 | 72 | var result = cache.Get(key); 73 | result.Should().NotBeNull(); 74 | result.ShouldBeEquivalentTo(value); 75 | } 76 | 77 | public class Customer 78 | { 79 | public int CustomerId { get; set; } 80 | public string Name { get; set; } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.15 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersistentMemoryCache.Tests", "PersistentMemoryCache.Tests\PersistentMemoryCache.Tests.csproj", "{4CAD6760-AABF-4585-B7CF-EE54558862BE}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersistentMemoryCache", "PersistentMemoryCache\PersistentMemoryCache.csproj", "{5425D53C-1FD5-4744-9512-5F9B63B7736A}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {4CAD6760-AABF-4585-B7CF-EE54558862BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {4CAD6760-AABF-4585-B7CF-EE54558862BE}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {4CAD6760-AABF-4585-B7CF-EE54558862BE}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {4CAD6760-AABF-4585-B7CF-EE54558862BE}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {5425D53C-1FD5-4744-9512-5F9B63B7736A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {5425D53C-1FD5-4744-9512-5F9B63B7736A}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {5425D53C-1FD5-4744-9512-5F9B63B7736A}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {5425D53C-1FD5-4744-9512-5F9B63B7736A}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {FE7B041C-75CA-41A1-A515-9A74891BA6D9} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/IPersistentStore.cs: -------------------------------------------------------------------------------- 1 | using PersistentMemoryCache.Internal; 2 | using System.Collections.Generic; 3 | 4 | namespace PersistentMemoryCache 5 | { 6 | public interface IPersistentStore 7 | { 8 | int AddEntry(Internal.LiteDbCacheEntry entry); 9 | Internal.LiteDbCacheEntry LoadEntry(int key); 10 | List LoadEntries(string cacheName); 11 | void RemoveEntry(int id); 12 | bool UpdateEntry(int key, Internal.LiteDbCacheEntry entry); 13 | } 14 | } -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/Infrastructure/GcNotification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Microsoft.Extensions.Internal 4 | { 5 | /// 6 | /// Registers a callback that fires each time a Gen2 garbage collection occurs, 7 | /// presumably due to memory pressure. 8 | /// For this to work no components can have a reference to the instance. 9 | /// 10 | public class GcNotification 11 | { 12 | private readonly Func _callback; 13 | private readonly object _state; 14 | private readonly int _initialCollectionCount; 15 | 16 | private GcNotification(Func callback, object state) 17 | { 18 | _callback = callback; 19 | _state = state; 20 | _initialCollectionCount = GC.CollectionCount(2); 21 | } 22 | 23 | public static void Register(Func callback, object state) 24 | { 25 | var notification = new GcNotification(callback, state); 26 | } 27 | 28 | ~GcNotification() 29 | { 30 | bool reRegister = true; 31 | try 32 | { 33 | // Only invoke the callback after this instance has made it into gen2. 34 | if (_initialCollectionCount < GC.CollectionCount(2)) 35 | { 36 | reRegister = _callback(_state); 37 | } 38 | } 39 | catch (Exception) 40 | { 41 | // Never throw from the finalizer thread 42 | } 43 | 44 | if (reRegister && !Environment.HasShutdownStarted) 45 | { 46 | GC.ReRegisterForFinalize(this); 47 | } 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/Internal/LiteDbCacheEntry.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Caching.Memory; 2 | using System; 3 | using System.Collections.Concurrent; 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | 7 | namespace PersistentMemoryCache.Internal 8 | { 9 | public abstract class LiteDbCacheEntry 10 | { 11 | private static ConcurrentDictionary _Constructors = new ConcurrentDictionary(); 12 | private static Type _LiteDbCacheEntryOpenType = typeof(LiteDbCacheEntry<>); 13 | private static Type[] _EmptyTypesArray = new Type[0]; 14 | 15 | private TimeSpan? _SlidingExpiration; 16 | 17 | public int Id { get; set; } 18 | public string CacheName { get; set; } 19 | public CacheItemPriority Priority { get; set; } 20 | public object Key { get; set; } 21 | internal DateTimeOffset LastAccessed { get; set; } 22 | public DateTimeOffset? AbsoluteExpiration { get; set; } 23 | public TimeSpan? SlidingExpiration 24 | { 25 | get 26 | { 27 | if (_SlidingExpiration <= TimeSpan.Zero) 28 | { 29 | throw new ArgumentOutOfRangeException(nameof(SlidingExpiration), _SlidingExpiration, "The sliding expiration value must be positive."); 30 | } 31 | return _SlidingExpiration; 32 | } 33 | set 34 | { 35 | if (value <= TimeSpan.Zero) 36 | { 37 | throw new ArgumentOutOfRangeException(nameof(SlidingExpiration), value, "The sliding expiration value must be positive."); 38 | } 39 | _SlidingExpiration = value; 40 | } 41 | } 42 | 43 | public static LiteDbCacheEntry ConstructCacheEntry(Type type) => (LiteDbCacheEntry)_Constructors.GetOrAdd(type, cacheType => 44 | { 45 | Type cacheEntryClosedType = _LiteDbCacheEntryOpenType.MakeGenericType(type); 46 | ConstructorInfo constructor = cacheEntryClosedType.GetConstructor(_EmptyTypesArray); 47 | Type delegateType = typeof(Func<>).MakeGenericType(cacheEntryClosedType); 48 | LambdaExpression lambda = Expression.Lambda(delegateType, Expression.New(constructor)); 49 | return lambda.Compile(); 50 | }).DynamicInvoke(); 51 | 52 | public abstract object GetValue(); 53 | public abstract void SetValue(object value); 54 | } 55 | 56 | public class LiteDbCacheEntry : LiteDbCacheEntry 57 | { 58 | public T Value { get; set; } 59 | 60 | public override object GetValue() => Value; 61 | 62 | public override void SetValue(object value) => Value = (T)value; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/Internal/PersistentLiteDatabase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using LiteDB; 3 | using System.IO; 4 | 5 | namespace PersistentMemoryCache.Internal 6 | { 7 | internal class PersistentLiteDatabase : LiteDB.LiteDatabase 8 | { 9 | static PersistentLiteDatabase() 10 | { 11 | BsonMapper.Global.RegisterType 12 | ( 13 | serialize: (ts) => new BsonValue(ts.TotalMilliseconds), 14 | deserialize: (bson) => TimeSpan.FromMilliseconds(bson.AsInt32) 15 | ); 16 | BsonMapper.Global.RegisterType 17 | ( 18 | serialize: (dto) => new BsonValue(dto.UtcDateTime), 19 | deserialize: (bson) => bson.AsDateTime.ToUniversalTime() 20 | ); 21 | } 22 | 23 | /// 24 | /// Starts LiteDB database using a connection string for file system database 25 | /// 26 | internal PersistentLiteDatabase(string connectionString, BsonMapper mapper = null) : base(connectionString, mapper) 27 | { 28 | 29 | } 30 | 31 | /// 32 | /// Initialize database using any read/write Stream (like MemoryStream) 33 | /// 34 | internal PersistentLiteDatabase(Stream stream, BsonMapper mapper = null) : base(stream, mapper) 35 | { 36 | 37 | } 38 | 39 | /// 40 | /// Starts LiteDB database using full parameters 41 | /// 42 | internal PersistentLiteDatabase(IDiskService diskService, BsonMapper mapper = null) : base(diskService, mapper) 43 | { 44 | 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/LiteDbOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PersistentMemoryCache 4 | { 5 | public class LiteDbOptions 6 | { 7 | public LiteDbOptions(string fileName) 8 | { 9 | if (fileName == null) 10 | { 11 | throw new ArgumentNullException(nameof(fileName)); 12 | } 13 | FileName = fileName; 14 | } 15 | 16 | public string FileName { get; } 17 | } 18 | } -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/LiteDbStore.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using PersistentMemoryCache.Internal; 4 | using LiteDB; 5 | 6 | namespace PersistentMemoryCache 7 | { 8 | public class LiteDbStore : IPersistentStore 9 | { 10 | private const string _CollectionName = "PersistedCacheEntry"; 11 | private readonly string _ConnectionString; 12 | 13 | public LiteDbStore(LiteDbOptions options) 14 | { 15 | _ConnectionString = $"filename={options.FileName};upgrade=true"; 16 | using (var db = new PersistentLiteDatabase(_ConnectionString)) 17 | { 18 | var collection = db.GetCollection(_CollectionName); 19 | collection.EnsureIndex(pce => pce.CacheName); 20 | } 21 | } 22 | 23 | public int AddEntry(Internal.LiteDbCacheEntry entry) 24 | { 25 | using (var db = new PersistentLiteDatabase(_ConnectionString)) 26 | { 27 | var collection = db.GetCollection(_CollectionName); 28 | return collection.Insert(entry).AsInt32; 29 | } 30 | } 31 | 32 | public List LoadEntries(string cacheName) 33 | { 34 | using (var db = new PersistentLiteDatabase(_ConnectionString)) 35 | { 36 | var collection = db.GetCollection(_CollectionName); 37 | return collection.Find(pce => pce.CacheName == cacheName).ToList(); 38 | } 39 | } 40 | 41 | public Internal.LiteDbCacheEntry LoadEntry(int key) 42 | { 43 | using (var db = new PersistentLiteDatabase(_ConnectionString)) 44 | { 45 | var collection = db.GetCollection(_CollectionName); 46 | return collection.FindById(new BsonValue(key)); 47 | } 48 | } 49 | 50 | public bool UpdateEntry(int key, Internal.LiteDbCacheEntry entry) 51 | { 52 | using (var db = new PersistentLiteDatabase(_ConnectionString)) 53 | { 54 | var collection = db.GetCollection(_CollectionName); 55 | return collection.Update(new BsonValue(key), entry); 56 | } 57 | } 58 | 59 | public void RemoveEntry(int id) 60 | { 61 | using (var db = new LiteDatabase(_ConnectionString)) 62 | { 63 | var collection = db.GetCollection(_CollectionName); 64 | collection.Delete(new BsonValue(id)); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/PersistentCacheEntry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.Primitives; 6 | using Microsoft.Extensions.Caching.Memory; 7 | using System.ComponentModel; 8 | using System.Runtime.CompilerServices; 9 | 10 | namespace PersistentMemoryCache 11 | { 12 | public class PersistentCacheEntry : ICacheEntry, INotifyPropertyChanged 13 | { 14 | private bool _Added = false; 15 | private static readonly Action _ExpirationCallback = ExpirationTokensExpired; 16 | private readonly Action _NotifyCacheOfExpiration; 17 | private readonly Action _NotifyCacheEntryDisposed; 18 | private IList _ExpirationTokenRegistrations; 19 | private EvictionReason _EvictionReason; 20 | private IList _PostEvictionCallbacks; 21 | private bool _IsExpired; 22 | internal IList _ExpirationTokens; 23 | 24 | internal readonly object _Lock = new object(); 25 | 26 | internal DateTimeOffset? _AbsoluteExpiration; 27 | internal TimeSpan? _AbsoluteExpirationRelativeToNow; 28 | private TimeSpan? _SlidingExpiration; 29 | DateTimeOffset _LastAccessed; 30 | object _Value; 31 | CacheItemPriority _Priority; 32 | 33 | public event PropertyChangedEventHandler PropertyChanged; 34 | 35 | internal PersistentCacheEntry(object key, Action notifyCacheEntryDisposed, Action notifyCacheOfExpiration) 36 | { 37 | if (key == null) 38 | { 39 | throw new ArgumentNullException(nameof(key)); 40 | } 41 | if (notifyCacheEntryDisposed == null) 42 | { 43 | throw new ArgumentNullException(nameof(notifyCacheEntryDisposed)); 44 | } 45 | if (notifyCacheOfExpiration == null) 46 | { 47 | throw new ArgumentNullException(nameof(notifyCacheOfExpiration)); 48 | } 49 | 50 | Key = key; 51 | _NotifyCacheEntryDisposed = notifyCacheEntryDisposed; 52 | _NotifyCacheOfExpiration = notifyCacheOfExpiration; 53 | 54 | Priority = CacheItemPriority.Normal; 55 | } 56 | 57 | private void SetValue(T value, ref T field, [CallerMemberName]string propertyName = "") 58 | { 59 | if (!Equals(field, value)) 60 | { 61 | field = value; 62 | if (_Added) 63 | { 64 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 65 | } 66 | } 67 | } 68 | 69 | /// 70 | /// Gets or sets an absolute expiration date for the cache entry. 71 | /// 72 | public DateTimeOffset? AbsoluteExpiration 73 | { 74 | get 75 | { 76 | return _AbsoluteExpiration; 77 | } 78 | set 79 | { 80 | SetValue(value, ref _AbsoluteExpiration); 81 | } 82 | } 83 | 84 | /// 85 | /// Gets or sets an absolute expiration time, relative to now. 86 | /// 87 | public TimeSpan? AbsoluteExpirationRelativeToNow 88 | { 89 | get 90 | { 91 | return _AbsoluteExpirationRelativeToNow; 92 | } 93 | set 94 | { 95 | if (value <= TimeSpan.Zero) 96 | { 97 | throw new ArgumentOutOfRangeException(nameof(AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive."); 98 | } 99 | SetValue(value, ref _AbsoluteExpirationRelativeToNow); 100 | } 101 | } 102 | 103 | /// 104 | /// Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed. 105 | /// This will not extend the entry lifetime beyond the absolute expiration (if set). 106 | /// 107 | public TimeSpan? SlidingExpiration 108 | { 109 | get 110 | { 111 | return _SlidingExpiration; 112 | } 113 | set 114 | { 115 | if (value <= TimeSpan.Zero) 116 | { 117 | throw new ArgumentOutOfRangeException( 118 | nameof(SlidingExpiration), 119 | value, 120 | "The sliding expiration value must be positive."); 121 | } 122 | SetValue(value, ref _SlidingExpiration); 123 | } 124 | } 125 | 126 | /// 127 | /// Gets the instances which cause the cache entry to expire. 128 | /// 129 | public IList ExpirationTokens 130 | { 131 | get 132 | { 133 | if (_ExpirationTokens == null) 134 | { 135 | _ExpirationTokens = new List(); 136 | } 137 | return _ExpirationTokens; 138 | } 139 | } 140 | 141 | /// 142 | /// Gets or sets the callbacks will be fired after the cache entry is evicted from the cache. 143 | /// 144 | public IList PostEvictionCallbacks 145 | { 146 | get 147 | { 148 | if (_PostEvictionCallbacks == null) 149 | { 150 | _PostEvictionCallbacks = new List(); 151 | } 152 | 153 | return _PostEvictionCallbacks; 154 | } 155 | } 156 | 157 | /// 158 | /// Gets or sets the priority for keeping the cache entry in the cache during a 159 | /// memory pressure triggered cleanup. The default is . 160 | /// 161 | public CacheItemPriority Priority 162 | { 163 | get 164 | { 165 | return _Priority; 166 | } 167 | set 168 | { 169 | SetValue(value, ref _Priority); 170 | } 171 | } 172 | 173 | public object Key { get; private set; } 174 | 175 | public object Value 176 | { 177 | get 178 | { 179 | return _Value; 180 | } 181 | set 182 | { 183 | SetValue(value, ref _Value); 184 | } 185 | } 186 | 187 | internal DateTimeOffset LastAccessed 188 | { 189 | get 190 | { 191 | return _LastAccessed; 192 | } 193 | 194 | set 195 | { 196 | SetValue(value, ref _LastAccessed); 197 | } 198 | } 199 | 200 | internal int? PersistentStoreId { get; set; } 201 | 202 | public long? Size { get; set; } 203 | 204 | public void Dispose() 205 | { 206 | if (!_Added) 207 | { 208 | _Added = true; 209 | _NotifyCacheEntryDisposed(this); 210 | } 211 | } 212 | 213 | internal bool CheckExpired(DateTimeOffset now) 214 | { 215 | return _IsExpired || CheckForExpiredTime(now) || CheckForExpiredTokens(); 216 | } 217 | 218 | internal void SetExpired(EvictionReason reason) 219 | { 220 | _IsExpired = true; 221 | if (_EvictionReason == EvictionReason.None) 222 | { 223 | _EvictionReason = reason; 224 | } 225 | DetachTokens(); 226 | } 227 | 228 | private bool CheckForExpiredTime(DateTimeOffset now) 229 | { 230 | if (_AbsoluteExpiration.HasValue && _AbsoluteExpiration.Value <= now) 231 | { 232 | SetExpired(EvictionReason.Expired); 233 | return true; 234 | } 235 | 236 | if (_SlidingExpiration.HasValue && (now - LastAccessed) >= _SlidingExpiration) 237 | { 238 | SetExpired(EvictionReason.Expired); 239 | return true; 240 | } 241 | 242 | return false; 243 | } 244 | 245 | internal bool CheckForExpiredTokens() 246 | { 247 | if (_ExpirationTokens != null) 248 | { 249 | for (int i = 0; i < _ExpirationTokens.Count; i++) 250 | { 251 | var expiredToken = _ExpirationTokens[i]; 252 | if (expiredToken.HasChanged) 253 | { 254 | SetExpired(EvictionReason.TokenExpired); 255 | return true; 256 | } 257 | } 258 | } 259 | return false; 260 | } 261 | 262 | internal void AttachTokens() 263 | { 264 | if (_ExpirationTokens != null) 265 | { 266 | lock (_Lock) 267 | { 268 | for (int i = 0; i < _ExpirationTokens.Count; i++) 269 | { 270 | var expirationToken = _ExpirationTokens[i]; 271 | if (expirationToken.ActiveChangeCallbacks) 272 | { 273 | if (_ExpirationTokenRegistrations == null) 274 | { 275 | _ExpirationTokenRegistrations = new List(1); 276 | } 277 | var registration = expirationToken.RegisterChangeCallback(_ExpirationCallback, this); 278 | _ExpirationTokenRegistrations.Add(registration); 279 | } 280 | } 281 | } 282 | } 283 | } 284 | 285 | private static void ExpirationTokensExpired(object obj) 286 | { 287 | // start a new thread to avoid issues with callbacks called from RegisterChangeCallback 288 | Task.Factory.StartNew(state => 289 | { 290 | var entry = (PersistentCacheEntry)state; 291 | entry.SetExpired(EvictionReason.TokenExpired); 292 | entry._NotifyCacheOfExpiration(entry); 293 | }, obj, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); 294 | } 295 | 296 | private void DetachTokens() 297 | { 298 | lock (_Lock) 299 | { 300 | var registrations = _ExpirationTokenRegistrations; 301 | if (registrations != null) 302 | { 303 | _ExpirationTokenRegistrations = null; 304 | for (int i = 0; i < registrations.Count; i++) 305 | { 306 | var registration = registrations[i]; 307 | registration.Dispose(); 308 | } 309 | } 310 | } 311 | } 312 | 313 | internal void InvokeEvictionCallbacks() 314 | { 315 | if (_PostEvictionCallbacks != null) 316 | { 317 | Task.Factory.StartNew(state => InvokeCallbacks((PersistentCacheEntry)state), this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); 318 | } 319 | } 320 | 321 | private static void InvokeCallbacks(PersistentCacheEntry entry) 322 | { 323 | var callbackRegistrations = Interlocked.Exchange(ref entry._PostEvictionCallbacks, null); 324 | 325 | if (callbackRegistrations == null) 326 | { 327 | return; 328 | } 329 | 330 | for (int i = 0; i < callbackRegistrations.Count; i++) 331 | { 332 | var registration = callbackRegistrations[i]; 333 | 334 | try 335 | { 336 | registration.EvictionCallback?.Invoke(entry.Key, entry.Value, entry._EvictionReason, registration.State); 337 | } 338 | catch (Exception) 339 | { 340 | return; 341 | // This will be invoked on a background thread, don't let it throw. 342 | // TODO: LOG 343 | } 344 | } 345 | } 346 | } 347 | } 348 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/PersistentMemoryCache.cs: -------------------------------------------------------------------------------- 1 | //Based on https://github.com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.Extensions.Internal; 9 | using Microsoft.Extensions.Caching.Memory; 10 | using PersistentMemoryCache.Internal; 11 | 12 | namespace PersistentMemoryCache 13 | { 14 | /// 15 | /// An implementation of using a dictionary and IPersistentStore to 16 | /// store its entries. 17 | /// 18 | public class PersistentMemoryCache : IMemoryCache 19 | { 20 | private Dictionary _InMemoryEntries; 21 | private readonly ReaderWriterLockSlim _EntryLock; 22 | private bool _Disposed; 23 | 24 | // We store the delegates locally to prevent allocations 25 | // every time a new CacheEntry is created. 26 | private readonly Action _SetEntry; 27 | private readonly Action _EntryExpirationNotification; 28 | 29 | private DateTimeOffset _LastExpirationScan; 30 | private bool _IsReloadingFromStore; 31 | private PersistentMemoryCacheOptions _Options; 32 | 33 | /// 34 | /// Creates a new instance. 35 | /// 36 | /// The options of the cache. 37 | public PersistentMemoryCache(PersistentMemoryCacheOptions options) 38 | { 39 | if (options == null) 40 | { 41 | throw new ArgumentNullException(nameof(options)); 42 | } 43 | _Options = options; 44 | 45 | _InMemoryEntries = new Dictionary(); 46 | _EntryLock = new ReaderWriterLockSlim(); 47 | _SetEntry = SetEntry; 48 | _EntryExpirationNotification = EntryExpired; 49 | 50 | _LastExpirationScan = _Options.Clock.UtcNow; 51 | if (_Options.CompactOnMemoryPressure) 52 | { 53 | GcNotification.Register(DoMemoryPreassureCollection, state: null); 54 | } 55 | if (_Options.IsPersistent) 56 | { 57 | ReloadDataFromStore(); 58 | } 59 | } 60 | 61 | private void ReloadDataFromStore() 62 | { 63 | try 64 | { 65 | _IsReloadingFromStore = true; 66 | List persistentCacheEntries = _Options.PersistentStore.LoadEntries(_Options.CacheName); 67 | foreach (Internal.LiteDbCacheEntry persistentCacheEntry in persistentCacheEntries) 68 | { 69 | using (PersistentCacheEntry cacheEntry = (PersistentCacheEntry)CreateEntry(persistentCacheEntry.Key)) 70 | { 71 | cacheEntry.PersistentStoreId = persistentCacheEntry.Id; 72 | cacheEntry.Priority = persistentCacheEntry.Priority; 73 | cacheEntry.Value = persistentCacheEntry.GetValue(); 74 | cacheEntry.LastAccessed = persistentCacheEntry.LastAccessed; 75 | cacheEntry.AbsoluteExpiration = persistentCacheEntry.AbsoluteExpiration; 76 | cacheEntry.SlidingExpiration = persistentCacheEntry.SlidingExpiration; 77 | } 78 | } 79 | } 80 | finally 81 | { 82 | _IsReloadingFromStore = false; 83 | } 84 | } 85 | 86 | /// 87 | /// Cleans up the background collection events. 88 | /// 89 | ~PersistentMemoryCache() 90 | { 91 | Dispose(false); 92 | } 93 | 94 | /// 95 | /// Gets the count of the current entries for diagnostic purposes. 96 | /// 97 | public int Count 98 | { 99 | get 100 | { 101 | return _InMemoryEntries.Count; 102 | } 103 | } 104 | 105 | public ICacheEntry CreateEntry(object key) 106 | { 107 | CheckDisposed(); 108 | return new PersistentCacheEntry(key: key, notifyCacheEntryDisposed: _SetEntry, notifyCacheOfExpiration: _EntryExpirationNotification); 109 | } 110 | 111 | private void SetEntry(PersistentCacheEntry entry) 112 | { 113 | var utcNow = _Options.Clock.UtcNow; 114 | 115 | DateTimeOffset? absoluteExpiration = null; 116 | if (entry._AbsoluteExpirationRelativeToNow.HasValue) 117 | { 118 | absoluteExpiration = utcNow + entry._AbsoluteExpirationRelativeToNow; 119 | } 120 | else if (entry._AbsoluteExpiration.HasValue) 121 | { 122 | absoluteExpiration = entry._AbsoluteExpiration; 123 | } 124 | 125 | // Applying the option's absolute expiration only if it's not already smaller. 126 | // This can be the case if a dependent cache entry has a smaller value, and 127 | // it was set by cascading it to its parent. 128 | if (absoluteExpiration.HasValue) 129 | { 130 | if (!entry._AbsoluteExpiration.HasValue || absoluteExpiration.Value < entry._AbsoluteExpiration.Value) 131 | { 132 | entry._AbsoluteExpiration = absoluteExpiration; 133 | } 134 | } 135 | 136 | // Initialize the last access timestamp at the time the entry is added 137 | entry.LastAccessed = utcNow; 138 | 139 | var added = false; 140 | PersistentCacheEntry priorEntry; 141 | 142 | _EntryLock.EnterWriteLock(); 143 | try 144 | { 145 | 146 | if (_InMemoryEntries.TryGetValue(entry.Key, out priorEntry)) 147 | { 148 | RemoveEntryFromMemoryAndStore(priorEntry); 149 | priorEntry.SetExpired(EvictionReason.Replaced); 150 | } 151 | 152 | if (!entry.CheckExpired(utcNow)) 153 | { 154 | AddEntryToMemoryAndStore(entry); 155 | entry.AttachTokens(); 156 | if (_Options.IsPersistent) 157 | { 158 | entry.PropertyChanged += Entry_PropertyChanged; 159 | } 160 | added = true; 161 | } 162 | } 163 | finally 164 | { 165 | _EntryLock.ExitWriteLock(); 166 | } 167 | 168 | if (priorEntry != null) 169 | { 170 | priorEntry.InvokeEvictionCallbacks(); 171 | } 172 | 173 | if (!added) 174 | { 175 | entry.InvokeEvictionCallbacks(); 176 | } 177 | StartScanForExpiredItems(); 178 | } 179 | 180 | private void Entry_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 181 | { 182 | var entry = (PersistentCacheEntry)sender; 183 | var liteDbEntry = CreateLiteDbEntryFromPersistentEntry(entry); 184 | _Options.PersistentStore.UpdateEntry(entry.PersistentStoreId.Value, liteDbEntry); 185 | } 186 | 187 | private void AddEntryToMemoryAndStore(PersistentCacheEntry entry) 188 | { 189 | _InMemoryEntries[entry.Key] = entry; 190 | if (_Options.IsPersistent && !_IsReloadingFromStore) 191 | { 192 | LiteDbCacheEntry liteDbEntry = CreateLiteDbEntryFromPersistentEntry(entry); 193 | entry.PersistentStoreId = _Options.PersistentStore.AddEntry(liteDbEntry); 194 | } 195 | } 196 | 197 | private LiteDbCacheEntry CreateLiteDbEntryFromPersistentEntry(PersistentCacheEntry entry) 198 | { 199 | Type cacheValueType = entry.Value?.GetType() ?? typeof(object); 200 | LiteDbCacheEntry liteDbEntry = LiteDbCacheEntry.ConstructCacheEntry(cacheValueType); 201 | liteDbEntry.CacheName = _Options.CacheName; 202 | liteDbEntry.Priority = entry.Priority; 203 | liteDbEntry.Key = entry.Key; 204 | liteDbEntry.LastAccessed = entry.LastAccessed; 205 | liteDbEntry.AbsoluteExpiration = entry.AbsoluteExpiration; 206 | liteDbEntry.SlidingExpiration = entry.SlidingExpiration; 207 | liteDbEntry.SetValue(entry.Value); 208 | return liteDbEntry; 209 | } 210 | 211 | public bool TryGetValue(object key, out object result) 212 | { 213 | if (key == null) 214 | { 215 | throw new ArgumentNullException(nameof(key)); 216 | } 217 | 218 | var utcNow = _Options.Clock.UtcNow; 219 | result = null; 220 | bool found = false; 221 | PersistentCacheEntry expiredEntry = null; 222 | CheckDisposed(); 223 | _EntryLock.EnterReadLock(); 224 | try 225 | { 226 | PersistentCacheEntry entry; 227 | if (_InMemoryEntries.TryGetValue(key, out entry)) 228 | { 229 | // Check if expired due to expiration tokens, timers, etc. and if so, remove it. 230 | if (entry.CheckExpired(utcNow)) 231 | { 232 | expiredEntry = entry; 233 | } 234 | else 235 | { 236 | found = true; 237 | entry.LastAccessed = utcNow; 238 | result = entry.Value; 239 | } 240 | } 241 | } 242 | finally 243 | { 244 | _EntryLock.ExitReadLock(); 245 | } 246 | 247 | if (expiredEntry != null) 248 | { 249 | // TODO: For efficiency queue this up for batch removal 250 | RemoveEntry(expiredEntry); 251 | } 252 | 253 | StartScanForExpiredItems(); 254 | 255 | return found; 256 | } 257 | 258 | public void Remove(object key) 259 | { 260 | if (key == null) 261 | { 262 | throw new ArgumentNullException(nameof(key)); 263 | } 264 | 265 | CheckDisposed(); 266 | PersistentCacheEntry entry; 267 | _EntryLock.EnterReadLock(); 268 | try 269 | { 270 | if (_InMemoryEntries.TryGetValue(key, out entry)) 271 | { 272 | entry.SetExpired(EvictionReason.Removed); 273 | } 274 | } 275 | finally 276 | { 277 | _EntryLock.ExitReadLock(); 278 | } 279 | 280 | if (entry != null) 281 | { 282 | // TODO: For efficiency consider processing these removals in batches. 283 | RemoveEntry(entry); 284 | } 285 | 286 | StartScanForExpiredItems(); 287 | } 288 | 289 | private void RemoveEntry(PersistentCacheEntry entry) 290 | { 291 | _EntryLock.EnterWriteLock(); 292 | try 293 | { 294 | // Only remove it if someone hasn't modified it since our lookup 295 | PersistentCacheEntry currentEntry; 296 | if (_InMemoryEntries.TryGetValue(entry.Key, out currentEntry) 297 | && object.ReferenceEquals(currentEntry, entry)) 298 | { 299 | RemoveEntryFromMemoryAndStore(entry); 300 | if (_Options.IsPersistent) 301 | { 302 | entry.PropertyChanged -= Entry_PropertyChanged; 303 | } 304 | } 305 | } 306 | finally 307 | { 308 | _EntryLock.ExitWriteLock(); 309 | } 310 | entry.InvokeEvictionCallbacks(); 311 | } 312 | 313 | private void RemoveEntryFromMemoryAndStore(PersistentCacheEntry entry) 314 | { 315 | _InMemoryEntries.Remove(entry.Key); 316 | if (_Options.IsPersistent) 317 | { 318 | _Options.PersistentStore.RemoveEntry(entry.PersistentStoreId.Value); 319 | } 320 | } 321 | 322 | private void RemoveEntries(List entries) 323 | { 324 | _EntryLock.EnterWriteLock(); 325 | try 326 | { 327 | foreach (var entry in entries) 328 | { 329 | // Only remove it if someone hasn't modified it since our lookup 330 | PersistentCacheEntry currentEntry; 331 | if (_InMemoryEntries.TryGetValue(entry.Key, out currentEntry) && object.ReferenceEquals(currentEntry, entry)) 332 | { 333 | RemoveEntryFromMemoryAndStore(entry); 334 | } 335 | } 336 | } 337 | finally 338 | { 339 | _EntryLock.ExitWriteLock(); 340 | } 341 | 342 | foreach (var entry in entries) 343 | { 344 | entry.InvokeEvictionCallbacks(); 345 | } 346 | } 347 | 348 | internal void Clear() 349 | { 350 | RemoveEntries(_InMemoryEntries.Values.ToList()); 351 | } 352 | 353 | private void EntryExpired(PersistentCacheEntry entry) 354 | { 355 | // TODO: For efficiency consider processing these expirations in batches. 356 | RemoveEntry(entry); 357 | StartScanForExpiredItems(); 358 | } 359 | 360 | // Called by multiple actions to see how long it's been since we last checked for expired items. 361 | // If sufficient time has elapsed then a scan is initiated on a background task. 362 | private void StartScanForExpiredItems() 363 | { 364 | var now = _Options.Clock.UtcNow; 365 | if (_Options.ExpirationScanFrequency < now - _LastExpirationScan) 366 | { 367 | _LastExpirationScan = now; 368 | Task.Factory.StartNew(state => ScanForExpiredItems((PersistentMemoryCache)state), this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); 369 | } 370 | } 371 | 372 | private static void ScanForExpiredItems(PersistentMemoryCache cache) 373 | { 374 | List expiredEntries = new List(); 375 | 376 | cache._EntryLock.EnterReadLock(); 377 | try 378 | { 379 | var now = cache._Options.Clock.UtcNow; 380 | foreach (var entry in cache._InMemoryEntries.Values) 381 | { 382 | if (entry.CheckExpired(now)) 383 | { 384 | expiredEntries.Add(entry); 385 | } 386 | } 387 | } 388 | finally 389 | { 390 | cache._EntryLock.ExitReadLock(); 391 | } 392 | 393 | cache.RemoveEntries(expiredEntries); 394 | } 395 | 396 | /// This is called after a Gen2 garbage collection. We assume this means there was memory pressure. 397 | /// Remove at least 10% of the total entries (or estimated memory?). 398 | private bool DoMemoryPreassureCollection(object state) 399 | { 400 | if (_Disposed) 401 | { 402 | return false; 403 | } 404 | 405 | Compact(0.10); 406 | 407 | return true; 408 | } 409 | 410 | /// Remove at least the given percentage (0.10 for 10%) of the total entries (or estimated memory?), according to the following policy: 411 | /// 1. Remove all expired items. 412 | /// 2. Bucket by CacheItemPriority. 413 | /// ?. Least recently used objects. 414 | /// ?. Items with the soonest absolute expiration. 415 | /// ?. Items with the soonest sliding expiration. 416 | /// ?. Larger objects - estimated by object graph size, inaccurate. 417 | public void Compact(double percentage) 418 | { 419 | List expiredEntries = new List(); 420 | List lowPriEntries = new List(); 421 | List normalPriEntries = new List(); 422 | List highPriEntries = new List(); 423 | List neverRemovePriEntries = new List(); 424 | 425 | _EntryLock.EnterReadLock(); 426 | try 427 | { 428 | // Sort items by expired & priority status 429 | var now = _Options.Clock.UtcNow; 430 | foreach (var entry in _InMemoryEntries.Values) 431 | { 432 | if (entry.CheckExpired(now)) 433 | { 434 | expiredEntries.Add(entry); 435 | } 436 | else 437 | { 438 | switch (entry.Priority) 439 | { 440 | case CacheItemPriority.Low: 441 | lowPriEntries.Add(entry); 442 | break; 443 | case CacheItemPriority.Normal: 444 | normalPriEntries.Add(entry); 445 | break; 446 | case CacheItemPriority.High: 447 | highPriEntries.Add(entry); 448 | break; 449 | case CacheItemPriority.NeverRemove: 450 | neverRemovePriEntries.Add(entry); 451 | break; 452 | default: 453 | System.Diagnostics.Debug.Assert(false, "Not implemented: " + entry.Priority); 454 | break; 455 | } 456 | } 457 | } 458 | 459 | int totalEntries = expiredEntries.Count + lowPriEntries.Count + normalPriEntries.Count + highPriEntries.Count + neverRemovePriEntries.Count; 460 | int removalCountTarget = (int)(totalEntries * percentage); 461 | 462 | ExpirePriorityBucket(removalCountTarget, expiredEntries, lowPriEntries); 463 | ExpirePriorityBucket(removalCountTarget, expiredEntries, normalPriEntries); 464 | ExpirePriorityBucket(removalCountTarget, expiredEntries, highPriEntries); 465 | } 466 | finally 467 | { 468 | _EntryLock.ExitReadLock(); 469 | } 470 | 471 | RemoveEntries(expiredEntries); 472 | } 473 | 474 | /// Policy: 475 | /// ?. Least recently used objects. 476 | /// ?. Items with the soonest absolute expiration. 477 | /// ?. Items with the soonest sliding expiration. 478 | /// ?. Larger objects - estimated by object graph size, inaccurate. 479 | private void ExpirePriorityBucket(int removalCountTarget, List expiredEntries, List priorityEntries) 480 | { 481 | // Do we meet our quota by just removing expired entries? 482 | if (removalCountTarget <= expiredEntries.Count) 483 | { 484 | // No-op, we've met quota 485 | return; 486 | } 487 | if (expiredEntries.Count + priorityEntries.Count <= removalCountTarget) 488 | { 489 | // Expire all of the entries in this bucket 490 | foreach (var entry in priorityEntries) 491 | { 492 | entry.SetExpired(EvictionReason.Capacity); 493 | } 494 | expiredEntries.AddRange(priorityEntries); 495 | return; 496 | } 497 | 498 | // Expire enough entries to reach our goal 499 | // TODO: Refine policy 500 | 501 | // LRU 502 | foreach (var entry in priorityEntries.OrderBy(entry => entry.LastAccessed)) 503 | { 504 | entry.SetExpired(EvictionReason.Capacity); 505 | expiredEntries.Add(entry); 506 | if (removalCountTarget <= expiredEntries.Count) 507 | { 508 | break; 509 | } 510 | } 511 | } 512 | 513 | public void Dispose() 514 | { 515 | Dispose(true); 516 | } 517 | 518 | protected virtual void Dispose(bool disposing) 519 | { 520 | if (!_Disposed) 521 | { 522 | if (disposing) 523 | { 524 | GC.SuppressFinalize(this); 525 | } 526 | 527 | _Disposed = true; 528 | } 529 | } 530 | 531 | private void CheckDisposed() 532 | { 533 | if (_Disposed) 534 | { 535 | throw new ObjectDisposedException(typeof(PersistentMemoryCache).FullName); 536 | } 537 | } 538 | } 539 | } -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/PersistentMemoryCache.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | netstandard2.0 4 | 5 | Joel Weiss 6 | PersistentMemoryCache 7 | Persistent Memory Cache, caches in Memory but also to Disk, so cache is persistent across restarts, build on Top of "Microsoft.Extensions.Caching.Abstractions" and "LiteDB" 8 | PersistentMemoryCache 9 | PersistentMemoryCache 10 | PersistentMemoryCache 11 | https://github.com/joelweiss/PersistentMemoryCache 12 | https://github.com/joelweiss/PersistentMemoryCache 13 | https://github.com/joelweiss/PersistentMemoryCache/blob/master/Licence.txt 14 | Memory Cache LiteDB 15 | 1.0.0-Beta 16 | false 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Source/PersistentMemoryCache/PersistentMemoryCacheOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.Internal; 3 | 4 | namespace PersistentMemoryCache 5 | { 6 | public class PersistentMemoryCacheOptions 7 | { 8 | public PersistentMemoryCacheOptions(string cacheName, IPersistentStore persistentStore) 9 | { 10 | CacheName = cacheName; 11 | PersistentStore = persistentStore; 12 | } 13 | 14 | public string CacheName { get; } = "Default"; 15 | public IPersistentStore PersistentStore { get; } 16 | public ISystemClock Clock { get; set; } = new SystemClock(); 17 | public bool CompactOnMemoryPressure { get; set; } = true; 18 | public TimeSpan ExpirationScanFrequency { get; set; } = TimeSpan.FromMinutes(1); 19 | public bool IsPersistent { get; set; } = true; 20 | } 21 | } -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.{build}-Beta 2 | configuration: Release 3 | pull_requests: 4 | do_not_increment_build_number: true 5 | image: Previous Visual Studio 2017 6 | dotnet_csproj: 7 | patch: true 8 | file: '**\PersistentMemoryCache.csproj' 9 | version: '{version}' 10 | package_version: '{version}' 11 | before_build: 12 | - ps: dotnet restore Source\PersistentMemoryCache.sln 13 | build: 14 | project: Source\PersistentMemoryCache.sln 15 | publish_nuget: true 16 | verbosity: minimal 17 | test_script: 18 | - ps: >- 19 | dotnet test Source\PersistentMemoryCache.Tests\PersistentMemoryCache.Tests.csproj --------------------------------------------------------------------------------