Skip to content

Commit

Permalink
adding changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ak-gupta89 committed Apr 20, 2023
1 parent 9b2e702 commit 7058b39
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package goredis

import (
"fmt"
"strconv"
"strings"
"time"
"fmt"
"strconv"
"strings"
"time"

myconfig "github.com/contlo/go_common_lib/config"
log "github.com/contlo/go_common_lib/logger"
myconfig "github.com/contlo/go_common_lib/config"
log "github.com/contlo/go_common_lib/logger"

"gopkg.in/redis.v4"
"gopkg.in/redis.v4"
)

//redis global client to be declared
Expand All @@ -28,7 +28,8 @@ type IClient interface {
ZCard(key string) int64
SMembers(key string) []string
Expire(key string, expire time.Duration) bool
Lock(key string, expire time.Duration) bool
Lock(key string, expire time.Duration) bool
GetRedisClient() *redis.Client
}

type Client struct {
Expand Down Expand Up @@ -128,14 +129,6 @@ func (client *Client) SetValueEx(key string, value string, seconds int) error {
}
return err
}
func (client *Client) Lock(key string, expire time.Duration) bool {
if client.GetRedisClient().Exists(key).Val() {
return false
}
if client.GetRedisClient().SetNX(key, 1, expire).Val() {
return true
}
}

func (client *Client) LPush(key string, value string) error {
err := client.GetRedisClient().LPush(key, value).Err()
Expand Down Expand Up @@ -184,6 +177,15 @@ func (client *Client) ZRangeByScore(key string, min string, max string, offset i
val := client.GetRedisClient().ZRangeByScore(key, redis.ZRangeBy{Min: min, Max: max, Offset: offset, Count: count})
return val.Val()
}
func (client *Client) Lock(key string, expire time.Duration) bool {
val := client.GetRedisClient().Incr(key)
if val.Val() > 1 {
return false
} else {
client.GetRedisClient().Expire(key, expire)
}
return true
}

////////////////// cluster functions

Expand Down Expand Up @@ -265,10 +267,11 @@ func (client *ClusterClient) Expire(key string, expire time.Duration) bool {
return val.Val()
}
func (client *ClusterClient) Lock(key string, expire time.Duration) bool {
if client.GetRedisClient().Exists(key).Val() {
return false
}
if client.GetRedisClient().SetNX(key, 1, expire).Val() {
return true
}
}
val := client.GetRedisClient().Incr(key)
if val.Val() > 1 {
return false
} else {
client.GetRedisClient().Expire(key, expire)
}
return true
}

0 comments on commit 7058b39

Please sign in to comment.