Documentation
¶
Overview ¶
Package mnemo provides a simple and robust way to generate and manage caches for any data type.
Example ¶
// Create some keys for stores, caches, and commands var ( ExampleStoreKey StoreKey = "example_store" ExampleCacheKey CacheKey = "example_cache" ExampleCmdKey CommandKey = "example_cmd" ) m := New() // Add optional configuration for one server per instance // Instances do not require a server to access stores, caches, or commands // However, a server is required to access the Mnemo API with a client m.WithServer("example", WithPattern("/example"), WithPort(8080)) // Create a new store store, _ := NewStore(ExampleStoreKey) // Add commands to the store cmd := store.Commands() cmd.Assign(map[CommandKey]func(){ ExampleCmdKey: func() { fmt.Println("I'm a command!") }, }) // Create a type to cache type Message struct { Msg string } // Create a new cache for messages cache, _ := NewCache[Message](ExampleStoreKey, ExampleCacheKey) // Cache a message cache.Cache(ExampleCacheKey, &Message{Msg: "Hello, Mnemo!"}) // Set a reducer for the cache // A reducer is a function that takes the current state of the cache and returns a mutation // The mutation is then cached and sent to the reducer's feed channel cache.SetReducer(func(state Message) (mutation any) { return state.Msg + " With a reducer!" }) // The integrity of the original cache is maintained and stored by it's creation time _ = cache.RawHistory() // Add store(s) by key // This can be done at any time, however, it is recommended to do so before attempting to access the store // Unique stores may only be added to one instance of Mnemo, although you may have multiple instances m.WithStores(ExampleStoreKey) // Access stores or caches by key from anywhere in the application myStore, _ := UseStore(ExampleStoreKey) // Access and execute commands from anywhere myCmds := myStore.Commands() myCmds.Execute(ExampleCmdKey) // List commands if needed _ = myCmds.List() // Access caches from anywhere myCache, _ := UseCache[Message](ExampleStoreKey, ExampleCacheKey) // Access data from the cache item, _ := myCache.Get(ExampleCacheKey) fmt.Println(item.Data.Msg) // Update the cache myCache.Update(ExampleCacheKey, Message{Msg: "Hello, Mnemo! With an update!"}) // Server is a wrapper around http.Server and is non-blocking m.Server().ListenAndServe()
Output: I'm a command! Hello, Mnemo! With a reducer!
Index ¶
- func NewCacheTimeoutConfig[T any](data *T, key interface{}, timeoutFun func(data *T), timeout time.Duration) cacheTimeoutConfig[T]
- type Cache
- func (c *Cache[T]) Cache(key CacheKey, data *T) error
- func (c *Cache[T]) CacheWithTimeout(cfg cacheTimeoutConfig[T]) error
- func (c *Cache[T]) DefaultReducer(state T) (mutation any)
- func (c *Cache[T]) Delete(key interface{}) error
- func (c *Cache[T]) Get(key CacheKey) (Item[T], bool)
- func (c *Cache[T]) GetAll() map[CacheKey]Item[T]
- func (c *Cache[T]) RawFeed() chan map[time.Time]map[CacheKey]Item[T]
- func (c *Cache[T]) RawHistory() map[time.Time]map[CacheKey]Item[T]
- func (c *Cache[T]) ReducerFeed() chan reducerFeed[any]
- func (c *Cache[T]) ReducerHistory() []reducerHistory[any]
- func (c *Cache[T]) SetReducer(rf ReducerFunc[T, any])
- func (c *Cache[T]) Update(key CacheKey, update T) bool
- type CacheKey
- type CommandKey
- type Commands
- type Conn
- type Error
- type Item
- type LogLevel
- type Logger
- type Mnemo
- type Opt
- type Pool
- type ReducerFunc
- type Server
- type Store
- type StoreKey
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCacheTimeoutConfig ¶
func NewCacheTimeoutConfig[T any]( data *T, key interface{}, timeoutFun func(data *T), timeout time.Duration, ) cacheTimeoutConfig[T]
NewCacheTimeoutConfig creates a new cacheTimeoutConfig. TODO: Convert to option
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
cache is a collection of data that can be cached and reduced.
func NewCache ¶
NewCache creates a new cache or returns an error if a cache with the same key already exists.
func (*Cache[T]) CacheWithTimeout ¶
TODO: Convert to option
func (*Cache[T]) DefaultReducer ¶
DefaultReducer is a reducer that returns the raw cache.
func (*Cache[T]) GetAll ¶
func (c *Cache[T]) GetAll() map[CacheKey]Item[T]
GetAll returns all caches.
func (*Cache[T]) RawHistory ¶
RawHistory returns the raw cache history.
func (*Cache[T]) ReducerFeed ¶
ReducerFeed returns a channel of the reduced cache updates.
func (*Cache[T]) ReducerHistory ¶
ReducerHistory returns the reduced cache history.
func (*Cache[T]) SetReducer ¶
SetReducer sets the user defined reducer function and starts monitoring changes.
Setting a reducer is mandatory for triggering change monitoring. DefaultReducer is available but merely returns the raw cache so it is not recommended if you are caching complex data types that cannot not be serialized to json.
type Commands ¶
type Commands struct {
// contains filtered or unexported fields
}
Commands is a collection of commands.
func (*Commands) Assign ¶
func (c *Commands) Assign(cmds map[CommandKey]func())
Assign assigns a map of commands to the collection.
func (*Commands) Execute ¶
func (c *Commands) Execute(key CommandKey) error
Execute executes a command and returns an error if the command does not exist.
func (*Commands) List ¶
func (c *Commands) List() map[CommandKey]func()
type Conn ¶
type Conn struct { Pool *Pool Key interface{} Messages chan interface{} // contains filtered or unexported fields }
Conn is a websocket connection with a unique key, a pointer to a pool, and a channel for messages.
func NewConn ¶
NewConn upgrades an http connection to a websocket connection and returns a Conn or an error if the upgrade fails.
func (*Conn) Close ¶
Close closes the websocket connection and removes the Conn from the pool. It returns an error if the Conn is nil.
type Error ¶
type Error[T any] struct { Err error Status int Logger Logger // contains filtered or unexported fields }
Error is a generic error type for the Mnemo package.
func IsErrorType ¶
IsErrorType reflects Error[T] from an error.
func (Error[T]) IsStatusError ¶
IsStatusError returns true if the error has a status code.
func (Error[T]) WithLogLevel ¶
WithLogLevel sets the log level for the error.
func (Error[T]) WithStatus ¶
WithStatus sets the status code for the error.
type Mnemo ¶
type Mnemo struct {
// contains filtered or unexported fields
}
Mnemo is the main struct for the Mnemo package.
func (*Mnemo) DetachStore ¶
func (*Mnemo) UseStore ¶
UseStore returns a store from the Mnemo instance.
Returns an error if the store does not exist or does not belong to the Mnemo instance.
func (*Mnemo) WithServer ¶
WithServer creates a server for the Mnemo instance.
func (*Mnemo) WithStores ¶
WithStore adds one or more stores to the Mnemo instance.
type Opt ¶
type Opt[T any] func(t *T)
func WithPattern ¶
WithPattern sets the pattern for the server's http handler.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a collection of Conns
type ReducerFunc ¶
ReducerFunc is a function that takes a cache and returns a reduced version of it.
It is run against the raw cache on every change and must return json serializable data.
type Server ¶
Server is a websocket server that manages connections and messages.
func NewServer ¶
NewServer creates a new server.
The server's key must be unique. If a server with the same key already exists, an error is returned.
func (*Server) HandleSubscribe ¶
func (s *Server) HandleSubscribe(w http.ResponseWriter, r *http.Request)
HandleSubscribe upgrades the http connection to a websocket connection and adds the connection to the connection pool.
func (*Server) ListenAndServe ¶
func (s *Server) ListenAndServe()
ListenAndServe starts the server in a go routine
func (*Server) Publish ¶
func (s *Server) Publish(msg interface{})
Publish publishes a message to all connections in the connection pool.
func (*Server) SetOnNewConnection ¶
SetOnNewConnection sets a user defined call back function when a new connection is established.
Mnemo must be initialized with a server before calling this method.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a collection of caches.
func NewStore ¶
NewStore creates a new store or returns an error if a store with the same key already exists.