faststorage

package module
v0.0.2-0...-92756a7 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: MIT Imports: 7 Imported by: 0

README

Faststore - A Golang lib to abstract Redis as cache layer for GCP Datastore

This is a small library to abstract the Redis and Datastore combined usage for a fast highly available data layer.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asset

type Asset interface {
	DSAsset
	RedisAsset
}

Asset is a interface combining both Redis and Datastore operations. A struct must implement this interface to take advantage of the combined usage of both Redis and Datastore.

type AssetMissError

type AssetMissError struct {
	DSmissErr    DSmissErr
	CachemissErr CachemissErr
}

func (AssetMissError) Error

func (amr AssetMissError) Error() string

type CachemissErr

type CachemissErr struct {
	AssetKey   string
	AssetField string
}

func (CachemissErr) Error

func (e CachemissErr) Error() string

type Cfg

type Cfg struct {
	RedisAddrs      string
	RedisPassword   string
	Db              string
	MaxIdle         int
	MaxActive       int
	IdleTimeout     time.Duration
	MaxConnLifetime time.Duration
	Wait            bool
	TestOnBorrow    func(c redis.Conn, t time.Time) error
	Client          *datastore.Client
}

Config struct to use with the NewWithCfg wrapper.

type DSAsset

type DSAsset interface {
	GetDSKind() string
	GetDSNameKey() (string, bool)
	GetDSIDKey() (int64, bool)
	GetDSNamespace() string
}

DSAsset is the interface that a struct must implement to use the Datastore operations. GetDSKind should return the Datastore Kind for the object to be save/get from. GetDSNameKey and GetDSIDKey: should return a identifier to be used in the Datastore Key along with a bool flag indicating which one (name key or id key) should be used. - If both true, Id key will be used, if both false will use Datasore Incomplete Key. GetDSNamespace should return the Datastore Namespace to be used.

type DSmissErr

type DSmissErr struct {
	AssetKey       string
	AssetNamespace string
}

func (DSmissErr) Error

func (e DSmissErr) Error() string

type DataLayer

type DataLayer struct {
	Redis RedisDB
	DS    DatastoreDB
}

func NewWithCfg

func NewWithCfg(cfg Cfg) (*DataLayer, error)

func (*DataLayer) Get

func (dl *DataLayer) Get(ctx context.Context, asset Asset) error

Lookup in Redis db for the asset Id, in case of cahce miss, try to get it from Datastore. If asset is present in Datastore it will put it back in Redis and return the asset found in Datastore

func (*DataLayer) Put

func (dl *DataLayer) Put(ctx context.Context, asset Asset, dsParent *datastore.Key) (*datastore.Key, interface{}, error)

Put asset in Datastore and Redis. Uses the Asset interface to get info about asset (namespace, kind, TTL, etc)

type DatastoreDB

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

func (*DatastoreDB) Get

func (ds *DatastoreDB) Get(ctx context.Context, asset DSAsset) error

func (*DatastoreDB) Put

func (ds *DatastoreDB) Put(ctx context.Context, asset DSAsset, parent *datastore.Key) (*datastore.Key, error)

type RedisAsset

type RedisAsset interface {
	GetRedisKey() interface{}
	GetRedisField() string
	GetTTL() time.Duration
}

RedisAsset is the interface that a struct must implement to use the Redis operations. GetRedisKey should return the Key for the Redis Hash GetRedisField should return the Field for the Redis Hash GetTTL should return the duration of ttl for a key. Min value: 1 second

The Redis operations rely on encoding/json Marshal and Unmarshal methods of the RedisAsset implementer struct. The bytes slice on these methods are use to load and save the structs in Redis

type RedisDB

type RedisDB struct {
	GetConn func() redis.Conn
	// contains filtered or unexported fields
}

func (*RedisDB) Get

func (rd *RedisDB) Get(ctx context.Context, asset RedisAsset, args ...interface{}) error

func (*RedisDB) Put

func (rd *RedisDB) Put(ctx context.Context, asset RedisAsset) (interface{}, error)

type Sub

type Sub struct {
	SubVal string
}

type TestAsset

type TestAsset struct {
	Id   string
	Val1 string
	Val2 []string
	Val3 int
	Val4 *Sub
}

Asset impl for testing

func (TestAsset) GetDSIDKey

func (ta TestAsset) GetDSIDKey() (int64, bool)

func (TestAsset) GetDSKind

func (ta TestAsset) GetDSKind() string

func (TestAsset) GetDSNameKey

func (ta TestAsset) GetDSNameKey() (string, bool)

func (TestAsset) GetDSNamespace

func (ta TestAsset) GetDSNamespace() string

func (TestAsset) GetRedisField

func (ta TestAsset) GetRedisField() string

func (TestAsset) GetRedisKey

func (ta TestAsset) GetRedisKey() interface{}

func (TestAsset) GetTTL

func (ta TestAsset) GetTTL() time.Duration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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