├── .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 | [](https://ci.appveyor.com/project/joelweiss/persistentmemorycache)
2 | [](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