Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document and (minor) refactor on MruCache class #2458

Merged
merged 1 commit into from Dec 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/NLog/Internal/MruCache.cs
Expand Up @@ -40,38 +40,38 @@ namespace NLog.Internal
/// </summary>
internal class MruCache<TKey, TValue>
{
private readonly Dictionary<TKey, MruItem> _dictionary;
private readonly Dictionary<TKey, MruCacheItem> _dictionary;
private readonly int _maxCapacity;
private long _currentVersion;

/// <summary>
/// Constructor
/// </summary>
/// <param name="maxCapacity">Max number of items to hold before discarding</param>
/// <param name="maxCapacity">Maximum number of items the cache will hold before discarding.</param>
public MruCache(int maxCapacity)
{
_maxCapacity = maxCapacity;
_dictionary = new Dictionary<TKey, MruItem>(_maxCapacity);
_dictionary = new Dictionary<TKey, MruCacheItem>(_maxCapacity);
_currentVersion = 1;
}

/// <summary>
/// Insert item into caceh
/// Attempt to insert item into cache.
/// </summary>
/// <param name="key">Dictionary Key</param>
/// <param name="value">Dictionary Value</param>
/// <returns>Key did not exist already</returns>
/// <param name="key">Key of the item to be inserted in the cache.</param>
/// <param name="value">Value of the item to be inserted in the cache.</param>
/// <returns><c>true</c> when the key does not already exist in the cache, <c>false</c> otherwise.</returns>
public bool TryAddValue(TKey key, TValue value)
{
lock (_dictionary)
{
MruItem item;
MruCacheItem item;
if (_dictionary.TryGetValue(key, out item))
{
var version = _currentVersion;
if (item.Version != version || !EqualityComparer<TValue>.Default.Equals(value, item.Value))
{
_dictionary[key] = new MruItem(value, version, false);
_dictionary[key] = new MruCacheItem(value, version, false);
}
return false; // Already exists
}
Expand All @@ -83,7 +83,7 @@ public bool TryAddValue(TKey key, TValue value)
PruneCache();
}

_dictionary.Add(key, new MruItem(value, _currentVersion, true));
_dictionary.Add(key, new MruCacheItem(value, _currentVersion, true));
return true;
}
}
Expand Down Expand Up @@ -147,14 +147,14 @@ private void PruneCache()
}

/// <summary>
/// Lookup existing item in cache
/// Lookup existing item in cache.
/// </summary>
/// <param name="key">Dictionary Key</param>
/// <param name="value">Dictionary Value [out]</param>
/// <returns>Key found</returns>
/// <param name="key">Key of the item to be searched in the cache.</param>
/// <param name="value">Output value of the item found in the cache.</param>
/// <returns><c>True</c> when the key is found in the cache, <c>false</c> otherwise.</returns>
public bool TryGetValue(TKey key, out TValue value)
{
MruItem item;
MruCacheItem item;
try
{
if (!_dictionary.TryGetValue(key, out item))
Expand All @@ -165,7 +165,7 @@ public bool TryGetValue(TKey key, out TValue value)
}
catch
{
item = default(MruItem); // Too many people in the same room
item = default(MruCacheItem); // Too many people in the same room
}

if (item.Version != _currentVersion || item.Virgin)
Expand All @@ -182,7 +182,7 @@ public bool TryGetValue(TKey key, out TValue value)
{
version = ++_currentVersion;
}
_dictionary[key] = new MruItem(item.Value, version, false);
_dictionary[key] = new MruCacheItem(item.Value, version, false);
}
}
else
Expand All @@ -197,13 +197,13 @@ public bool TryGetValue(TKey key, out TValue value)
return true;
}

struct MruItem
struct MruCacheItem
{
public readonly TValue Value;
public readonly long Version;
public readonly bool Virgin;
public TValue Value { get; }
public long Version { get; }
public bool Virgin { get; }

public MruItem(TValue value, long version, bool virgin)
public MruCacheItem(TValue value, long version, bool virgin)
{
Value = value;
Version = version;
Expand Down