github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/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(_ 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(_ context.Context, key string, value interface{}) { m[key] = value }
    25  
    26  type NoCache struct{}
    27  
    28  func (n NoCache) Get(_ context.Context, _ string) (value interface{}, ok bool) { return nil, false }
    29  func (n NoCache) Add(_ context.Context, _ string, _ interface{})               {}