45 lines
789 B
Go
45 lines
789 B
Go
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
|
|
}
|