mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-09-19 18:35:25 -04:00
40 lines
697 B
Go
40 lines
697 B
Go
package cache
|
|
|
|
import "sync"
|
|
|
|
// InMemoryCache is a simple in-memory thumbnail cache using sync.RWMutex.
|
|
type InMemoryCache struct {
|
|
mu sync.RWMutex
|
|
data map[string][]byte
|
|
}
|
|
|
|
func NewInMemoryCache() *InMemoryCache {
|
|
return &InMemoryCache{
|
|
data: make(map[string][]byte),
|
|
}
|
|
}
|
|
|
|
func (c *InMemoryCache) Get(key string) ([]byte, error) {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
|
|
data, ok := c.data[key]
|
|
if !ok {
|
|
return nil, ErrCacheMiss
|
|
}
|
|
|
|
result := make([]byte, len(data))
|
|
copy(result, data)
|
|
return result, nil
|
|
}
|
|
|
|
func (c *InMemoryCache) Put(key string, data []byte) error {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
cp := make([]byte, len(data))
|
|
copy(cp, data)
|
|
c.data[key] = cp
|
|
return nil
|
|
}
|