RKSoftware.Packages.Caching.System.Text.Json.StreamConverter
3.0.0
See the version list below for details.
dotnet add package RKSoftware.Packages.Caching.System.Text.Json.StreamConverter --version 3.0.0
NuGet\Install-Package RKSoftware.Packages.Caching.System.Text.Json.StreamConverter -Version 3.0.0
<PackageReference Include="RKSoftware.Packages.Caching.System.Text.Json.StreamConverter" Version="3.0.0" />
<PackageVersion Include="RKSoftware.Packages.Caching.System.Text.Json.StreamConverter" Version="3.0.0" />
<PackageReference Include="RKSoftware.Packages.Caching.System.Text.Json.StreamConverter" />
paket add RKSoftware.Packages.Caching.System.Text.Json.StreamConverter --version 3.0.0
#r "nuget: RKSoftware.Packages.Caching.System.Text.Json.StreamConverter, 3.0.0"
#:package RKSoftware.Packages.Caching.System.Text.Json.StreamConverter@3.0.0
#addin nuget:?package=RKSoftware.Packages.Caching.System.Text.Json.StreamConverter&version=3.0.0
#tool nuget:?package=RKSoftware.Packages.Caching.System.Text.Json.StreamConverter&version=3.0.0
RKSoftware.Packages.Caching.System.Text.Json.StreamConverter
Binary System.Text.Json serialization for
RKSoftware.Packages.Caching. It
contains an IObjectToStreamConverter implementation together with the registration extension that
plugs it into the cache service, so cached objects are written to Redis as UTF-8 bytes and read back
from a stream.
The core caching package defines how values are cached but not how they are serialized — this package supplies the missing half. Register exactly one converter package in an application.
Installation
dotnet add package RKSoftware.Packages.Caching
dotnet add package RKSoftware.Packages.Caching.System.Text.Json.StreamConverter
Getting started
Chain the converter registration onto the cache registration:
using RKSoftware.Packages.Caching.Infrastructure;
using RKSoftware.Packages.Caching.System.Text.Json.StreamConverter;
builder.Services
.UseRKSoftwareCache(
builder.Configuration.GetSection(nameof(RedisCacheSettings)),
scopedKeyPrefix: "MyApp")
.UseSystemTextJsonStreamConverter();
Nothing else is required — the ICacheService API is unchanged, only the storage format differs:
using RKSoftware.Packages.Caching.Contract;
public sealed class ReportService(ICacheService cache, IReportRepository repository)
{
public Task<Report?> GetAsync(int id) =>
cache.GetOrSetCachedObjectAsync(
$"report.{id}",
() => repository.RenderAsync(id),
storageDuration: 600);
}
What it registers
UseSystemTextJsonStreamConverter() (namespace
RKSoftware.Packages.Caching.System.Text.Json.StreamConverter) adds two scoped services:
| Contract | Implementation |
|---|---|
IObjectToStreamConverter |
SystemTextJsonStreamConverter |
ICacheRepository |
StreamCacheRepository |
API overview
SystemTextJsonStreamConverter implements IObjectToStreamConverter:
| Member | Behaviour |
|---|---|
byte[] ToBytes<T>(T obj) |
JsonSerializer.SerializeToUtf8Bytes of the value. |
Task<T?> FromStreamAsync<T>(Stream data) |
JsonSerializer.DeserializeAsync over the stream; returns null when the payload deserializes to null. |
FromStreamAsync<T> is constrained to where T : class, matching ICacheService. Serialization
uses the System.Text.Json defaults, so annotate your models with
System.Text.Json.Serialization attributes ([JsonPropertyName], [JsonIgnore],
[JsonConverter]) to influence the output.
When to use this instead of a text converter
The two text converters serialize through an intermediate string; this one goes straight to and
from UTF-8 bytes. For large cached objects that avoids allocating the whole JSON document as a
string on every read and write, which reduces large-object-heap pressure.
For small values the difference is negligible, and the text converters produce human-readable
entries that are easier to inspect with redis-cli.
Register exactly one converter package. All three register
ICacheRepository, so adding more than one leaves the last registration in effect and silently changes the storage format. Values written by a text converter cannot be read back by this one, and vice versa — switching converters in an existing deployment requires flushing the affected keys.
Related packages
— the core caching abstractions this package extends. Required.
— JSON string serialization with
System.Text.Json.— JSON string serialization with
Newtonsoft.Json.
Source, issues and full documentation: github.com/rk-software-systems/rk-dn-redis-cache-provider
License
Released into the public domain under The Unlicense.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- RKSoftware.Packages.Caching (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.