Skip to content

Commit

Permalink
Fixed HashedFileCacheTest.FlushTest
Browse files Browse the repository at this point in the history
Extracted FileCacheManager's code for deserializing a policy into a new
function.

Made FileCacheManager and HashedFileCacheManager use that function.
  • Loading branch information
cole-brown committed Sep 2, 2021
1 parent 51f2259 commit 59e1222
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
54 changes: 32 additions & 22 deletions src/FileCache/FileCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ protected virtual object DeserializePayloadData(string fileName, SerializationBi
return data;
}

protected SerializableCacheItemPolicy DeserializePolicyData(string policyPath)
{
SerializableCacheItemPolicy policy = null;
try
{
if (File.Exists(policyPath))
{
using (FileStream stream = GetStream(policyPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (BinaryReader reader = new BinaryReader(stream))
{
// TODO: In part of the merge it looked like the policy was force serialized with LocalCacheBinder(), is this intended?
policy = SerializableCacheItemPolicy.Deserialize(reader, stream.Length);
}
}
}
else
{
policy = new SerializableCacheItemPolicy();
}
}
catch
{
policy = new SerializableCacheItemPolicy();
}

return policy;
}

/// <summary>
/// This function serves to centralize file reads within this class.
/// </summary>
Expand Down Expand Up @@ -125,28 +154,9 @@ public virtual FileCachePayload ReadFile(FileCache.PayloadMode mode, string key,
default:
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
}
try
{
if (File.Exists(policyPath))
{
using (FileStream stream = GetStream(policyPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (BinaryReader reader = new BinaryReader(stream))
{
// TODO: In part of the merge it looked like the policy was force serialized with LocalCacheBinder(), is this intended?
payload.Policy = SerializableCacheItemPolicy.Deserialize(reader, stream.Length);
}
}
}
else
{
payload.Policy = new SerializableCacheItemPolicy();
}
}
catch
{
payload.Policy = new SerializableCacheItemPolicy();
}

payload.Policy = DeserializePolicyData(policyPath);

return payload;
}

Expand Down
5 changes: 3 additions & 2 deletions src/FileCache/HashedFileCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace System.Runtime.Caching
public class HashedFileCacheManager : FileCacheManager
{
private static IxxHash _hasher = xxHashFactory.Instance.Create();

/// <summary>
/// Returns a 64bit hash in hex of supplied key
/// </summary>
Expand Down Expand Up @@ -54,7 +55,7 @@ private string GetFileName(string key, string regionName = null)
//check for correct key
try
{
SerializableCacheItemPolicy policy = Deserialize(fileName) as SerializableCacheItemPolicy;
SerializableCacheItemPolicy policy = DeserializePolicyData(fileName);
if (key.CompareTo(policy.Key) == 0)
{
//correct key found!
Expand Down Expand Up @@ -124,7 +125,7 @@ public override IEnumerable<string> GetKeys(string regionName = null)
{
try
{
SerializableCacheItemPolicy policy = Deserialize(file) as SerializableCacheItemPolicy;
SerializableCacheItemPolicy policy = DeserializePolicyData(file);
keys.Add(policy.Key);
}
catch
Expand Down

0 comments on commit 59e1222

Please sign in to comment.