presets
packageAPI reference for the presets
package.
Imports
(14)github.com/mirkobrombin/go-warp/v1/adapter
INT
github.com/mirkobrombin/go-warp/v1/cache
INT
github.com/mirkobrombin/go-warp/v1/core
INT
github.com/mirkobrombin/go-warp/v1/merge
INT
github.com/mirkobrombin/go-warp/v1/syncbus
INT
github.com/mirkobrombin/go-warp/v1/syncbus/mesh
INT
github.com/mirkobrombin/go-warp/v1/syncbus/nats
INT
github.com/mirkobrombin/go-warp/v1/syncbus/redis
PKG
github.com/nats-io/nats.go
PKG
github.com/redis/go-redis/v9
STD
context
STD
testing
STD
time
PKG
github.com/alicebob/miniredis/v2
RedisOptions
RedisOptions configures the connection to Redis.
type RedisOptions struct
Fields
| Name | Type | Description |
|---|---|---|
| Addr | string | |
| Password | string | |
| DB | int |
NewRedisEventual
NewRedisEventual creates a Warp instance configured for Eventual Consistency
using Redis as both the L2 Store and the Synchronization Bus.
This is suitable for high-performance caching where occasional staleness is acceptable.
Parameters
Returns
func NewRedisEventual[T any](opts RedisOptions) *core.Warp[T]
{
client := redis.NewClient(&redis.Options{
Addr: opts.Addr,
Password: opts.Password,
DB: opts.DB,
})
// L1: InMemory with Merge capabilities
c := cache.NewInMemory[merge.Value[T]]()
// L2: Redis Store
store := adapter.NewRedisStore[T](client)
// Bus: Redis Pub/Sub
bus := busredis.NewRedisBus(busredis.RedisBusOptions{Client: client})
// Engine: Standard Last-Write-Wins (or Vector Clock if T is Versioned, but default Engine is simpler)
engine := merge.NewEngine[T]()
return core.New[T](c, store, bus, engine)
}
NewRedisStrong
NewRedisStrong creates a Warp instance configured for Strong Consistency
using Redis as the L2 Store and Bus.
Note: True strong consistency requires Quorum writes which are slower.
Parameters
Returns
func NewRedisStrong[T any](opts RedisOptions) *core.Warp[T]
{
// Re-use logic, the difference is mainly in how the user Registers keys (ModeStrong).
// But we can enable optimization flags here if needed.
// For now, it returns the same structure, but the naming guides the user intent.
return NewRedisEventual[T](opts)
}
NewMeshEventual
NewMeshEventual creates a Warp instance configured for Eventual Consistency
using Warp Mesh (UDP P2P) for synchronization and an in-memory L2 store.
This provides zero-infrastructure clustering for Go backends.
Parameters
Returns
func NewMeshEventual[T any](opts mesh.MeshOptions) *core.Warp[T]
{
c := cache.NewInMemory[merge.Value[T]]()
s := adapter.NewInMemoryStore[T]()
bus, _ := mesh.NewMeshBus(opts)
engine := merge.NewEngine[T]()
return core.New[T](c, s, bus, engine)
}
NewInMemoryStandalone
NewInMemoryStandalone creates a Warp instance that runs entirely in-memory
with no external dependencies. Useful for local development or simple caching.
Returns
func NewInMemoryStandalone[T any]() *core.Warp[T]
{
c := cache.NewInMemory[merge.Value[T]]()
s := adapter.NewInMemoryStore[T]()
b := syncbus.NewInMemoryBus()
engine := merge.NewEngine[T]()
return core.New[T](c, s, b, engine)
}
NATSOptions
NATSOptions configures a connection to a NATS server.
type NATSOptions struct
Fields
| Name | Type | Description |
|---|---|---|
| Conn | *nats.Conn |
NewNATSEventual
NewNATSEventual creates a Warp instance configured for eventual consistency
using NATS Core as the synchronization bus. L2 is in-memory per node, making
this preset suitable when NATS is already present in the infrastructure but
a shared persistent store is not required.
Parameters
Returns
func NewNATSEventual[T any](opts NATSOptions) *core.Warp[T]
{
c := cache.NewInMemory[merge.Value[T]]()
s := adapter.NewInMemoryStore[T]()
bus := busnats.NewNATSBus(opts.Conn)
engine := merge.NewEngine[T]()
return core.New[T](c, s, bus, engine)
}
NewNATSStrong
NewNATSStrong creates a Warp instance backed by NATS with strong-consistency
semantics as the user-facing intent.
NOTE: the current implementation is identical to NewNATSEventual — strong
consistency is not enforced at this time. This constructor is provided as a
semantic marker for future enforcement and to signal intent at the call site.
Do not rely on strong consistency guarantees in production until this is
explicitly implemented.
Parameters
Returns
func NewNATSStrong[T any](opts NATSOptions) *core.Warp[T]
{
return NewNATSEventual[T](opts)
}
TestNewInMemoryStandalone
Parameters
func TestNewInMemoryStandalone(t *testing.T)
{
w := NewInMemoryStandalone[string]()
ctx := context.Background()
w.Register("foo", core.ModeStrongLocal, time.Minute)
if err := w.Set(ctx, "foo", "bar"); err != nil {
t.Fatalf("Set failed: %v", err)
}
val, err := w.Get(ctx, "foo")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if val != "bar" {
t.Fatalf("expected bar, got %s", val)
}
}
TestNewRedisEventual
Parameters
func TestNewRedisEventual(t *testing.T)
{
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("miniredis run: %v", err)
}
defer mr.Close()
w := NewRedisEventual[string](RedisOptions{Addr: mr.Addr()})
ctx := context.Background()
w.Register("foo", core.ModeEventualDistributed, time.Minute)
if err := w.Set(ctx, "foo", "bar"); err != nil {
t.Fatalf("Set failed: %v", err)
}
val, err := w.Get(ctx, "foo")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if val != "bar" {
t.Fatalf("expected bar, got %s", val)
}
}