github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/signer/storage/storage.go (about)

     1  //
     2  
     3  package storage
     4  
     5  import (
     6  	"fmt"
     7  )
     8  
     9  type Storage interface {
    10  	// Put stores a value by key. 0-length keys results in no-op
    11  	Put(key, value string)
    12  	// Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length
    13  	Get(key string) string
    14  }
    15  
    16  // EphemeralStorage is an in-memory storage that does
    17  // not persist values to disk. Mainly used for testing
    18  type EphemeralStorage struct {
    19  	data      map[string]string
    20  	namespace string
    21  }
    22  
    23  func (s *EphemeralStorage) Put(key, value string) {
    24  	if len(key) == 0 {
    25  		return
    26  	}
    27  	fmt.Printf("storage: put %v -> %v\n", key, value)
    28  	s.data[key] = value
    29  }
    30  
    31  func (s *EphemeralStorage) Get(key string) string {
    32  	if len(key) == 0 {
    33  		return ""
    34  	}
    35  	fmt.Printf("storage: get %v\n", key)
    36  	if v, exist := s.data[key]; exist {
    37  		return v
    38  	}
    39  	return ""
    40  }
    41  
    42  func NewEphemeralStorage() Storage {
    43  	s := &EphemeralStorage{
    44  		data: make(map[string]string),
    45  	}
    46  	return s
    47  }