db

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SCacheChannelBreak -> Channel to subscribe for cache break notifications
	SCacheChannelBreak = "gme.sh-scache:break"

	// SCacheChannelUpdate -> Channel to subscribe for cache update notifications
	SCacheChannelUpdate = "gme.sh-scache:update"
)

Variables

This section is empty.

Functions

func NewHealthCheck

func NewHealthCheck(pe PersistentDatabase, st StatsDatabase, ps PubSub) (h *health.Health, err error)

Types

type DBCache

type DBCache interface {
	UpdateCache(u *short.ShortURL) (err error)
	BreakCache(id *short.ShortID) (err error)
	Get(key string) (interface{}, bool)
	GetShortURL(id *short.ShortID) *short.ShortURL
}

DBCache is an interface which may be implemented by a StatsDatabase to provide cache functions. └ LocalCache

└ SharedCache

type ExpirationCheck

type ExpirationCheck struct {
	Interval            time.Duration
	LastExpirationCheck time.Time
	DB                  PersistentDatabase
	DryRun              bool
}

func NewExpirationCheck

func NewExpirationCheck(interval time.Duration, dryRun bool, database PersistentDatabase) *ExpirationCheck

func (*ExpirationCheck) Check

func (e *ExpirationCheck) Check()

func (*ExpirationCheck) Start

func (e *ExpirationCheck) Start(cancel chan bool)

type LastExpirationCheckMeta

type LastExpirationCheckMeta struct {
	LastCheck time.Time
}

type LocalCache

type LocalCache struct {
	Cache *cache.Cache
}

LocalCache is a purely local cache and only while the backend is running. If the application is terminated, the cache is also flushed.

func NewLocalCache

func NewLocalCache() *LocalCache

NewLocalCache creates and returns a new LocalCache with a *hodl* time of 5 minutes and a clear time of 10 minutes.

func (*LocalCache) BreakCache

func (l *LocalCache) BreakCache(id *short.ShortID) (_ error)

BreakCache removes the ShortURL object from the cache that matches the ShortID. No further check is made whether it was already in the cache or not. Since no error can occur here, nil is always returned.

func (*LocalCache) Get

func (l *LocalCache) Get(key string) (interface{}, bool)

Get returns an interface from the cache if it exists. Otherwise the interface is nil and the return value is false.

func (*LocalCache) GetShortURL

func (l *LocalCache) GetShortURL(id *short.ShortID) *short.ShortURL

func (*LocalCache) UpdateCache

func (l *LocalCache) UpdateCache(u *short.ShortURL) (_ error)

UpdateCache adds a new ShortURL object to the cache. Since no error can occur here, nil is always returned.

type PersistentDatabase

type PersistentDatabase interface {
	ServiceName() string
	HealthCheck(ctx context.Context) error

	// PersistentDatabase Functions
	SaveShortenedURL(url *short.ShortURL) (err error)
	DeleteShortenedURL(id *short.ShortID) (err error)
	FindShortenedURL(id *short.ShortID) (res *short.ShortURL, err error)
	ShortURLAvailable(id *short.ShortID) (available bool)
	FindExpiredURLs() ([]*short.ShortURL, error)
	GetLastExpirationCheck() *LastExpirationCheckMeta
	UpdateLastExpirationCheck(t time.Time)
}

PersistentDatabase functions

func MustPersistent

func MustPersistent(db PersistentDatabase, err error) PersistentDatabase

Must -> Don't use database, if some error occurred

func NewBBoltDatabase

func NewBBoltDatabase(cfg *config.BBoltConfig, cache DBCache) (bbdb PersistentDatabase, err error)

NewBBoltDatabase -> Create new BBoltDatabase

func NewMongoDatabase

func NewMongoDatabase(cfg *config.MongoConfig, cache DBCache) (db PersistentDatabase, err error)

NewMongoDatabase -> Creates a new implementation of PersistentDatabase (mongodb), connects, and returns it

func NewRedisDatabase

func NewRedisDatabase(cfg *config.RedisConfig) (PersistentDatabase, error)

NewRedisDatabase -> Use Redis as backend

type PubSub

type PubSub interface {
	ServiceName() string
	HealthCheck(ctx context.Context) error

	Heartbeat() (err error)
	Publish(channel, msg string) (err error)
	Subscribe(c func(channel, payload string), channels ...string) (err error)
	Close() (err error)
}

func MustPubSub

func MustPubSub(db PubSub, err error) PubSub

func NewRedisPubSub

func NewRedisPubSub(cfg *config.RedisConfig) (PubSub, error)

type SharedCache

type SharedCache struct {
	NodeID string
	// contains filtered or unexported fields
}

SharedCache only makes sense if you want to run multiple backend shards / servers at the same time. If a request is then cached on one server, this cache is passed on to all other servers via PubSub, whereby the requests to the database are brought to a minimum.

func NewSharedCache

func NewSharedCache(pubSub PubSub) *SharedCache

NewSharedCache creates a new SharedCache object and returns it

func (*SharedCache) BreakCache

func (s *SharedCache) BreakCache(id *short.ShortID) (err error)

BreakCache removes the ShortURL object from the cache that matches the ShortID. No further check is made whether it was already in the cache or not. returns an error if there was an error publishing the break notification

func (*SharedCache) Get

func (s *SharedCache) Get(key string) (interface{}, bool)

Get returns an interface from the cache if it exists. Otherwise the interface is nil and the return value is false. Alias for LocalCache.Get()

func (*SharedCache) GetShortURL

func (s *SharedCache) GetShortURL(id *short.ShortID) *short.ShortURL

func (*SharedCache) Subscribe

func (s *SharedCache) Subscribe() (err error)

Subscribe subscribes to SCacheChannelBreak + SCacheChannelUpdate channels and processes their messages

func (*SharedCache) UpdateCache

func (s *SharedCache) UpdateCache(u *short.ShortURL) (err error)

UpdateCache adds a new ShortURL object to the cache.

type StatsDatabase

type StatsDatabase interface {
	ServiceName() string
	HealthCheck(ctx context.Context) error

	// StatsDatabase Functions
	FindStats(id *short.ShortID) (stats *short.Stats, err error)
	AddStats(id *short.ShortID) (err error)
	DeleteStats(id *short.ShortID) (err error)
}

StatsDatabase functions

func MustStats

func MustStats(db StatsDatabase, err error) StatsDatabase

func NewRedisStats

func NewRedisStats(cfg *config.RedisConfig) (StatsDatabase, error)

Jump to

Keyboard shortcuts

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