diff --git a/Core/Cache/ICacheStorage.cs b/Core/Cache/ICacheStorage.cs index 016f554..0983354 100644 --- a/Core/Cache/ICacheStorage.cs +++ b/Core/Cache/ICacheStorage.cs @@ -4,7 +4,7 @@ namespace Kruzya.TelegramBot.Core.Cache { public interface ICacheStorage { - public bool TryGetValue(TKey key, ref TValue value); + public bool TryGetValue(TKey key, out TValue value); public void SetValue(TKey key, TValue value, int timeToLive); } } \ No newline at end of file diff --git a/Core/Cache/MemoryCache.cs b/Core/Cache/MemoryCache.cs index b4afc69..baf063f 100644 --- a/Core/Cache/MemoryCache.cs +++ b/Core/Cache/MemoryCache.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; +using SQLitePCL; namespace Kruzya.TelegramBot.Core.Cache { @@ -47,9 +48,10 @@ namespace Kruzya.TelegramBot.Core.Cache _defaultTtl = defaultTtl; } - public bool TryGetValue(TKey key, ref TValue value) + public bool TryGetValue(TKey key, out TValue value) { - if (dict.ContainsKey(key)) + value = default; + if (!dict.ContainsKey(key)) { return false; } diff --git a/Kruzya.TelegramBot.sln b/Kruzya.TelegramBot.sln index 4e5a92d..05b2821 100644 --- a/Kruzya.TelegramBot.sln +++ b/Kruzya.TelegramBot.sln @@ -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} diff --git a/tests/MemoryCache.cs b/tests/MemoryCache.cs new file mode 100644 index 0000000..5c3cb2d --- /dev/null +++ b/tests/MemoryCache.cs @@ -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(); + + 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(); + 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."); + } + } +} \ No newline at end of file diff --git a/tests/Tests.csproj b/tests/Tests.csproj new file mode 100644 index 0000000..fab280a --- /dev/null +++ b/tests/Tests.csproj @@ -0,0 +1,23 @@ + + + + netcoreapp3.1 + + false + + TelegramBot.Test + + Kruzya.TelegramBot.Core.Tests + + + + + + + + + + + + +