School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,122 @@
using Timer = System.Timers.Timer;
namespace CacheModule{
public class CacheService
{
private readonly Dictionary<string, Dictionary<string, CachedObject>> _cache = new Dictionary<string, Dictionary<string, CachedObject>>();
private static object _lock = new object();
private readonly Timer _timer = new Timer();
private static CacheService? _instance;
private CacheService(){
_timer.Interval = 3600 * 1000;
_timer.Elapsed += (sender, e) => ClearExpired();
_timer.Start();
}
public static CacheService Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new CacheService();
}
return _instance;
}
}
}
public void Add(string functionName, string functionParams, object value, Duration duration)
{
Add(functionName, functionParams, value, (int)duration);
}
public void Add(string functionName, string functionParams, object value, int duration)
{
if (!_cache.ContainsKey(functionName))
{
_cache[functionName] = new Dictionary<string, CachedObject>();
}
_cache[functionName][functionParams] = new CachedObject(){
Value = value,
Timestamp = DateTime.Now,
Duration = duration
};
}
public object? Get(string functionName, string functionParams)
{
if (!_cache.ContainsKey(functionName))
{
return null;
}
if (!_cache[functionName].ContainsKey(functionParams))
{
return null;
}
var cachedObject = _cache[functionName][functionParams];
if (cachedObject.IsExpired())
{
_cache[functionName].Remove(functionParams);
return null;
}
return cachedObject.Value;
}
public void Clear()
{
_cache.Clear();
}
public void Clear(string functionName)
{
if (_cache.ContainsKey(functionName))
{
_cache.Remove(functionName);
}
}
public void Clear(string functionName, string functionParams)
{
if (_cache.ContainsKey(functionName))
{
if (_cache[functionName].ContainsKey(functionParams))
{
_cache[functionName].Remove(functionParams);
if (_cache[functionName].Count == 0)
{
_cache.Remove(functionName);
}
}
}
}
public void ClearExpired()
{
foreach (var functionName in _cache.Keys)
{
foreach (var functionParams in _cache[functionName].Keys)
{
if (_cache[functionName][functionParams].IsExpired())
{
_cache[functionName].Remove(functionParams);
if (_cache[functionName].Count == 0)
{
_cache.Remove(functionName);
}
}
}
}
}
}
}
@@ -0,0 +1,17 @@
using System;
namespace CacheModule{
public class CachedObject
{
public object? Value { get; set; }
public DateTime Timestamp {get; set; }
public int Duration { get; set; }
public bool IsExpired()
{
return DateTime.Now > Timestamp.AddSeconds(Duration);
}
}
}
@@ -0,0 +1,22 @@
namespace CacheModule{
public enum Duration
{
OneSecond = 1,
OneMinute = 60,
OneHour = 3600,
OneDay = 86400,
OneWeek = 604800,
OneMonth = 2592000,
OneYear = 31536000,
FiveSeconds = 5,
ThirtySeconds = 30,
ThirtyMinutes = 1800,
SixHours = 21600,
TwelveHours = 43200,
TwoDays = 172800,
ThreeDays = 259200,
TwoWeeks = 1209600,
ThreeMonths = 7776000,
SixMonths = 15552000
}
}