Cache Encodings with the .NET Client
|
Tip
|
The source code for this tutorial is available on GitHub. |
This tutorial demonstrates how to use different cache encodings with the .NET Hot Rod client.
Prerequisites
-
A running Infinispan server.
-
.NET 10 or later.
Running the Example
dotnet run --project encoding
Code Walkthrough
Text Cache
Create a cache with text/plain encoding.
The builder infers StringMarshaller from the string type parameter.
var textCache = client.NewCache<string>("textCache")
.WithEncoding(MediaType.PlainText)
.Build();
await textCache.Put("greeting", "Hello, Infinispan!");
var val = await textCache.Get("greeting");
JSON Cache
Create a cache with application/json encoding and store JSON documents.
Keys must be valid JSON values (e.g. a quoted string).
var jsonCache = client.NewCache<string>("jsonCache")
.WithEncoding(MediaType.JSON)
.Build();
await jsonCache.Put("\"name\"", "{\"project\": \"infinispan\"}");
Expected Output
Caches ready.
== Cache with text/plain encoding ==
textCache[greeting] = Hello, Infinispan!
== Cache with application/json encoding ==
jsonCache[name] = {"project": "infinispan"}
Caches removed.


