github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/xorm/memory_store.go (about)

     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  
     5  package xorm
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/insionng/yougam/libraries/go-xorm/core"
    11  )
    12  
    13  var _ core.CacheStore = NewMemoryStore()
    14  
    15  // MemoryStore represents in-memory store
    16  type MemoryStore struct {
    17  	store map[interface{}]interface{}
    18  	mutex sync.RWMutex
    19  }
    20  
    21  func NewMemoryStore() *MemoryStore {
    22  	return &MemoryStore{store: make(map[interface{}]interface{})}
    23  }
    24  
    25  func (s *MemoryStore) Put(key string, value interface{}) error {
    26  	s.mutex.Lock()
    27  	defer s.mutex.Unlock()
    28  	s.store[key] = value
    29  	return nil
    30  }
    31  
    32  func (s *MemoryStore) Get(key string) (interface{}, error) {
    33  	s.mutex.RLock()
    34  	defer s.mutex.RUnlock()
    35  	if v, ok := s.store[key]; ok {
    36  		return v, nil
    37  	}
    38  
    39  	return nil, ErrNotExist
    40  }
    41  
    42  func (s *MemoryStore) Del(key string) error {
    43  	s.mutex.Lock()
    44  	defer s.mutex.Unlock()
    45  	delete(s.store, key)
    46  	return nil
    47  }