git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/graphql/cache.go (about)

     1  package graphql
     2  
     3  import "context"
     4  
     5  // Cache is a shared store for APQ and query AST caching
     6  type Cache interface {
     7  	// Get looks up a key's value from the cache.
     8  	Get(ctx context.Context, key string) (value interface{}, ok bool)
     9  
    10  	// Add adds a value to the cache.
    11  	Add(ctx context.Context, key string, value interface{})
    12  }
    13  
    14  // MapCache is the simplest implementation of a cache, because it can not evict it should only be used in tests
    15  type MapCache map[string]interface{}
    16  
    17  // Get looks up a key's value from the cache.
    18  func (m MapCache) Get(ctx context.Context, key string) (value interface{}, ok bool) {
    19  	v, ok := m[key]
    20  	return v, ok
    21  }
    22  
    23  // Add adds a value to the cache.
    24  func (m MapCache) Add(ctx context.Context, key string, value interface{}) { m[key] = value }
    25  
    26  type NoCache struct{}
    27  
    28  func (n NoCache) Get(ctx context.Context, key string) (value interface{}, ok bool) { return nil, false }
    29  func (n NoCache) Add(ctx context.Context, key string, value interface{})           {}