github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/storage/outbox_memcache.go (about)

     1  package storage
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/gregor1"
     8  )
     9  
    10  type outboxMemCacheImpl struct {
    11  	sync.RWMutex
    12  
    13  	datMap map[string]*diskOutbox
    14  }
    15  
    16  func newoutboxMemCacheImpl() *outboxMemCacheImpl {
    17  	return &outboxMemCacheImpl{
    18  		datMap: make(map[string]*diskOutbox),
    19  	}
    20  }
    21  
    22  func (o *outboxMemCacheImpl) Get(uid gregor1.UID) *diskOutbox {
    23  	o.RLock()
    24  	defer o.RUnlock()
    25  	if obox, ok := o.datMap[uid.String()]; ok {
    26  		return obox
    27  	}
    28  	return nil
    29  }
    30  
    31  func (o *outboxMemCacheImpl) Put(uid gregor1.UID, obox *diskOutbox) {
    32  	o.Lock()
    33  	defer o.Unlock()
    34  	o.datMap[uid.String()] = obox
    35  }
    36  
    37  func (o *outboxMemCacheImpl) Clear(uid gregor1.UID) {
    38  	o.Lock()
    39  	defer o.Unlock()
    40  	delete(o.datMap, uid.String())
    41  }
    42  
    43  func (o *outboxMemCacheImpl) clearCache() {
    44  	o.Lock()
    45  	defer o.Unlock()
    46  	o.datMap = make(map[string]*diskOutbox)
    47  }
    48  
    49  func (o *outboxMemCacheImpl) OnLogout(mctx libkb.MetaContext) error {
    50  	o.clearCache()
    51  	return nil
    52  }
    53  
    54  func (o *outboxMemCacheImpl) OnDbNuke(mctx libkb.MetaContext) error {
    55  	o.clearCache()
    56  	return nil
    57  }
    58  
    59  var outboxMemCache = newoutboxMemCacheImpl()