vegvisir/pkg/cache/cache.go
Piotr Biernat b0e492c163
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Fixes after code linters suggestions
2021-08-03 00:13:14 +02:00

49 lines
1.1 KiB
Go

// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
import (
"log"
"vegvisir/pkg/config"
)
const (
typeRedis = "redis"
typeMemory = "memory"
)
// Datastore interface
type Datastore interface {
SetKey(string, interface{}, int) error
GetKey(string) (interface{}, error)
IsConnected() bool
}
// GetCacheDatastore function
func GetCacheDatastore(config config.Cache) *Datastore {
var datastore Datastore
if config.Type == typeRedis {
datastore = NewRedisDatastore(config.Host, config.Port)
} else {
datastore = NewMemoryDatastore()
}
// fail-safe switch to memory datasource
if !datastore.IsConnected() {
log.Println("Cache server is not responding, switching to memory cache.")
datastore = NewMemoryDatastore()
}
return &datastore
}