2016-11-03 22:16:01 +00:00
|
|
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
package caches
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
var _ CacheStore = NewMemoryStore()
|
2016-11-03 22:16:01 +00:00
|
|
|
|
2016-11-11 16:40:21 +00:00
|
|
|
// MemoryStore represents in-memory store
|
2016-11-03 22:16:01 +00:00
|
|
|
type MemoryStore struct {
|
|
|
|
store map[interface{}]interface{}
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
// NewMemoryStore creates a new store in memory
|
2016-11-03 22:16:01 +00:00
|
|
|
func NewMemoryStore() *MemoryStore {
|
|
|
|
return &MemoryStore{store: make(map[interface{}]interface{})}
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
// Put puts object into store
|
2016-11-03 22:16:01 +00:00
|
|
|
func (s *MemoryStore) Put(key string, value interface{}) error {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
s.store[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
// Get gets object from store
|
2016-11-03 22:16:01 +00:00
|
|
|
func (s *MemoryStore) Get(key string) (interface{}, error) {
|
|
|
|
s.mutex.RLock()
|
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
if v, ok := s.store[key]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotExist
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:20:28 +00:00
|
|
|
// Del deletes object
|
2016-11-03 22:16:01 +00:00
|
|
|
func (s *MemoryStore) Del(key string) error {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
delete(s.store, key)
|
|
|
|
return nil
|
|
|
|
}
|