You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory_store.go 1.0KB

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