From 01dd6fa435a18c7cfce99b5ed0682996f1b87251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= <12201973+fredericDelaporte@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:22:18 +0200 Subject: [PATCH] Avoid custom registration for the Json serializer (#117) --- .../Async/CoreDistributedCacheFixture.cs | 2 +- .../CoreDistributedCacheFixture.cs | 10 +--------- .../Async/CoreDistributedCache.cs | 8 +++++--- .../CoreDistributedCache.cs | 8 +++++--- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/Async/CoreDistributedCacheFixture.cs b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/Async/CoreDistributedCacheFixture.cs index 2487fff..bccb8f9 100644 --- a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/Async/CoreDistributedCacheFixture.cs +++ b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/Async/CoreDistributedCacheFixture.cs @@ -78,7 +78,7 @@ public async Task CanUseCacheKeyWithJsonSerializerAsync() const string value = "valuePut"; var props = GetDefaultProperties(); - props["cache.serializer"] = typeof(DistributedCacheJsonSerializer).AssemblyQualifiedName; + props["cache.serializer"] = typeof(JsonCacheSerializer).AssemblyQualifiedName; var cache = (CacheBase) DefaultProvider.BuildCache(DefaultRegion, props); // Due to async version, it may already be there. await (cache.RemoveAsync(key, CancellationToken.None)); diff --git a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheFixture.cs b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheFixture.cs index c196766..26381b1 100644 --- a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheFixture.cs +++ b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheFixture.cs @@ -73,7 +73,7 @@ public void CanUseCacheKeyWithJsonSerializer() const string value = "valuePut"; var props = GetDefaultProperties(); - props["cache.serializer"] = typeof(DistributedCacheJsonSerializer).AssemblyQualifiedName; + props["cache.serializer"] = typeof(JsonCacheSerializer).AssemblyQualifiedName; var cache = (CacheBase) DefaultProvider.BuildCache(DefaultRegion, props); // Due to async version, it may already be there. cache.Remove(key); @@ -85,13 +85,5 @@ public void CanUseCacheKeyWithJsonSerializer() Assert.That(item, Is.Not.Null, "Unable to retrieve cached item"); Assert.That(item, Is.EqualTo(value), "didn't return the item we added"); } - - private class DistributedCacheJsonSerializer : JsonCacheSerializer - { - public DistributedCacheJsonSerializer() - { - RegisterType(typeof(Tuple), "tso"); - } - } } } diff --git a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/Async/CoreDistributedCache.cs b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/Async/CoreDistributedCache.cs index 2c13244..0ec7801 100644 --- a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/Async/CoreDistributedCache.cs +++ b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/Async/CoreDistributedCache.cs @@ -40,8 +40,10 @@ public override async Task GetAsync(object key, CancellationToken cancel if (cachedData == null) return null; - var entry = _serializer.Deserialize(cachedData) as Tuple; - return Equals(entry?.Item1, fullKey) ? entry.Item2 : null; + var entry = _serializer.Deserialize(cachedData) as object[]; + if (entry == null || entry.Length != 2) + return null; + return Equals(entry[0], fullKey) ? entry[1] : null; } /// @@ -64,7 +66,7 @@ public override Task PutAsync(object key, object value, CancellationToken cancel { var (fullKey, cacheKey) = GetCacheKey(key); - var entry = new Tuple(fullKey, value); + var entry = new object[] { fullKey, value }; var cachedData = _serializer.Serialize(entry); var options = new DistributedCacheEntryOptions(); diff --git a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCache.cs b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCache.cs index 6e03ca9..70237aa 100644 --- a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCache.cs +++ b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache/CoreDistributedCache.cs @@ -357,8 +357,10 @@ public override object Get(object key) if (cachedData == null) return null; - var entry = _serializer.Deserialize(cachedData) as Tuple; - return Equals(entry?.Item1, fullKey) ? entry.Item2 : null; + var entry = _serializer.Deserialize(cachedData) as object[]; + if (entry == null || entry.Length != 2) + return null; + return Equals(entry[0], fullKey) ? entry[1] : null; } /// @@ -375,7 +377,7 @@ public override void Put(object key, object value) } var (fullKey, cacheKey) = GetCacheKey(key); - var entry = new Tuple(fullKey, value); + var entry = new object[] { fullKey, value }; var cachedData = _serializer.Serialize(entry); var options = new DistributedCacheEntryOptions();