Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
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.

52 lines
1.0KB

  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. "xorm.io/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. }