Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Mar 22, 2022
2 parents 3d5956e + 757487a commit 9438c3b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Monkey Cache is a .NET Standard 2.0 library, but has some platform specific twea
|Windows 10 UWP|10.0.16299+|
|.NET Core|2.0+|
|ASP.NET Core|2.0+|
|.NET|4.6.1+|
|.NET Framework|4.6.1+|

## Setup

Expand Down Expand Up @@ -173,7 +173,7 @@ Cache will always be stored in the default platform specific location:
|Windows 10 UWP|Windows.Storage.ApplicationData.Current.LocalFolder.Path|
|.NET Core|Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)|
|ASP.NET Core|Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)|
|.NET|Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)|
|.NET Framework|Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)|


#### Persisting Data Longer
Expand Down
26 changes: 20 additions & 6 deletions src/MonkeyCache.FileStore/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public class Barrel : IBarrel
ReaderWriterLockSlim indexLocker;
readonly JsonSerializerSettings jsonSettings;
Lazy<string> baseDirectory;
HashAlgorithm hashAlgorithm;

Barrel(string cacheDirectory = null)
/// <summary>
/// FileStore Barrel constructor
/// </summary>
/// <param name="cacheDirectory">Optionally specify directory where cache will live</param>
/// <param name="hash">Optionally specify hash algorithm</param>
Barrel(string cacheDirectory = null, HashAlgorithm hash = null)
{
baseDirectory = new Lazy<string>(() =>
{
Expand All @@ -24,6 +30,10 @@ public class Barrel : IBarrel
: cacheDirectory;
});

hashAlgorithm = hash;
if (hashAlgorithm == null)
hashAlgorithm = MD5.Create();

indexLocker = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

jsonSettings = new JsonSerializerSettings
Expand All @@ -50,8 +60,13 @@ public class Barrel : IBarrel
/// </summary>
public static IBarrel Current => (instance ?? (instance = new Barrel()));

public static IBarrel Create(string cacheDirectory) =>
new Barrel(cacheDirectory);
/// <summary>
/// FileStore Barrel
/// </summary>
/// <param name="cacheDirectory">Optionally specify directory where cache will live</param>
/// <param name="hash">Optionally specify hash algorithm</param>
public static IBarrel Create(string cacheDirectory, HashAlgorithm hash = null) =>
new Barrel(cacheDirectory, hash);

/// <summary>
/// Adds an entry to the barrel
Expand Down Expand Up @@ -456,10 +471,9 @@ void LoadIndex()
}
}

static string Hash(string input)
string Hash(string input)
{
var md5Hasher = MD5.Create();
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
var data = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input));
return BitConverter.ToString(data);
}

Expand Down
7 changes: 6 additions & 1 deletion src/MonkeyCache.LiteDB/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Barrel : IBarrel
{
public static string ApplicationId { get; set; } = string.Empty;
public static string EncryptionKey { get; set; } = string.Empty;
public static bool Upgrade { get; set; } = false;

static readonly Lazy<string> baseCacheDir = new Lazy<string>(() =>
{
Expand Down Expand Up @@ -63,7 +64,11 @@ public static IBarrel Create(string cacheDirectory, bool cache = false)

if (!string.IsNullOrWhiteSpace(EncryptionKey))
path = $"Filename={path}; Password={EncryptionKey}";
else
path = $"Filename={path}";
#endif
if(Upgrade)
path += "; Upgrade=true";

db = new LiteDatabase(path);
col = db.GetCollection<Banana>();
Expand Down Expand Up @@ -301,4 +306,4 @@ public void Empty(params string[] key)
}
#endregion
}
}
}
1 change: 1 addition & 0 deletions src/MonkeyCache.LiteDB/MonkeyCache.LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
</PropertyGroup>



<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.11" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/MonkeyCache.TestApp.UWP/MonkeyCache.TestApp.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/MonkeyCache.TestApp/MonkeyCache.TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
<DependentUpon>*.xaml</DependentUpon>
</Compile>
</ItemGroup>
</Project>
</Project>

0 comments on commit 9438c3b

Please sign in to comment.