using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.FSE.BL
{
///
/// Represents an in-memory cache dictionary of type T.
///
///
///
public class MemoryCacheDictionary : IMemoryCacheDictionary where TValue : class
{
private ConcurrentDictionary _dictionary;
///
/// Gets the cache name.
///
public String Name { get; private set; }
///
/// Gets the count of items.
///
public int Count
{
get { return _dictionary.Count; }
}
///
/// Initializes a new instance of the class.
///
/// The name of the cache.
public MemoryCacheDictionary(String name)
{
Name = name;
_dictionary = new ConcurrentDictionary();
}
///
/// Determines whether this cache contains an element with the specified key.
///
/// The key.
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
///
/// Gets a cached item by the specified key.
///
/// The key.
/// if set to true throws exception when item not found otherwise returns null.
///
public TValue Get(TKey key, bool throwIfNotFound = true)
{
TValue value = null;
if (_dictionary.TryGetValue(key, out value))
{
return value;
}
else
{
if (throwIfNotFound)
{
throw new KeyNotFoundException($"Could not locate key '{key}' on cache '{Name}'.");
}
else
{
return null;
}
}
}
///
/// Puts a new cached item or overrides an existing one.
///
/// The key.
/// The value.
public void Put(TKey key, TValue value)
{
_dictionary[key] = value;
}
///
/// Get all cached items as a list.
///
///
public List ToList(bool throwOnEmpty = false)
{
var list = _dictionary.Select(x => x.Value).ToList();
if (throwOnEmpty && list.Count == 0)
{
throw new KeyNotFoundException("The memory cache does not contain any items.");
}
return list;
}
///
/// Clears this all items from cache.
///
public void Clear()
{
_dictionary.Clear();
}
}
///
/// Represents an in-memory cache dictionary of type T.
///
///
///
public class MemoryCacheDoubleKeyDictionary : IMemoryCacheDictionary where TValue : class
{
private ConcurrentDictionary _dictionary;
///
/// Gets the cache name.
///
public String Name { get; private set; }
///
/// Gets the count of items.
///
public int Count
{
get { return _dictionary.Count; }
}
///
/// Initializes a new instance of the class.
///
/// The name of the cache.
public MemoryCacheDoubleKeyDictionary(String name)
{
Name = name;
_dictionary = new ConcurrentDictionary();
}
///
/// Determines whether this cache contains an element with the specified keys.
///
/// The first key.
/// The second key.
public bool ContainsKey(TKey1 key1, TKey2 key2)
{
return _dictionary.ContainsKey(key1.ToString() + key2.ToString());
}
///
/// Gets a cached item by the specified keys.
///
/// The first key.
/// The second key.
/// if set to true throws exception when item not found otherwise returns null.
///
public TValue Get(TKey1 key1, TKey2 key2, bool throwIfNotFound = true)
{
TValue value = null;
if (_dictionary.TryGetValue(key1.ToString() + key2.ToString(), out value))
{
return value;
}
else
{
if (throwIfNotFound)
{
throw new KeyNotFoundException($"Could not locate key '{key1 + " " + key2}' on cache '{Name}'.");
}
else
{
return null;
}
}
}
///
/// Puts a new cached item or overrides an existing one.
///
/// The first key.
/// The second key.
/// The value.
public void Put(TKey1 key1, TKey2 key2, TValue value)
{
_dictionary[key1.ToString() + key2.ToString()] = value;
}
///
/// Get all cached items as a list.
///
///
public List ToList(bool throwOnEmpty = false)
{
var list = _dictionary.Select(x => x.Value).ToList();
if (throwOnEmpty && list.Count == 0)
{
throw new KeyNotFoundException("The memory cache does not contain any items.");
}
return list;
}
///
/// Clears this all items from cache.
///
public void Clear()
{
_dictionary.Clear();
}
}
}