MCPcopy Create free account
hub / github.com/bricks-cloud/BricksLLM / IncrementCounter

Method IncrementCounter

internal/storage/redis/cache.go:62–101  ·  view source on GitHub ↗
(keyId string, timeUnit key.TimeUnit, incr int64)

Source from the content-addressed store, hash-verified

60}
61
62func (c *Cache) IncrementCounter(keyId string, timeUnit key.TimeUnit, incr int64) error {
63 ctxTimeout, cancel := context.WithTimeout(context.Background(), c.wt)
64 defer cancel()
65
66 ts, err := getCounterTimeStamp(timeUnit)
67 if err != nil {
68 return err
69 }
70
71 err = c.client.HIncrBy(ctxTimeout, keyId, strconv.FormatInt(ts, 10), incr).Err()
72 if err != nil {
73 return err
74 }
75
76 ctxTimeout, cancel = context.WithTimeout(context.Background(), c.rt)
77 defer cancel()
78 dur := c.client.TTL(ctxTimeout, keyId)
79 err = dur.Err()
80 if err != nil {
81 return err
82 }
83
84 val := dur.Val()
85 if val < 0 {
86 ttl, err := getCounterTtl(timeUnit)
87 if err != nil {
88 return err
89 }
90
91 ctxTimeout, cancel = context.WithTimeout(context.Background(), c.wt)
92 defer cancel()
93 err = c.client.ExpireAt(ctxTimeout, keyId, ttl).Err()
94 if err != nil {
95 return err
96 }
97
98 }
99
100 return nil
101}
102
103func getCounterTtl(rateLimitUnit key.TimeUnit) (time.Time, error) {
104 now := time.Now().UTC()

Callers

nothing calls this directly

Calls 2

getCounterTimeStampFunction · 0.85
getCounterTtlFunction · 0.85

Tested by

no test coverage detected