github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbfsmd/key_bundle_cache.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package kbfsmd
     6  
     7  import (
     8  	"errors"
     9  
    10  	"github.com/keybase/client/go/kbfs/cache"
    11  )
    12  
    13  // KeyBundleCache is an interface to a key bundle cache for use with v3 metadata.
    14  type KeyBundleCache interface {
    15  	// GetTLFReaderKeyBundle returns the TLFReaderKeyBundleV3 for
    16  	// the given TLFReaderKeyBundleID, or nil if there is none.
    17  	GetTLFReaderKeyBundle(TLFReaderKeyBundleID) (*TLFReaderKeyBundleV3, error)
    18  	// GetTLFWriterKeyBundle returns the TLFWriterKeyBundleV3 for
    19  	// the given TLFWriterKeyBundleID, or nil if there is none.
    20  	GetTLFWriterKeyBundle(TLFWriterKeyBundleID) (*TLFWriterKeyBundleV3, error)
    21  	// PutTLFReaderKeyBundle stores the given TLFReaderKeyBundleV3.
    22  	PutTLFReaderKeyBundle(TLFReaderKeyBundleID, TLFReaderKeyBundleV3)
    23  	// PutTLFWriterKeyBundle stores the given TLFWriterKeyBundleV3.
    24  	PutTLFWriterKeyBundle(TLFWriterKeyBundleID, TLFWriterKeyBundleV3)
    25  }
    26  
    27  // KeyBundleCacheStandard is an LRU-based implementation of the KeyBundleCache interface.
    28  type KeyBundleCacheStandard struct {
    29  	cache cache.Cache
    30  }
    31  
    32  var _ KeyBundleCache = (*KeyBundleCacheStandard)(nil)
    33  
    34  // NewKeyBundleCacheLRU constructs a new KeyBundleCacheStandard with LRU
    35  // eviction strategy. The capacity of the cache is set to capacityBytes bytes.
    36  func NewKeyBundleCacheLRU(capacityBytes int) *KeyBundleCacheStandard {
    37  	return &KeyBundleCacheStandard{cache.NewLRUEvictedCache(capacityBytes)}
    38  }
    39  
    40  // NewKeyBundleCacheRandom constructs a new KeyBundleCacheStandard with random
    41  // eviction strategy. The capacity of the cache is set to capacityBytes bytes.
    42  func NewKeyBundleCacheRandom(capacityBytes int) *KeyBundleCacheStandard {
    43  	return &KeyBundleCacheStandard{cache.NewRandomEvictedCache(capacityBytes)}
    44  }
    45  
    46  // GetTLFReaderKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
    47  func (k *KeyBundleCacheStandard) GetTLFReaderKeyBundle(
    48  	bundleID TLFReaderKeyBundleID) (*TLFReaderKeyBundleV3, error) {
    49  	if entry, ok := k.cache.Get(bundleID); ok {
    50  		if rkb, ok := entry.(TLFReaderKeyBundleV3); ok {
    51  			return &rkb, nil
    52  		}
    53  		// Shouldn't be possible.
    54  		return nil, errors.New("Invalid key bundle type")
    55  	}
    56  	return nil, nil
    57  }
    58  
    59  // GetTLFWriterKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
    60  func (k *KeyBundleCacheStandard) GetTLFWriterKeyBundle(
    61  	bundleID TLFWriterKeyBundleID) (*TLFWriterKeyBundleV3, error) {
    62  	if entry, ok := k.cache.Get(bundleID); ok {
    63  		if wkb, ok := entry.(TLFWriterKeyBundleV3); ok {
    64  			return &wkb, nil
    65  		}
    66  		// Shouldn't be possible.
    67  		return nil, errors.New("Invalid key bundle type")
    68  	}
    69  	return nil, nil
    70  }
    71  
    72  // PutTLFReaderKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
    73  func (k *KeyBundleCacheStandard) PutTLFReaderKeyBundle(
    74  	bundleID TLFReaderKeyBundleID, rkb TLFReaderKeyBundleV3) {
    75  	k.cache.Add(bundleID, rkb)
    76  }
    77  
    78  // PutTLFWriterKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
    79  func (k *KeyBundleCacheStandard) PutTLFWriterKeyBundle(
    80  	bundleID TLFWriterKeyBundleID, wkb TLFWriterKeyBundleV3) {
    81  	k.cache.Add(bundleID, wkb)
    82  }