/// <summary> /// This function serves to centralize file reads within this class. /// </summary> /// <param name="mode">the payload reading mode</param> /// <param name="key"></param> /// <param name="objectBinder"></param> /// <returns></returns> // TODO: This was protected, but since Simon Thum's additions other methods require this. We need to merge with that change to move whatever is relevant behind this interface // and restore the visibility to protected public virtual FileCachePayload ReadFile(FileCache.PayloadMode mode, string key, string regionName = null, SerializationBinder objectBinder = null) { string cachePath = GetCachePath(key, regionName); string policyPath = GetPolicyPath(key, regionName); FileCachePayload payload = new FileCachePayload(null); switch (mode) { case FileCache.PayloadMode.Filename: payload.Payload = cachePath; break; case FileCache.PayloadMode.Serializable: payload.Payload = Deserialize(cachePath); break; case FileCache.PayloadMode.RawBytes: payload.Payload = LoadRawPayloadData(cachePath); break; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } try { // TODO: In part of the merge it looked like the policy was force serialized with LocalCacheBinder(), is this intended? payload.Policy = Deserialize(policyPath) as SerializableCacheItemPolicy; } catch { payload.Policy = new SerializableCacheItemPolicy(); } return(payload); }
/// <summary> /// This function serves to centralize file writes within this class /// </summary> public virtual long WriteFile(FileCache.PayloadMode mode, string key, FileCachePayload data, string regionName = null, bool policyUpdateOnly = false) { string cachedPolicy = GetPolicyPath(key, regionName); string cachedItemPath = GetCachePath(key, regionName); long cacheSizeDelta = 0; //ensure that the cache policy contains the correct key data.Policy.Key = key; if (!policyUpdateOnly) { long oldBlobSize = 0; if (File.Exists(cachedItemPath)) { oldBlobSize = new FileInfo(cachedItemPath).Length; } switch (mode) { case FileCache.PayloadMode.Serializable: using (FileStream stream = GetStream(cachedItemPath, FileMode.Create, FileAccess.Write, FileShare.None)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, data.Payload); } break; case FileCache.PayloadMode.RawBytes: using (FileStream stream = GetStream(cachedItemPath, FileMode.Create, FileAccess.Write, FileShare.None)) { if (data.Payload is byte[]) { byte[] dataPayload = (byte[])data.Payload; stream.Write(dataPayload, 0, dataPayload.Length); } else if (data.Payload is Stream) { Stream dataPayload = (Stream)data.Payload; dataPayload.CopyTo(stream); // no close or the like, we are not the owner } } break; case FileCache.PayloadMode.Filename: File.Copy((string)data.Payload, cachedItemPath, true); break; } //adjust cache size (while we have the file to ourselves) cacheSizeDelta += new FileInfo(cachedItemPath).Length - oldBlobSize; } //remove current policy file from cache size calculations if (File.Exists(cachedPolicy)) { cacheSizeDelta -= new FileInfo(cachedPolicy).Length; } //write the cache policy using (FileStream stream = GetStream(cachedPolicy, FileMode.Create, FileAccess.Write, FileShare.None)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, data.Policy); // adjust cache size cacheSizeDelta += new FileInfo(cachedPolicy).Length; stream.Close(); } return(cacheSizeDelta); }