github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/storage/inbox_memcache.go (about)

     1  package storage
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/keybase/client/go/chat/types"
     7  	"github.com/keybase/client/go/libkb"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  	"github.com/keybase/client/go/protocol/gregor1"
    10  )
    11  
    12  type inboxMemCacheImpl struct {
    13  	sync.Mutex
    14  
    15  	versMap  map[string]*inboxDiskVersions
    16  	indexMap map[string]*inboxDiskIndex
    17  	convMap  map[string]types.RemoteConversation
    18  }
    19  
    20  func newInboxMemCacheImpl() *inboxMemCacheImpl {
    21  	return &inboxMemCacheImpl{
    22  		versMap:  make(map[string]*inboxDiskVersions),
    23  		indexMap: make(map[string]*inboxDiskIndex),
    24  		convMap:  make(map[string]types.RemoteConversation),
    25  	}
    26  }
    27  
    28  func (i *inboxMemCacheImpl) GetVersions(uid gregor1.UID) *inboxDiskVersions {
    29  	i.Lock()
    30  	defer i.Unlock()
    31  	if ibox, ok := i.versMap[uid.String()]; ok {
    32  		return ibox
    33  	}
    34  	return nil
    35  }
    36  
    37  func (i *inboxMemCacheImpl) PutVersions(uid gregor1.UID, ibox *inboxDiskVersions) {
    38  	i.Lock()
    39  	defer i.Unlock()
    40  	i.versMap[uid.String()] = ibox
    41  }
    42  
    43  func (i *inboxMemCacheImpl) GetIndex(uid gregor1.UID) *inboxDiskIndex {
    44  	i.Lock()
    45  	defer i.Unlock()
    46  	if ibox, ok := i.indexMap[uid.String()]; ok {
    47  		ret := ibox.DeepCopy()
    48  		return &ret
    49  	}
    50  	return nil
    51  }
    52  
    53  func (i *inboxMemCacheImpl) PutIndex(uid gregor1.UID, ibox *inboxDiskIndex) {
    54  	i.Lock()
    55  	defer i.Unlock()
    56  	i.indexMap[uid.String()] = ibox
    57  }
    58  
    59  func (i *inboxMemCacheImpl) convKey(uid gregor1.UID, convID chat1.ConversationID) string {
    60  	return uid.String() + convID.String()
    61  }
    62  
    63  func (i *inboxMemCacheImpl) GetConv(uid gregor1.UID, convID chat1.ConversationID) *types.RemoteConversation {
    64  	i.Lock()
    65  	defer i.Unlock()
    66  	if conv, ok := i.convMap[i.convKey(uid, convID)]; ok {
    67  		ret := conv.DeepCopy()
    68  		return &ret
    69  	}
    70  	return nil
    71  }
    72  
    73  func (i *inboxMemCacheImpl) PutConv(uid gregor1.UID, conv types.RemoteConversation) {
    74  	i.Lock()
    75  	defer i.Unlock()
    76  	i.convMap[i.convKey(uid, conv.GetConvID())] = conv
    77  }
    78  
    79  func (i *inboxMemCacheImpl) Clear(uid gregor1.UID) {
    80  	i.Lock()
    81  	defer i.Unlock()
    82  	delete(i.versMap, uid.String())
    83  	delete(i.indexMap, uid.String())
    84  	i.convMap = make(map[string]types.RemoteConversation)
    85  }
    86  
    87  func (i *inboxMemCacheImpl) clearCache() {
    88  	i.Lock()
    89  	defer i.Unlock()
    90  	i.versMap = make(map[string]*inboxDiskVersions)
    91  	i.indexMap = make(map[string]*inboxDiskIndex)
    92  	i.convMap = make(map[string]types.RemoteConversation)
    93  }
    94  
    95  func (i *inboxMemCacheImpl) OnLogout(m libkb.MetaContext) error {
    96  	i.clearCache()
    97  	return nil
    98  }
    99  
   100  func (i *inboxMemCacheImpl) OnDbNuke(m libkb.MetaContext) error {
   101  	i.clearCache()
   102  	return nil
   103  }
   104  
   105  var inboxMemCache = newInboxMemCacheImpl()