using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
namespace Photoviewer.Common
{
public static class ImageCache
{
private static readonly Dictionary _cacheEntries;
private static readonly int _maxEntries;
static ImageCache()
{
_cacheEntries = new Dictionary();
_maxEntries = 10;
}
///
/// Gets a cached version of an image source
/// specified by the key
///
///
///
public static ImageSource GetCachedImageSource(string key)
{
ImageSource source = null;
if (_cacheEntries.ContainsKey(key))
source = _cacheEntries[key];
return source;
}
///
/// Saves an image source in the image cache
///
///
///
public static void SaveImageSourceInCache(string key, ImageSource source)
{
if (_cacheEntries.ContainsKey(key))
{
_cacheEntries[key] = source;
return;
}
if (_cacheEntries.Count + 1 > _maxEntries)
{
_cacheEntries.Remove(_cacheEntries.Keys.First());
}
_cacheEntries.Add(key, source);
}
}
}