Use file-scoped namespaces

This commit is contained in:
Andriy
2023-07-29 15:14:52 +03:00
parent a24332370d
commit 8ab067a027
58 changed files with 2222 additions and 2280 deletions
+6 -7
View File
@@ -1,9 +1,8 @@
namespace Kruzya.TelegramBot.Core.Cache
namespace Kruzya.TelegramBot.Core.Cache;
public interface ICacheStorage<TKey, TValue>
{
public interface ICacheStorage<TKey, TValue>
{
public bool TryGetValue(TKey key, out TValue value);
public void SetValue(TKey key, TValue value, int timeToLive);
public TValue GetOr(TKey key, TValue value);
}
public bool TryGetValue(TKey key, out TValue value);
public void SetValue(TKey key, TValue value, int timeToLive);
public TValue GetOr(TKey key, TValue value);
}
+88 -89
View File
@@ -2,103 +2,102 @@
using System.Collections.Generic;
using System.Threading;
namespace Kruzya.TelegramBot.Core.Cache
namespace Kruzya.TelegramBot.Core.Cache;
public class MemoryCache<TKey, TValue> : ICacheStorage<TKey, TValue>
{
public class MemoryCache<TKey, TValue> : ICacheStorage<TKey, TValue>
/// <summary>
/// Represents a cache entry in internal dictionary.
/// </summary>
/// <typeparam name="TVal"></typeparam>
internal struct CacheEntry<TVal>
{
/// <summary>
/// Represents a cache entry in internal dictionary.
/// </summary>
/// <typeparam name="TVal"></typeparam>
internal struct CacheEntry<TVal>
{
public TVal Value;
public DateTime Created;
public int ExpiresAt;
public TVal Value;
public DateTime Created;
public int ExpiresAt;
public bool IsExpired()
{
if (ExpiresAt == Timeout.Infinite)
{
return false;
}
return (Created.AddSeconds(ExpiresAt)) < DateTime.Now;
}
}
/// <summary>
///
/// </summary>
private readonly Dictionary<TKey, CacheEntry<TValue>> _dict = new();
/// <summary>
/// The TTL for all entries if not set.
/// </summary>
private int _defaultTtl;
public MemoryCache() : this(Timeout.Infinite)
public bool IsExpired()
{
}
public MemoryCache(int defaultTtl)
{
_defaultTtl = defaultTtl;
}
public bool TryGetValue(TKey key, out TValue value)
{
value = default;
if (!_dict.ContainsKey(key))
if (ExpiresAt == Timeout.Infinite)
{
return false;
}
CacheEntry<TValue> temp;
if (!_dict.TryGetValue(key, out temp))
{
return false;
}
if (temp.IsExpired())
{
_dict.Remove(key);
return false;
}
value = temp.Value;
return true;
}
public TValue GetOr(TKey key, TValue defValue = default)
{
var doesExist = TryGetValue(key, out var value);
if (!doesExist)
{
value = defValue;
}
return value;
}
public void SetValue(TKey key, TValue value)
{
SetValue(key, value, _defaultTtl);
}
public void SetValue(TKey key, TValue value, int timeToLive)
{
if (_dict.ContainsKey(key))
{
_dict.Remove(key);
}
_dict.Add(key, new CacheEntry<TValue>()
{
Created = DateTime.Now,
ExpiresAt = timeToLive,
Value = value
});
return (Created.AddSeconds(ExpiresAt)) < DateTime.Now;
}
}
/// <summary>
///
/// </summary>
private readonly Dictionary<TKey, CacheEntry<TValue>> _dict = new();
/// <summary>
/// The TTL for all entries if not set.
/// </summary>
private int _defaultTtl;
public MemoryCache() : this(Timeout.Infinite)
{
}
public MemoryCache(int defaultTtl)
{
_defaultTtl = defaultTtl;
}
public bool TryGetValue(TKey key, out TValue value)
{
value = default;
if (!_dict.ContainsKey(key))
{
return false;
}
CacheEntry<TValue> temp;
if (!_dict.TryGetValue(key, out temp))
{
return false;
}
if (temp.IsExpired())
{
_dict.Remove(key);
return false;
}
value = temp.Value;
return true;
}
public TValue GetOr(TKey key, TValue defValue = default)
{
var doesExist = TryGetValue(key, out var value);
if (!doesExist)
{
value = defValue;
}
return value;
}
public void SetValue(TKey key, TValue value)
{
SetValue(key, value, _defaultTtl);
}
public void SetValue(TKey key, TValue value, int timeToLive)
{
if (_dict.ContainsKey(key))
{
_dict.Remove(key);
}
_dict.Add(key, new CacheEntry<TValue>()
{
Created = DateTime.Now,
ExpiresAt = timeToLive,
Value = value
});
}
}