vegvisir/pkg/cache/memory_datastore.go

43 lines
962 B
Go
Raw Permalink Normal View History

// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
import (
"errors"
)
func NewMemoryDatastore() *MemoryDatastore {
return &MemoryDatastore{
cache: make(map[string]interface{}),
}
}
type MemoryDatastore struct {
cache map[string]interface{}
}
func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error {
ds.cache[key] = data
return nil
}
func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) {
if data, ok := ds.cache[key]; ok {
return data, nil
}
return nil, errors.New("Key not found" + key)
}
func (ds *MemoryDatastore) IsConnected() bool {
return true
}