aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Data.Caching/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/VNLib.Data.Caching/src')
-rw-r--r--lib/VNLib.Data.Caching/src/ClientExtensions.cs6
-rw-r--r--lib/VNLib.Data.Caching/src/ClientRetryManager.cs5
-rw-r--r--lib/VNLib.Data.Caching/src/JsonCacheObjectSerializer.cs35
-rw-r--r--lib/VNLib.Data.Caching/src/ReusableJsonWriter.cs69
4 files changed, 24 insertions, 91 deletions
diff --git a/lib/VNLib.Data.Caching/src/ClientExtensions.cs b/lib/VNLib.Data.Caching/src/ClientExtensions.cs
index a2ec27d..8273486 100644
--- a/lib/VNLib.Data.Caching/src/ClientExtensions.cs
+++ b/lib/VNLib.Data.Caching/src/ClientExtensions.cs
@@ -45,7 +45,7 @@ namespace VNLib.Data.Caching
public static class ClientExtensions
{
- private static readonly JsonCacheObjectSerializer DefaultSerializer = new();
+ private static readonly JsonCacheObjectSerializer DefaultSerializer = new(256);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void LogDebug(this FBMClient client, string message, params object?[] args)
@@ -192,7 +192,7 @@ namespace VNLib.Data.Caching
/// <exception cref="InvalidResponseException"></exception>
/// <exception cref="MessageTooLargeException"></exception>
/// <exception cref="ObjectNotFoundException"></exception>
- public async static Task AddOrUpdateObjectAsync<T>(this FBMClient client, string objectId, string? newId, ObjectDataReader<T> callback, T state, CancellationToken cancellationToken = default)
+ public static async Task AddOrUpdateObjectAsync<T>(this FBMClient client, string objectId, string? newId, ObjectDataReader<T> callback, T state, CancellationToken cancellationToken = default)
{
_ = client ?? throw new ArgumentNullException(nameof(client));
_ = callback ?? throw new ArgumentNullException(nameof(callback));
@@ -306,7 +306,7 @@ namespace VNLib.Data.Caching
return deserialzer.Deserialize<T>(response.ResponseBody);
}
- //Object may not exist on the server yet
+ //Object may not exist on the server yet
if (status.Value.Equals(ResponseCodes.NotFound, StringComparison.Ordinal))
{
return default;
diff --git a/lib/VNLib.Data.Caching/src/ClientRetryManager.cs b/lib/VNLib.Data.Caching/src/ClientRetryManager.cs
index 5cee583..b7a5f2a 100644
--- a/lib/VNLib.Data.Caching/src/ClientRetryManager.cs
+++ b/lib/VNLib.Data.Caching/src/ClientRetryManager.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Data.Caching
@@ -32,7 +32,7 @@ using VNLib.Net.Messaging.FBM.Client;
namespace VNLib.Data.Caching
{
/// <summary>
- /// Manages a <see cref="FBMClientWorkerBase"/> reconnect policy
+ /// Manages a <see cref="IStatefulConnection"/> reconnect policy
/// </summary>
public class ClientRetryManager<T> : VnDisposeable where T: IStatefulConnection
{
@@ -62,6 +62,7 @@ namespace VNLib.Data.Caching
/// Raised before client is to be reconnected
/// </summary>
public event Action<T>? OnBeforeReconnect;
+
/// <summary>
/// Raised when the client fails to reconnect. Should return a value that instructs the
/// manager to reconnect
diff --git a/lib/VNLib.Data.Caching/src/JsonCacheObjectSerializer.cs b/lib/VNLib.Data.Caching/src/JsonCacheObjectSerializer.cs
index 85d1184..09fbd6c 100644
--- a/lib/VNLib.Data.Caching/src/JsonCacheObjectSerializer.cs
+++ b/lib/VNLib.Data.Caching/src/JsonCacheObjectSerializer.cs
@@ -23,12 +23,13 @@
*/
using System;
+
+using System.IO;
using System.Buffers;
+using System.Threading;
using System.Text.Json;
using System.Text.Json.Serialization;
-using VNLib.Utils.Memory.Caching;
-
namespace VNLib.Data.Caching
{
/// <summary>
@@ -37,8 +38,7 @@ namespace VNLib.Data.Caching
/// </summary>
public class JsonCacheObjectSerializer : ICacheObjectSerializer, ICacheObjectDeserializer
{
- //Create threadlocal writer for attempted lock-free writer reuse
- private static readonly ObjectRental<ReusableJsonWriter> JsonWriterPool = ObjectRental.CreateThreadLocal<ReusableJsonWriter>();
+ private static readonly ThreadLocal<Utf8JsonWriter> _writer = new(static () => new(Stream.Null));
private readonly JsonSerializerOptions? _options;
@@ -46,16 +46,13 @@ namespace VNLib.Data.Caching
/// Initializes a new <see cref="JsonCacheObjectSerializer"/>
/// </summary>
/// <param name="options">JSON serialization/deserialization options</param>
- public JsonCacheObjectSerializer(JsonSerializerOptions options)
- {
- _options = options;
- }
+ public JsonCacheObjectSerializer(JsonSerializerOptions options) => _options = options;
/// <summary>
/// Initializes a new <see cref="JsonCacheObjectSerializer"/> using
/// the default serialization rules
/// </summary>
- public JsonCacheObjectSerializer()
+ public JsonCacheObjectSerializer(int bufferSize)
{
//Configure default serialzation options
_options = new()
@@ -68,9 +65,7 @@ namespace VNLib.Data.Caching
IgnoreReadOnlyFields = true,
PropertyNameCaseInsensitive = true,
IncludeFields = false,
-
- //Use small buffers
- DefaultBufferSize = 128
+ DefaultBufferSize = bufferSize
};
}
@@ -80,17 +75,23 @@ namespace VNLib.Data.Caching
///<inheritdoc/>
public virtual void Serialize<T>(T obj, IBufferWriter<byte> finiteWriter)
{
- //Rent new json writer
- ReusableJsonWriter writer = JsonWriterPool.Rent();
+ //Read thread-local writer
+ Utf8JsonWriter localWriter = _writer.Value!;
+ //Init the writer with the new buffer writer
+ localWriter.Reset(finiteWriter);
try
{
- //Serialize the message
- writer.Serialize(finiteWriter, obj, _options);
+ //Serialize message
+ JsonSerializer.Serialize(localWriter, obj, _options);
+
+ //Flush writer to underlying buffer
+ localWriter.Flush();
}
finally
{
- JsonWriterPool.Return(writer);
+ //Unlink the writer
+ localWriter.Reset(Stream.Null);
}
}
}
diff --git a/lib/VNLib.Data.Caching/src/ReusableJsonWriter.cs b/lib/VNLib.Data.Caching/src/ReusableJsonWriter.cs
deleted file mode 100644
index c763f91..0000000
--- a/lib/VNLib.Data.Caching/src/ReusableJsonWriter.cs
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
-* Copyright (c) 2022 Vaughn Nugent
-*
-* Library: VNLib
-* Package: VNLib.Data.Caching
-* File: ReusableJsonWriter.cs
-*
-* ReusableJsonWriter.cs is part of VNLib.Data.Caching which is part of the larger
-* VNLib collection of libraries and utilities.
-*
-* VNLib.Data.Caching is free software: you can redistribute it and/or modify
-* it under the terms of the GNU Affero General Public License as
-* published by the Free Software Foundation, either version 3 of the
-* License, or (at your option) any later version.
-*
-* VNLib.Data.Caching is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU Affero General Public License for more details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program. If not, see https://www.gnu.org/licenses/.
-*/
-
-using System.IO;
-using System.Buffers;
-using System.Text.Json;
-
-using VNLib.Utils;
-
-namespace VNLib.Data.Caching
-{
- internal sealed class ReusableJsonWriter : VnDisposeable
- {
- private readonly Utf8JsonWriter _writer;
-
- public ReusableJsonWriter()
- {
- _writer = new(Stream.Null);
- }
-
- /// <summary>
- /// Serializes the message and writes the serialzied data to the buffer writer
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="writer">The buffer writer to store data at</param>
- /// <param name="value">The object to serialize</param>
- /// <param name="options">Optional - serializer options</param>
- public void Serialize<T>(IBufferWriter<byte> writer, T value, JsonSerializerOptions? options = null)
- {
- //Init the writer with the new buffer writer
- _writer.Reset(writer);
- try
- {
- //Serialize message
- JsonSerializer.Serialize(_writer, value, options);
- //Flush writer to underlying buffer
- _writer.Flush();
- }
- finally
- {
- //Unlink the writer
- _writer.Reset(Stream.Null);
- }
- }
-
- protected override void Free() => _writer.Dispose();
- }
-}