memroy_store.go 917 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "sync"
  7. "github.com/xormplus/core"
  8. )
  9. var _ core.CacheStore = NewMemoryStore()
  10. // memory store
  11. type MemoryStore struct {
  12. store map[interface{}]interface{}
  13. mutex sync.RWMutex
  14. }
  15. func NewMemoryStore() *MemoryStore {
  16. return &MemoryStore{store: make(map[interface{}]interface{})}
  17. }
  18. func (s *MemoryStore) Put(key string, value interface{}) error {
  19. s.mutex.Lock()
  20. defer s.mutex.Unlock()
  21. s.store[key] = value
  22. return nil
  23. }
  24. func (s *MemoryStore) Get(key string) (interface{}, error) {
  25. s.mutex.RLock()
  26. defer s.mutex.RUnlock()
  27. if v, ok := s.store[key]; ok {
  28. return v, nil
  29. }
  30. return nil, ErrNotExist
  31. }
  32. func (s *MemoryStore) Del(key string) error {
  33. s.mutex.Lock()
  34. defer s.mutex.Unlock()
  35. delete(s.store, key)
  36. return nil
  37. }