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

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/logger"
     7  
     8  	lru "github.com/hashicorp/golang-lru"
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/chat1"
    11  	"github.com/keybase/client/go/protocol/gregor1"
    12  	context "golang.org/x/net/context"
    13  )
    14  
    15  type logContext struct {
    16  	log  logger.Logger
    17  	vlog *libkb.VDebugLog
    18  }
    19  
    20  func newLogContext() *logContext {
    21  	return &logContext{
    22  		log:  logger.NewNull(),
    23  		vlog: libkb.NewVDebugLog(logger.NewNull()),
    24  	}
    25  }
    26  
    27  func (c *logContext) GetLog() logger.Logger {
    28  	return c.log
    29  }
    30  
    31  func (c *logContext) GetVDebugLog() *libkb.VDebugLog {
    32  	return c.vlog
    33  }
    34  
    35  type blockEngineMemCacheImpl struct {
    36  	blockCache *lru.Cache
    37  	logContext *logContext
    38  	lockTab    *libkb.LockTable
    39  }
    40  
    41  func newBlockEngineMemCache() *blockEngineMemCacheImpl {
    42  	c, _ := lru.New(100)
    43  	return &blockEngineMemCacheImpl{
    44  		blockCache: c,
    45  		logContext: newLogContext(),
    46  		lockTab:    libkb.NewLockTable(),
    47  	}
    48  }
    49  
    50  func (b *blockEngineMemCacheImpl) key(uid gregor1.UID, convID chat1.ConversationID, id int) string {
    51  	return fmt.Sprintf("%s:%s:%d", uid, convID, id)
    52  }
    53  
    54  func (b *blockEngineMemCacheImpl) getBlock(ctx context.Context, uid gregor1.UID,
    55  	convID chat1.ConversationID, id int) (block, bool) {
    56  	key := b.key(uid, convID, id)
    57  	lock := b.lockTab.AcquireOnName(ctx, b.logContext, key)
    58  	defer lock.Release(ctx)
    59  	if v, ok := b.blockCache.Get(key); ok {
    60  		bl := v.(block)
    61  		var retMsgs [blockSize]chat1.MessageUnboxed
    62  		for i := 0; i < blockSize; i++ {
    63  			retMsgs[i] = bl.Msgs[i].DeepCopy()
    64  		}
    65  		return block{
    66  			BlockID: bl.BlockID,
    67  			Msgs:    retMsgs,
    68  		}, true
    69  	}
    70  	return block{}, false
    71  }
    72  
    73  func (b *blockEngineMemCacheImpl) writeBlock(ctx context.Context, uid gregor1.UID,
    74  	convID chat1.ConversationID, bl block) {
    75  	key := b.key(uid, convID, bl.BlockID)
    76  	lock := b.lockTab.AcquireOnName(ctx, b.logContext, key)
    77  	defer lock.Release(ctx)
    78  	var storedMsgs [blockSize]chat1.MessageUnboxed
    79  	for i := 0; i < blockSize; i++ {
    80  		storedMsgs[i] = bl.Msgs[i].DeepCopy()
    81  	}
    82  	b.blockCache.Add(key, block{
    83  		BlockID: bl.BlockID,
    84  		Msgs:    storedMsgs,
    85  	})
    86  }
    87  
    88  func (b *blockEngineMemCacheImpl) OnLogout(m libkb.MetaContext) error {
    89  	b.blockCache.Purge()
    90  	return nil
    91  }
    92  
    93  func (b *blockEngineMemCacheImpl) OnDbNuke(m libkb.MetaContext) error {
    94  	b.blockCache.Purge()
    95  	return nil
    96  }
    97  
    98  var blockEngineMemCache = newBlockEngineMemCache()