github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/storage/readoutbox_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 readOutboxMemCacheImpl struct {
    11  	sync.RWMutex
    12  
    13  	datMap map[string]*diskReadOutbox
    14  }
    15  
    16  func newReadOutboxMemCacheImpl() *readOutboxMemCacheImpl {
    17  	return &readOutboxMemCacheImpl{
    18  		datMap: make(map[string]*diskReadOutbox),
    19  	}
    20  }
    21  
    22  func (o *readOutboxMemCacheImpl) Get(uid gregor1.UID) *diskReadOutbox {
    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 *readOutboxMemCacheImpl) Put(uid gregor1.UID, obox *diskReadOutbox) {
    32  	o.Lock()
    33  	defer o.Unlock()
    34  	o.datMap[uid.String()] = obox
    35  }
    36  
    37  func (o *readOutboxMemCacheImpl) Clear(uid gregor1.UID) {
    38  	o.Lock()
    39  	defer o.Unlock()
    40  	delete(o.datMap, uid.String())
    41  }
    42  
    43  func (o *readOutboxMemCacheImpl) clearCache() {
    44  	o.Lock()
    45  	defer o.Unlock()
    46  	o.datMap = make(map[string]*diskReadOutbox)
    47  }
    48  
    49  func (o *readOutboxMemCacheImpl) OnLogout(mctx libkb.MetaContext) error {
    50  	o.clearCache()
    51  	return nil
    52  }
    53  
    54  func (o *readOutboxMemCacheImpl) OnDbNuke(mctx libkb.MetaContext) error {
    55  	o.clearCache()
    56  	return nil
    57  }
    58  
    59  var readOutboxMemCache = newReadOutboxMemCacheImpl()