mgokv

package
v0.0.0-...-9d78121 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: LGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package mgokv defines cached MongoDB-backed global persistent storage for key-value pairs.

It is designed to be used when there is a small set of attributes that change infrequently. It shouldn't be used when there's an unbounded set of keys, as key entries are not deleted.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errgo.New("persistent data entry not found")

ErrNotFound is returned as the cause of the error when an entry is not found.

Functions

This section is empty.

Types

type Session

type Session struct {
	*Store
	// contains filtered or unexported fields
}

Session associates a Store instance with a mongo session.

func (*Session) Get

func (s *Session) Get(key string, v interface{}) error

Get gets the value associated with the given key into the value pointed to by v, which should be a pointer to the same struct type used to put the value originally.

If the value is not found, it returns ErrNotFound.

func (*Session) Put

func (s *Session) Put(key string, val interface{}) error

Put stores the given value for the given key. The value must be a struct type that is marshalable as BSON (see http://gopkg.in/mgo.v2/bson).

func (*Session) PutInitial

func (s *Session) PutInitial(key string, val interface{}) (bool, error)

PutInitial puts an initial value for the given key. It does nothing if there is already a value stored for the key. It reports whether the value was actually set.

func (*Session) Update

func (s *Session) Update(key string, update interface{}) error

Update updates the value using the MongoDB update operation specified in update. The value is stored in the "value" field in the document.

For example, if a value of type struct { N int } is associated with a key, then:

s.Update(key, bson.M{"$inc": bson.M{"value.n": 1}})

will atomically increment the N value.

If there is no value associated with the key, Update returns ErrNotFound.

type Store

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

Store represents a cached set of key-value pairs.

func NewStore

func NewStore(cacheLifetime time.Duration, c *mgo.Collection) *Store

NewStore returns a Store that will cache items for at most the given time in the given collection. The session in the collection will not be used - the session passed to Store.Session will be used instead.

func (*Store) Refresh

func (s *Store) Refresh()

Refresh forgets all cached items.

func (*Store) Session

func (s *Store) Session(session *mgo.Session) *Session

Session returns a store session that uses the given session for storage. Each store entry is stored in a document in the collection.

Jump to

Keyboard shortcuts

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