mirror of
https://github.com/go-gitea/gitea.git
synced 2024-08-18 10:33:24 +02:00
48 lines
916 B
Go
48 lines
916 B
Go
|
// 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.
|
||
|
|
||
|
package xorm
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"github.com/go-xorm/core"
|
||
|
)
|
||
|
|
||
|
var _ core.CacheStore = NewMemoryStore()
|
||
|
|
||
|
// memory store
|
||
|
type MemoryStore struct {
|
||
|
store map[interface{}]interface{}
|
||
|
mutex sync.RWMutex
|
||
|
}
|
||
|
|
||
|
func NewMemoryStore() *MemoryStore {
|
||
|
return &MemoryStore{store: make(map[interface{}]interface{})}
|
||
|
}
|
||
|
|
||
|
func (s *MemoryStore) Put(key string, value interface{}) error {
|
||
|
s.mutex.Lock()
|
||
|
defer s.mutex.Unlock()
|
||
|
s.store[key] = value
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func (s *MemoryStore) Del(key string) error {
|
||
|
s.mutex.Lock()
|
||
|
defer s.mutex.Unlock()
|
||
|
delete(s.store, key)
|
||
|
return nil
|
||
|
}
|