reg := kevent.NewRegistry()
// Wired once at startup, per event type.
kevent.Register(reg, kevent.NewBus[OrderPlaced]())
kevent.Register(reg, kevent.NewBus[UserSignedUp]())
// Elsewhere, code that only knows the event type gets the right bus.
orders := kevent.From[OrderPlaced](reg)
orders.Subscribe(func(ctx context.Context, e OrderPlaced) error {
fmt.Println("emailing receipt for", e.ID)
return nil
})
orders.Publish(ctx, OrderPlaced{ID: "o4", Total: 7})
See pkg/kevent for the full package docs and runnable
examples.
kcache
Cache
profiles := kcache.NewCache[string, UserProfileDTO]()
profiles.Set("user-1", UserProfileDTO{Name: "Alice", Plan: "pro"}, 5*time.Minute)
if p, ok := profiles.Get("user-1"); ok {
fmt.Printf("%s is on the %s plan\n", p.Name, p.Plan)
}
Registry
reg := kcache.NewRegistry()
// Wired once at startup, per named cache.
kcache.Register(reg, "sessions", kcache.NewCache[string, string]())
kcache.Register(reg, "profiles", kcache.NewCache[string, UserProfileDTO]())
// Elsewhere, code that only knows the name gets the right cache.
sessions := kcache.From[string, string](reg, "sessions")
sessions.Set("session-1", "alice", time.Minute)
See pkg/kcache for the full package docs and runnable
examples.