/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: https://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; using System.Timers; using log4net; namespace Greenshot.Base.Core { /// /// Cache class /// /// Type of key /// Type of value public class Cache { private static readonly ILog Log = LogManager.GetLogger(typeof(Cache)); private readonly IDictionary _internalCache = new Dictionary(); private readonly object _lockObject = new object(); private readonly int _secondsToExpire = 10; private readonly CacheObjectExpired _expiredCallback; public delegate void CacheObjectExpired(TK key, TV cacheValue); /// /// Initialize the cache /// public Cache() { } /// /// Initialize the cache /// /// public Cache(CacheObjectExpired expiredCallback) : this() { _expiredCallback = expiredCallback; } /// /// Initialize the cache with a expire setting /// /// public Cache(int secondsToExpire) : this() { _secondsToExpire = secondsToExpire; } /// /// Initialize the cache with a expire setting /// /// /// public Cache(int secondsToExpire, CacheObjectExpired expiredCallback) : this(expiredCallback) { _secondsToExpire = secondsToExpire; } /// /// Enumerable for the values in the cache /// public IEnumerable Elements { get { List elements = new List(); lock (_lockObject) { foreach (TV element in _internalCache.Values) { elements.Add(element); } } foreach (TV element in elements) { yield return element; } } } /// /// Get the value by key from the cache /// /// /// public TV this[TK key] { get { TV result = default; lock (_lockObject) { if (_internalCache.ContainsKey(key)) { result = _internalCache[key]; } } return result; } } /// /// Contains /// /// /// true if the cache contains the key public bool Contains(TK key) { lock (_lockObject) { return _internalCache.ContainsKey(key); } } /// /// Add a value to the cache /// /// /// public void Add(TK key, TV value) { Add(key, value, null); } /// /// Add a value to the cache /// /// /// /// optional value for the seconds to expire public void Add(TK key, TV value, int? secondsToExpire) { lock (_lockObject) { var cachedItem = new CachedItem(key, value, secondsToExpire ?? _secondsToExpire); cachedItem.Expired += delegate(TK cacheKey, TV cacheValue) { if (_internalCache.ContainsKey(cacheKey)) { Log.DebugFormat("Expiring object with Key: {0}", cacheKey); _expiredCallback?.Invoke(cacheKey, cacheValue); Remove(cacheKey); } else { Log.DebugFormat("Expired old object with Key: {0}", cacheKey); } }; if (_internalCache.ContainsKey(key)) { _internalCache[key] = value; Log.DebugFormat("Updated item with Key: {0}", key); } else { _internalCache.Add(key, cachedItem); Log.DebugFormat("Added item with Key: {0}", key); } } } /// /// Remove item from cache /// /// public void Remove(TK key) { lock (_lockObject) { if (!_internalCache.ContainsKey(key)) { throw new ApplicationException($"An object with key ‘{key}’ does not exists in cache"); } _internalCache.Remove(key); Log.DebugFormat("Removed item with Key: {0}", key); } } /// /// A cache item /// private class CachedItem { public event CacheObjectExpired Expired; private readonly int _secondsToExpire; private readonly Timer _timerEvent; public CachedItem(TK key, TV item, int secondsToExpire) { if (key == null) { throw new ArgumentNullException(nameof(key)); } Key = key; Item = item; _secondsToExpire = secondsToExpire; if (secondsToExpire <= 0) { return; } _timerEvent = new Timer(secondsToExpire * 1000) { AutoReset = false }; _timerEvent.Elapsed += timerEvent_Elapsed; _timerEvent.Start(); } private void ExpireNow() { _timerEvent.Stop(); if (_secondsToExpire > 0) { Expired?.Invoke(Key, Item); } } private void timerEvent_Elapsed(object sender, ElapsedEventArgs e) { ExpireNow(); } public TK Key { get; private set; } public TV Item { get; private set; } public static implicit operator TV(CachedItem a) { return a.Item; } } } }