cache_memory_store.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/go-xorm/core"
  8. )
  9. var _ core.CacheStore = NewMemoryStore()
  10. // MemoryStore represents in-memory store
  11. type MemoryStore struct {
  12. store map[interface{}]interface{}
  13. mutex sync.RWMutex
  14. }
  15. // NewMemoryStore creates a new store in memory
  16. func NewMemoryStore() *MemoryStore {
  17. return &MemoryStore{store: make(map[interface{}]interface{})}
  18. }
  19. // Put puts object into store
  20. func (s *MemoryStore) Put(key string, value interface{}) error {
  21. s.mutex.Lock()
  22. defer s.mutex.Unlock()
  23. s.store[key] = value
  24. return nil
  25. }
  26. // Get gets object from store
  27. func (s *MemoryStore) Get(key string) (interface{}, error) {
  28. s.mutex.RLock()
  29. defer s.mutex.RUnlock()
  30. if v, ok := s.store[key]; ok {
  31. return v, nil
  32. }
  33. return nil, ErrNotExist
  34. }
  35. // Del deletes object
  36. func (s *MemoryStore) Del(key string) error {
  37. s.mutex.Lock()
  38. defer s.mutex.Unlock()
  39. delete(s.store, key)
  40. return nil
  41. }