Skip to content

Commit

Permalink
Avoid custom registration for the Json serializer (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericDelaporte authored Oct 24, 2022
1 parent a8ad6d6 commit 01dd6fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<string, object>), "tso");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public override async Task<object> GetAsync(object key, CancellationToken cancel
if (cachedData == null)
return null;

var entry = _serializer.Deserialize(cachedData) as Tuple<string, object>;
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;
}

/// <inheritdoc />
Expand All @@ -64,7 +66,7 @@ public override Task PutAsync(object key, object value, CancellationToken cancel
{

var (fullKey, cacheKey) = GetCacheKey(key);
var entry = new Tuple<string, object>(fullKey, value);
var entry = new object[] { fullKey, value };
var cachedData = _serializer.Serialize(entry);

var options = new DistributedCacheEntryOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ public override object Get(object key)
if (cachedData == null)
return null;

var entry = _serializer.Deserialize(cachedData) as Tuple<string, object>;
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;
}

/// <inheritdoc />
Expand All @@ -375,7 +377,7 @@ public override void Put(object key, object value)
}

var (fullKey, cacheKey) = GetCacheKey(key);
var entry = new Tuple<string, object>(fullKey, value);
var entry = new object[] { fullKey, value };
var cachedData = _serializer.Serialize(entry);

var options = new DistributedCacheEntryOptions();
Expand Down

0 comments on commit 01dd6fa

Please sign in to comment.