refactor: cache interface

main
sundowndev 2021-05-24 19:05:36 +02:00
parent 2bf75aaa29
commit 6b5766144c
2 changed files with 14 additions and 7 deletions

View File

@ -24,7 +24,7 @@ type EC2Client interface {
type ec2Repository struct {
client ec2iface.EC2API
cache *cache.Cache
cache cache.Cache
}
func NewEC2Repository(session *session.Session) *ec2Repository {

View File

@ -1,20 +1,27 @@
package cache
import "sync"
import (
"sync"
)
type Cache struct {
type Cache interface {
Set(string, interface{}) bool
Get(string) interface{}
}
type Store struct {
mu *sync.RWMutex
store map[string]interface{}
}
func New() *Cache {
return &Cache{
func New() Cache {
return &Store{
mu: &sync.RWMutex{},
store: map[string]interface{}{},
}
}
func (c *Cache) Set(key string, value interface{}) (overridden bool) {
func (c *Store) Set(key string, value interface{}) (overridden bool) {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.store[key]; ok {
@ -24,7 +31,7 @@ func (c *Cache) Set(key string, value interface{}) (overridden bool) {
return
}
func (c *Cache) Get(key string) interface{} {
func (c *Store) Get(key string) interface{} {
c.mu.RLock()
defer c.mu.RUnlock()
return c.store[key]