Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/kumparan/go-utils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -165,3 +166,33 @@ func StoreCaches[K comparable, V any](cacheKeeper Keeper, keys []K, buffer map[K
logger.WithField("cacheItems", utils.Dump(cacheItems)).Error(err)
}
}

// StoreCachesWithCustomTTL store multiple object by keys with custom ttl
func StoreCachesWithCustomTTL[K comparable, V any](cacheKeeper Keeper, keys []K, buffer map[K]V, cacheKeyFunc func(K) string, ttl time.Duration) {
logger := logrus.WithFields(logrus.Fields{
"keys": keys,
"buffer": utils.Dump(buffer),
})

var cacheItems []Item
for _, key := range keys {
val, ok := buffer[key]
if !ok {
cacheItems = append(cacheItems, NewItem(cacheKeyFunc(key), []byte("null")))
continue
}

jsonVal, err := json.Marshal(val)
if err != nil {
logger.WithField("key", key).Error(err)
continue
}

cacheItems = append(cacheItems, NewItemWithCustomTTL(cacheKeyFunc(key), jsonVal, ttl))
}

err := cacheKeeper.StoreMultiWithoutBlocking(cacheItems)
if err != nil {
logger.WithField("cacheItems", utils.Dump(cacheItems)).Error(err)
}
}
2 changes: 1 addition & 1 deletion keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type (

CheckKeyExist(string) (bool, error)

//list
// list
StoreRightList(string, any) error
StoreLeftList(string, any) error
GetList(string, int64, int64) (any, error)
Expand Down
Loading