Skip to content

Commit

Permalink
-added xxHash library
Browse files Browse the repository at this point in the history
-removed .net40 target (xxHash library requires >=.net45)
-replaced unstable .net hashing to stable xxHash
  • Loading branch information
acarteas committed Sep 17, 2020
1 parent 23efe5f commit cd56246
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 19 deletions.
62 changes: 56 additions & 6 deletions src/FileCache.Signed/FileCache.Signed.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net40;net45;net48;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net45;net48;netstandard2.0;netstandard2.1</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>cache objectcache System.Runtime.Caching.ObjectCache</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand All @@ -11,8 +11,8 @@
<SignAssembly>True</SignAssembly>
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
<UpdateVersionProperties>true</UpdateVersionProperties>
<Version>0.0.0</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>3.1.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<DebugSymbols>True</DebugSymbols>
<DebugType>portable</DebugType>
Expand Down Expand Up @@ -44,9 +44,6 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
Expand All @@ -59,4 +56,57 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/FileCache.UnitTests/HashedFileCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void AccessTimeoutTest()
_cache = new FileCache();
_cache.AccessTimeout = new TimeSpan(1);
_cache["primer"] = 0;
string filePath = Path.Combine(_cache.CacheDir, "cache", "primer".GetHashCode() + "_0.dat");
string filePath = Path.Combine(_cache.CacheDir, "cache", HashedFileCacheManager.ComputeHash("primer") + "_0.dat");
FileStream stream = File.Open(filePath, FileMode.Create);
try
{
Expand Down
63 changes: 57 additions & 6 deletions src/FileCache/FileCache.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net40;net45;net48;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net45;net48;netstandard2.0;netstandard2.1</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>cache objectcache System.Runtime.Caching.ObjectCache</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Authors>Adam Carter</Authors>
<Description>FileCache is a concrete implementation of the .NET System.Runtime.Caching.ObjectCache that uses the local filesystem as the target location.</Description>
<Copyright>Copyright (c) 2012, 2013, 2017 Adam Carter (http://adam-carter.com)</Copyright>
<UpdateVersionProperties>true</UpdateVersionProperties>
<Version>0.0.0</Version>
<Version>3.1.0</Version>
<Company>Adam Carter</Company>
<RootNamespace>System.Runtime.Caching</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<DebugSymbols>True</DebugSymbols>
<DebugType>portable</DebugType>
Expand All @@ -22,6 +22,7 @@

<!-- SourceLink Config, needs nuget package in each csproj Microsoft.SourceLink.GitHub in order to work -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net40|AnyCPU'">
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
Expand All @@ -34,9 +35,6 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
Expand All @@ -49,4 +47,57 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="HashDepot">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Core">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.Interfaces">
<Version>2.0.0</Version>
</PackageReference>
<PackageReference Include="System.Data.HashFunction.xxHash">
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
22 changes: 16 additions & 6 deletions src/FileCache/HashedFileCacheManager.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

using System.Data.HashFunction;
using System.Data.HashFunction.xxHash;
namespace System.Runtime.Caching
{
/// <summary>
/// File-based caching using the built-in .NET GetHashCode(). Collisions are handled by appending
/// File-based caching using xxHash. Collisions are handled by appending
/// numerically ascending identifiers to each hash key (e.g. _1, _2, etc.).
/// </summary>
public class HashedFileCacheManager : FileCacheManager
{
private static IxxHash _hasher = xxHashFactory.Instance.Create();
/// <summary>
/// Returns a 64bit hash in hex of supplied key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ComputeHash(string key)
{
var hash = _hasher.ComputeHash(key, 64);
return hash.AsHexString();
}

/// <summary>
/// Because hash collisions prevent us from knowing the exact file name of the supplied key, we need to probe through
/// all possible fine name combinations. This function is used internally by the Delete and Get functions in this class.
Expand All @@ -32,7 +42,7 @@ private string GetFileName(string key, string regionName = null)
//the policy. It also means that deleting a policy file makes the related .dat "invisible" to FC.
string directory = Path.Combine(CacheDir, PolicySubFolder, regionName);

string hash = key.GetHashCode().ToString();
string hash = ComputeHash(key);
int hashCounter = 0;
string fileName = Path.Combine(directory, string.Format("{0}_{1}.policy", hash, hashCounter));
bool found = false;
Expand Down

0 comments on commit cd56246

Please sign in to comment.