64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
// ___ ____ ___ ___
|
|
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
// \ / | _ | _ | \ / || __ | || ||\\
|
|
// \/ |___ |___ | \/ || ____| || || \\
|
|
//
|
|
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
|
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
|
|
|
package cache
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
)
|
|
|
|
type RouteCache struct {
|
|
SourceUrl string
|
|
TargetUrl string
|
|
}
|
|
|
|
type RouteCacheManager struct {
|
|
datastore CacheDatastore
|
|
ttl int
|
|
}
|
|
|
|
func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager {
|
|
return RouteCacheManager{
|
|
datastore: datastore,
|
|
ttl: ttl,
|
|
}
|
|
}
|
|
|
|
func (rm *RouteCacheManager) Save(name string, r RouteCache) {
|
|
data, err := json.Marshal(r)
|
|
if err != nil {
|
|
log.Println("JSON:", err) // FIXME
|
|
}
|
|
|
|
err = rm.datastore.SetKey("route_"+name, data, rm.ttl)
|
|
if err != nil {
|
|
log.Println("REDIS:", err, name) // FIXME
|
|
}
|
|
}
|
|
|
|
func (rm *RouteCacheManager) Load(name string) (bool, RouteCache) {
|
|
name = "route_" + name
|
|
|
|
data, err := rm.datastore.GetKey(name)
|
|
if err != nil {
|
|
log.Println("REDIS:", err, name) // FIXME
|
|
|
|
return false, RouteCache{}
|
|
}
|
|
|
|
rc := RouteCache{}
|
|
err = json.Unmarshal([]byte(data.(string)), &rc)
|
|
if err != nil {
|
|
log.Println("JSON:", err) // FIXME
|
|
}
|
|
|
|
return true, rc
|
|
}
|