diff --git a/src/FileCache/FileCache.cs b/src/FileCache/FileCache.cs index fbe9bda..34ad22e 100644 --- a/src/FileCache/FileCache.cs +++ b/src/FileCache/FileCache.cs @@ -608,12 +608,14 @@ private long CacheSizeHelper(DirectoryInfo root) { size += fi.Length; } + // Add subdirectory sizes. var dis = root.EnumerateDirectories(); foreach (DirectoryInfo di in dis) { size += CacheSizeHelper(di); } + return size; } @@ -743,10 +745,9 @@ private void WriteHelper(PayloadMode mode, string key, FileCachePayload data, st //check to see if limit was reached if (CurrentCacheSize > MaxCacheSize) - if (CurrentCacheSize > MaxCacheSize) - { - MaxCacheSizeReached(this, new FileCacheEventArgs(CurrentCacheSize, MaxCacheSize)); - } + { + MaxCacheSizeReached(this, new FileCacheEventArgs(CurrentCacheSize, MaxCacheSize)); + } } #endregion @@ -955,7 +956,6 @@ public override object Remove(string key, string regionName = null) { object valueToDelete = null; - if (Contains(key, regionName) == true) { diff --git a/src/FileCache/FileCacheManager.cs b/src/FileCache/FileCacheManager.cs index 5361cc4..2d8bc09 100644 --- a/src/FileCache/FileCacheManager.cs +++ b/src/FileCache/FileCacheManager.cs @@ -284,12 +284,12 @@ public virtual long WriteFile(FileCache.PayloadMode mode, string key, FileCacheP using (BinaryWriter writer = new BinaryWriter(stream)) { data.Policy.Serialize(writer); - - // adjust cache size - cacheSizeDelta += new FileInfo(cachedPolicy).Length; } } + // Adjust cache size outside of the using blocks to ensure it's after the data is written. + cacheSizeDelta += new FileInfo(cachedPolicy).Length; + return cacheSizeDelta; }