...

Source file src/github.com/launchdarkly/ccache/secondarycache.go

Documentation: github.com/launchdarkly/ccache

     1  package ccache
     2  
     3  import "time"
     4  
     5  type SecondaryCache struct {
     6  	bucket *bucket
     7  	pCache *LayeredCache
     8  }
     9  
    10  // Get the secondary key.
    11  // The semantics are the same as for LayeredCache.Get
    12  func (s *SecondaryCache) Get(secondary string) *Item {
    13  	return s.bucket.get(secondary)
    14  }
    15  
    16  // Set the secondary key to a value.
    17  // The semantics are the same as for LayeredCache.Set
    18  func (s *SecondaryCache) Set(secondary string, value interface{}, duration time.Duration) *Item {
    19  	item, existing := s.bucket.set(secondary, value, duration, false)
    20  	if existing != nil {
    21  		s.pCache.deletables <- existing
    22  	}
    23  	s.pCache.promote(item)
    24  	return item
    25  }
    26  
    27  // Fetch or set a secondary key.
    28  // The semantics are the same as for LayeredCache.Fetch
    29  func (s *SecondaryCache) Fetch(secondary string, duration time.Duration, fetch func() (interface{}, error)) (*Item, error) {
    30  	item := s.Get(secondary)
    31  	if item != nil {
    32  		return item, nil
    33  	}
    34  	value, err := fetch()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return s.Set(secondary, value, duration), nil
    39  }
    40  
    41  // Delete a secondary key.
    42  // The semantics are the same as for LayeredCache.Delete
    43  func (s *SecondaryCache) Delete(secondary string) bool {
    44  	item := s.bucket.delete(secondary)
    45  	if item != nil {
    46  		s.pCache.deletables <- item
    47  		return true
    48  	}
    49  	return false
    50  }
    51  
    52  // Replace a secondary key.
    53  // The semantics are the same as for LayeredCache.Replace
    54  func (s *SecondaryCache) Replace(secondary string, value interface{}) bool {
    55  	item := s.Get(secondary)
    56  	if item == nil {
    57  		return false
    58  	}
    59  	s.Set(secondary, value, item.TTL())
    60  	return true
    61  }
    62  
    63  // Track a secondary key.
    64  // The semantics are the same as for LayeredCache.TrackingGet
    65  func (c *SecondaryCache) TrackingGet(secondary string) TrackedItem {
    66  	item := c.Get(secondary)
    67  	if item == nil {
    68  		return NilTracked
    69  	}
    70  	item.track()
    71  	return item
    72  }
    73  

View as plain text