39 lines
942 B
Go
39 lines
942 B
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 (
|
|
TYPE_REDIS = "redis"
|
|
TYPE_MEMORY = "memory"
|
|
)
|
|
|
|
func GetCacheDatastore(config config.Cache) *CacheDatastore {
|
|
var datastore CacheDatastore
|
|
|
|
if config.Type == TYPE_REDIS {
|
|
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
|
|
}
|