using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Media; namespace ChatController.Core { public class OwnChatCache { // Key-> "group-127" oder "user-938" private Dictionary _ImageSources = new Dictionary(); // Key-> "group-127" oder "user-938" private Dictionary _Bitmaps = new Dictionary(); private static OwnChatCache _Instance; private OwnChatCache() { } public static OwnChatCache GetInstance() { return _Instance ?? (_Instance = new OwnChatCache()); } public void ClearAll() { ClearImageSources(); ClearBitmaps(); } public void ClearImageSources() { _ImageSources.Clear(); } public void ClearBitmaps() { _Bitmaps.Clear(); } public ImageSource GetImageSourceFromCache(string key) { if(key == null || _ImageSources == null || !_ImageSources.ContainsKey(key)) { return null; } var imageSourceCacheObject = _ImageSources[key]; var expirationDate = imageSourceCacheObject.ExpirationDate; return expirationDate < DateTime.Now ? null : imageSourceCacheObject.Image; } public void AddImageToCache(string key, ImageSource imageSource) { if(_ImageSources == null) { _ImageSources = new Dictionary(); } var newImageSourceCacheObject = new ImageSourceCacheObject(imageSource); if(_ImageSources.ContainsKey(key)) { _ImageSources[key] = newImageSourceCacheObject; } else { _ImageSources.Add(key, newImageSourceCacheObject); } } public Bitmap GetBitmapFromCache(string key) { if(key == null || _Bitmaps == null || !_Bitmaps.ContainsKey(key)) { return null; } var bitmapCacheObject = _Bitmaps[key]; var expirationDate = bitmapCacheObject.ExpirationDate; return expirationDate < DateTime.Now ? null : bitmapCacheObject.Bitmap; } public void AddBitmapToCache(string key, Bitmap bitmap) { if(_Bitmaps == null) { _Bitmaps = new Dictionary(); } var newBitmapCacheObject = new BitmapCacheObject(bitmap); if(_Bitmaps.ContainsKey(key)) { _Bitmaps[key] = newBitmapCacheObject; } else { _Bitmaps.Add(key, newBitmapCacheObject); } } } public class ImageSourceCacheObject { public DateTime ExpirationDate { get; } public ImageSource Image { get; } public ImageSourceCacheObject(ImageSource imageSource) { ExpirationDate = DateTime.Now.AddYears(1); Image = imageSource; } } public class BitmapCacheObject { public DateTime ExpirationDate { get; } public Bitmap Bitmap { get; } public BitmapCacheObject(Bitmap bitmap) { ExpirationDate = DateTime.Now.AddYears(1); Bitmap = bitmap; } } }