Skip to content

Commit

Permalink
fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 10, 2023
1 parent 61e9657 commit c071e00
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions bridge-history-api/internal/logic/history_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,28 @@ func (h *HistoryLogic) getCachedTxsInfo(ctx context.Context, cacheKey string, pa

total, err := h.redis.ZCard(ctx, cacheKey).Result()
if err != nil {
if err == redis.Nil {
// Key does not exist, cache miss.
return nil, 0, false, nil
}
log.Error("failed to get zcard result", "error", err)
return nil, 0, false, err
}

values, err := h.redis.ZRange(ctx, cacheKey, start, end).Result()
if err != nil {
if err == redis.Nil {
// Key does not exist, cache miss.
return nil, 0, false, nil
}
log.Error("failed to get zrange result", "error", err)
return nil, 0, false, err
}

// Perform a final check to confirm the existence of the key to ensure consistency between ZCard and ZRange data reads.
exists, err := h.redis.Exists(ctx, cacheKey).Result()
if err != nil {
log.Error("failed to check if key exists", "error", err)
return nil, 0, false, err
}

// If the key does not exist, we consider it a cache miss and return accordingly.
if exists == 0 {
return nil, 0, false, nil
}

var pagedTxs []*types.TxHistoryInfo
for _, v := range values {
var tx types.TxHistoryInfo
Expand All @@ -326,7 +330,12 @@ func (h *HistoryLogic) cacheTxsInfo(ctx context.Context, cacheKey string, txs []
_, err := h.redis.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
// The transactions are sorted, thus we set the score as their indices.
for i, tx := range txs {
if err := pipe.ZAdd(ctx, cacheKey, &redis.Z{Score: float64(i), Member: tx}).Err(); err != nil {
txBytes, err := json.Marshal(tx)
if err != nil {
log.Error("failed to marshal transaction to json", "error", err)
return err
}
if err := pipe.ZAdd(ctx, cacheKey, &redis.Z{Score: float64(i), Member: txBytes}).Err(); err != nil {
log.Error("failed to add transaction to sorted set", "error", err)
return err
}
Expand Down

0 comments on commit c071e00

Please sign in to comment.