From 13df939636a1afa6e1b809b71beef84cd6646a93 Mon Sep 17 00:00:00 2001 From: jano3 <jano@bob.co.za> Date: Mon, 12 Aug 2024 13:13:39 +0200 Subject: [PATCH] Add Redis increment and decrement counter functions --- redis/redis.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/redis/redis.go b/redis/redis.go index cb305b8..98972ed 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -308,3 +308,29 @@ func (r ClientWithHelpers) KeepLockKeyAlive(key string, expiration time.Duration _ = r.Client.Expire(ctx, key, expiration) } + +func (r ClientWithHelpers) IncrementCounter(key string) *int64 { + if !r.IsConnected() { + return nil + } + + val, err := r.Client.Incr(ctx, key).Result() + if err != nil { + return nil + } + + return &val +} + +func (r ClientWithHelpers) DecrementCounter(key string) *int64 { + if !r.IsConnected() { + return nil + } + + val, err := r.Client.Decr(ctx, key).Result() + if err != nil { + return nil + } + + return &val +} -- GitLab