This commit is contained in:
44
pkg/storage/mem-storage.go
Normal file
44
pkg/storage/mem-storage.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type MemoryStorage struct {
|
||||
cache map[string][]byte
|
||||
}
|
||||
|
||||
func NewMemoryStorage(ids []string) (Storage, error) {
|
||||
s := &MemoryStorage{
|
||||
cache: make(map[string][]byte),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (ms *MemoryStorage) Get(key string) (io.Reader, error) {
|
||||
content, ok := ms.cache[key]
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return bytes.NewReader(content), nil
|
||||
}
|
||||
|
||||
func (ms *MemoryStorage) Put(key string, data []byte) error {
|
||||
ms.cache[key] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MemoryStorage) Delete(key string) error {
|
||||
delete(ms.cache, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MemoryStorage) List(prefix string) ([]string, error) {
|
||||
res := make([]string, 0, len(ms.cache))
|
||||
for file, _ := range ms.cache {
|
||||
res = append(res, file)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user