SharedStateManager
is a Go package that provides a thread-safe, in-memory key-value store with support for conditional notifications and timed expiration of keys. It is designed for efficient internal messaging within your application.
- Thread-safe key-value storage
- Conditional notifications for subscribers
- Timed value expiration with notifications
- Simple API for setting, getting, and deleting values
go get github.com/christerso/shared-state-manager
import "github.com/christerso/shared-state-manager"
package main
import (
"fmt"
"time"
"github.com/christerso/shared-state-manager"
)
func main() {
ssm := managers.NewSharedStateManager()
// Define the event handler
eventHandler := func(data interface{}) {
switch v := data.(type) {
case managers.ExpirationNotification:
fmt.Println("Event expired for key:", v.Key)
default:
fmt.Println("Received:", data)
}
}
// Start subscription with conditional notifications enabled
managers.StartSubscription(ssm, "exampleEvent", eventHandler, true)
// Set a value
ssm.Set("exampleEvent", "Hello, EventBus!")
// Try setting the same value again (should not notify)
ssm.Set("exampleEvent", "Hello, EventBus!")
// Change the value (should notify)
ssm.Set("exampleEvent", "New Value")
// Start another subscription without conditional notifications
managers.StartSubscription(ssm, "exampleEvent", func(data interface{}) {
switch v := data.(type) {
case managers.ExpirationNotification:
fmt.Println("Always received expiration for key:", v.Key)
default:
fmt.Println("Always received:", data)
}
}, false)
// Set a value again (should notify both subscribers)
ssm.Set("exampleEvent", "Another Value")
// Set a timed value without a subscriber
ssm.SetWithTimeout("timedKey", "This will expire", 5*time.Second)
// Wait to observe the expiration
time.Sleep(6 * time.Second)
}
Creates a new instance of SharedStateManager
.
Sets a value for a given key.
Sets a value for a given key with an expiration time.
Retrieves a value for a given key.
Retrieves a string value for a given key.
Retrieves a struct value for a given key.
Removes a key-value pair.
Adds a subscriber for a specific key with conditional notifications.
Helper function to start a subscription goroutine with a handler.
This project is licensed under the MIT License.