Merge branch 'feature/cache' into dev

This commit is contained in:
2020-03-03 13:16:28 +04:00
5 changed files with 181 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
using System;
namespace Kruzya.TelegramBot.Core.Cache
{
public interface ICacheStorage<TKey, TValue>
{
public bool TryGetValue(TKey key, out TValue value);
public void SetValue(TKey key, TValue value, int timeToLive);
}
}
+95
View File
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SQLitePCL;
namespace Kruzya.TelegramBot.Core.Cache
{
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>
{
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 Dictionary<TKey, CacheEntry<TValue>> dict = new Dictionary<TKey, CacheEntry<TValue>>();
/// <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 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
});
}
}
}
+6
View File
@@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CombotAntiSpam", "modules\C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RichSiteSummary", "modules\RichSiteSummary\RichSiteSummary.csproj", "{C50D64C3-204B-4B58-927B-C8D759787252}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests.csproj", "{5C62CA70-F270-4029-953E-458623C31A3B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
{C50D64C3-204B-4B58-927B-C8D759787252}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C50D64C3-204B-4B58-927B-C8D759787252}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C50D64C3-204B-4B58-927B-C8D759787252}.Release|Any CPU.Build.0 = Release|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
+47
View File
@@ -0,0 +1,47 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Kruzya.TelegramBot.Core.Cache;
using NUnit.Framework;
namespace Kruzya.TelegramBot.Core.Tests
{
public class MemoryCache
{
[Test]
public void GetSetItem()
{
var cache = new MemoryCache<string, int>();
int id;
Assert.IsFalse(cache.TryGetValue("user", out id), "GetValue() returned something");
id = 5;
cache.SetValue("user", id);
id = 10;
Assert.IsTrue(cache.TryGetValue("user", out id), "GetValue() doesn't returned any value");
Assert.IsTrue(id == 5, "GetValue() returned don't expected value");
}
[Test]
public void TimeToLive()
{
var cache = new MemoryCache<string, int>();
cache.SetValue("user", 5, 1);
cache.SetValue("admin", 10, 2);
cache.SetValue("manager", 15, Timeout.Infinite);
int id;
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(cache.TryGetValue("user", out id), "GetValue() returned expired entry (user).");
Assert.IsTrue(cache.TryGetValue("admin", out id), "GetValue() doesn't returned live entry.");
Assert.IsTrue(id == 10, "GetValue() returned unexpected entry.");
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsFalse(cache.TryGetValue("admin", out id), "GetValue() returned expired entry (admin).");
Assert.IsTrue(cache.TryGetValue("manager", out id), "GetValue() doesn't returned live entry.");
Assert.IsTrue(id == 15, "GetValue() returned unexpected entry.");
}
}
}
+23
View File
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>TelegramBot.Test</AssemblyName>
<RootNamespace>Kruzya.TelegramBot.Core.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
</Project>