vegvisir/pkg/cache/cache.go

51 lines
1.2 KiB
Go
Raw Permalink Normal View History

// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
2021-11-15 21:41:38 +01:00
// Package cache whole cache functionality
package cache
import (
2021-11-15 16:33:31 +01:00
"git.pbiernat.dev/golang/vegvisir/pkg/config"
"log"
)
const (
2021-08-03 00:13:14 +02:00
typeRedis = "redis"
typeMemory = "memory"
)
2021-08-03 00:13:14 +02:00
// Datastore interface
type Datastore interface {
SetKey(string, interface{}, int) error
2021-08-03 00:13:14 +02:00
GetKey(string) (interface{}, error)
IsConnected() bool
}
// GetDatastore function
func GetDatastore(cfg config.Cache) *Datastore {
2021-08-03 00:13:14 +02:00
var datastore Datastore
log.Printf("Cache datastore type: %s", cfg.Type)
if cfg.Type == typeRedis {
datastore = NewRedisDatastore(cfg.Host, cfg.Password, cfg.Database, cfg.Port)
} else {
datastore = NewMemoryDatastore()
}
2021-07-25 20:13:03 +02:00
// fail-safe switch to memory datasource
if !datastore.IsConnected() {
log.Println("Cache server is not responding, switching to memory cache.")
datastore = NewMemoryDatastore()
}
return &datastore
}