diff --git a/src/FileCache/FileCacheManager.cs b/src/FileCache/FileCacheManager.cs index 3cf6b71..b9515fd 100644 --- a/src/FileCache/FileCacheManager.cs +++ b/src/FileCache/FileCacheManager.cs @@ -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; + } + /// /// This function serves to centralize file reads within this class. /// @@ -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; } diff --git a/src/FileCache/HashedFileCacheManager.cs b/src/FileCache/HashedFileCacheManager.cs index 45abe92..b0d62ea 100644 --- a/src/FileCache/HashedFileCacheManager.cs +++ b/src/FileCache/HashedFileCacheManager.cs @@ -12,6 +12,7 @@ namespace System.Runtime.Caching public class HashedFileCacheManager : FileCacheManager { private static IxxHash _hasher = xxHashFactory.Instance.Create(); + /// /// Returns a 64bit hash in hex of supplied key /// @@ -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! @@ -124,7 +125,7 @@ public override IEnumerable GetKeys(string regionName = null) { try { - SerializableCacheItemPolicy policy = Deserialize(file) as SerializableCacheItemPolicy; + SerializableCacheItemPolicy policy = DeserializePolicyData(file); keys.Add(policy.Key); } catch