Skip to content

Commit

Permalink
add caching to emote loader
Browse files Browse the repository at this point in the history
  • Loading branch information
broadeditz committed Nov 14, 2023
1 parent ce775eb commit b0e62b3
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions internal/loaders/emote.loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/seventv/common/structures/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)

func emoteLoader(ctx context.Context, x inst, key string) EmoteLoaderByID {
Expand All @@ -26,9 +27,33 @@ func emoteLoader(ctx context.Context, x inst, key string) EmoteLoaderByID {
items[i] = structures.DeletedEmote
}

// Fetch emotes from cache
cachedEmotes, err := getEmotesFromCache(ctx, x, keys)
if err != nil {
zap.S().Errorw("redis failed to fetch emotes from cache", "error", err)
errs = append(errs, err)
}

remainingKeys := []primitive.ObjectID{}
keysLoop:
for i, key := range keys {
for _, emote := range cachedEmotes {
if emote.ID == key {
// TODO
zap.S().Info("Loaded emote from cache" + emote.Name)

items[i] = emote
continue keysLoop
}
}

// key was not found in cache, so we get it from mongo
remainingKeys = append(remainingKeys, key)
}

// Fetch emotes
emotes, err := x.query.Emotes(ctx, bson.M{
key: bson.M{"$in": keys},
key: bson.M{"$in": remainingKeys},
}).Items()

if err == nil {
Expand All @@ -40,15 +65,22 @@ func emoteLoader(ctx context.Context, x inst, key string) EmoteLoaderByID {
}

for i, v := range keys {
if x, ok := m[v]; ok {
ver, _ := x.GetVersion(v)
if emote, ok := m[v]; ok {
ver, _ := emote.GetVersion(v)
if ver.ID.IsZero() {
continue
}

x.ID = v
x.VersionRef = &ver
items[i] = x
emote.ID = v
emote.VersionRef = &ver
items[i] = emote

// store emote in redis cache
err = setEmoteInCache(ctx, x, emote)
if err != nil {
zap.S().Errorw("redis failed to set emote in cache", "error", err)
errs = append(errs, err)
}
}
}
}
Expand All @@ -58,6 +90,29 @@ func emoteLoader(ctx context.Context, x inst, key string) EmoteLoaderByID {
})
}

var cacheKeyEmotes = "cache.emotes."

func getEmotesFromCache(ctx context.Context, x inst, baseKeys []primitive.ObjectID) ([]structures.Emote, error) {
keys := make([]string, len(baseKeys))

for _, key := range baseKeys {
keys = append(keys, cacheKeyEmotes+key.String())
}

emotes := []structures.Emote{}

err := x.redis.RawClient().MGet(ctx, keys...).Scan(&emotes)
if err != nil {
return nil, err
}

return emotes, nil
}

func setEmoteInCache(ctx context.Context, x inst, emote structures.Emote) error {
return x.redis.RawClient().Set(ctx, cacheKeyEmotes+emote.ID.String(), emote, 30*time.Second).Err()
}

func batchEmoteLoader(ctx context.Context, x inst, key string) BatchEmoteLoaderByID {
return dataloader.New(dataloader.Config[primitive.ObjectID, []structures.Emote]{
Wait: time.Millisecond * 25,
Expand Down

0 comments on commit b0e62b3

Please sign in to comment.