Basic Cache Operations with the Go Client
|
Tip
|
The source code for this tutorial is available on GitHub. |
This tutorial demonstrates how to connect to an Infinispan server and perform basic cache operations using the Go Hot Rod client.
Prerequisites
-
A running Infinispan server.
-
Go 1.25 or later.
Running the Example
go run ./cache
Code Walkthrough
Connecting to the Server
The example connects to the Infinispan server using a Hot Rod URI.
You can override the default URI by setting the INFINISPAN_URI environment variable.
client, err := hotrod.NewClient(ctx, "hotrod://admin:password@localhost:11222")
Creating the Cache
The example creates a distributed cache with ProtoStream encoding using the administration API.
client.Administration().GetOrCreateCache(ctx, "test", cacheConfig)
Storing and Retrieving Entries
Use Put to store a key-value pair and Get to retrieve it.
cache.Put(ctx, []byte("key"), []byte("value"))
val, found, err := cache.Get(ctx, []byte("key"))
Expected Output
Cache 'test' ready.
key = value
Cache removed.


