inmemorydb

package
v1.15.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2019 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package inmemorydb provides an implementation of github.com/alexandre-normand/slackscot/store's StringStorer interface as an in-memory data store relying on a wrapping StringStorer for actual persistence.

The main use-case for the inmemorydb is to shield the real StringStorer implementation from receiving too many calls as plugins may very well query their StringStorer on every message to evaluate for a match or answer. Of course, using this also allows the slackscot instance to offer lower latency at the expense of increased memory usage.

Most plugin databases are small and this is therefore a good idea to use inmemorydb but if your instance uses plugins storing a large number of rows, consider using a different storage interface than the slackscot StringStorer or skipping the usage of the inmemorydb.

Requirements for the Google Cloud Datastore integration:

Example code:

import (
	"github.com/alexandre-normand/slackscot/store/datastoredb"
	"github.com/alexandre-normand/slackscot/store/inmemorydb"
	"google.golang.org/api/option"
)

func main() {
	// Create your persistent storer first
	persistentStorer, err := datastoredb.New(plugins.KarmaPluginName, "youppi", option.WithCredentialsFile(*gcloudCredentialsFile))
	if err != nil {
		log.Fatalf("Opening [%s] db failed: %s", plugins.KarmaPluginName, err.Error())
	}
	defer persistentStorer.Close()

	// Create the inmemorydb
	karmaStorer, err := inmemorydb.New(persistenStorer)
	if err != nil {
		log.Fatalf("Opening creating in-memory db wrapper: %s", err.Error())
	}

	// Do something with the database
	karma := plugins.NewKarma(karmaStorer)}

	// Run your instance
	...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InMemoryDB

type InMemoryDB struct {
	// contains filtered or unexported fields
}

InMemoryDB implements the slackscot StringStorer interface and keeps a copy of everything in memory while writing through puts and deletes to the wrapped (persistent) StringStorer

func New

func New(storer store.StringStorer) (imdb *InMemoryDB, err error)

New returns a new instance of InMemoryDB wrapping the persistent StringStorer. Note that instantiation might have some latency induced by the initial scan to load the current database content from the persistentStorer in memory

func (*InMemoryDB) Close

func (imdb *InMemoryDB) Close() (err error)

Close closes the underlying storer

func (*InMemoryDB) DeleteString

func (imdb *InMemoryDB) DeleteString(key string) (err error)

DeleteString deletes the entry for the given key. This is propagated to the persistent storage first and then deleted from memory

func (*InMemoryDB) GetString

func (imdb *InMemoryDB) GetString(key string) (value string, err error)

GetString returns the value associated to a given key. If the value is not found or an error occurred, the zero-value string is returned along with the error

func (*InMemoryDB) PutString

func (imdb *InMemoryDB) PutString(key string, value string) (err error)

PutString stores the key/value to the database. The key/value is persisted to persistent storage and also kept in memory

func (*InMemoryDB) Scan

func (imdb *InMemoryDB) Scan() (entries map[string]string, err error)

Scan returns all key/values from the database. This one returns a copy of the in-memory copy without querying the persistent storer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL