Skip to content

Commit

Permalink
♻️ refactor: updated codebase #6 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 7, 2024
1 parent b147590 commit 072280f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 4 additions & 4 deletions example/redisconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func TestPublish(t *testing.T) {

func TestConsume(t *testing.T) {
r, _ := createConn()
svc := redisconn.NewRedisPubSub(r.GetConn())
subs, err := svc.Subscribe("channel_1")
svc := redisconn.NewRedisService(r.GetConn())
subs, err := svc.SyncPubSub().Subscribe("channel_1")
if err != nil {
logger.Errorf("Subscribing message on redis got an error", err)
return
Expand All @@ -102,12 +102,12 @@ func TestConsume(t *testing.T) {
}()

// Unsubscribe from multiple channels and close the connection
err = svc.Unsubscribe("channel_1")
err = svc.SyncPubSub().Unsubscribe("channel_1")
if err != nil {
logger.Errorf("Unsubscribing message on redis got an error", err)
return
}
err = svc.Close()
err = svc.SyncPubSub().Close()
if err != nil {
logger.Errorf("Closing message on redis got an error", err)
return
Expand Down
4 changes: 2 additions & 2 deletions redisconn_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type RedisPubSubClient struct {
redisConn *redis.Client `json:"-"`
subscriptionMap map[string]*redis.PubSub `json:"-"`
redisConn *redis.Client
subscriptionMap map[string]*redis.PubSub
}

type RedisMutex struct {
Expand Down
13 changes: 11 additions & 2 deletions redisconn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ type RedisService interface {
Lock(key string, value interface{}, expiration time.Duration) error
Unlock(key string, value interface{}) error
Handler() *redis.Client

// sync pubsub hook
SyncPubSub() RedisPubSubService
}

type redisServiceImpl struct {
redisConn *redis.Client
mutex *RedisMutex
pubsub RedisPubSubService
}

func NewRedisMutex() *RedisMutex {
func newRedisMutex() *RedisMutex {
return &RedisMutex{make(map[string]*sync.Mutex)}
}

func NewRedisService(redisConn *redis.Client) RedisService {
s := &redisServiceImpl{
redisConn: redisConn,
mutex: NewRedisMutex(),
mutex: newRedisMutex(),
pubsub: NewRedisPubSub(redisConn),
}
return s
}
Expand Down Expand Up @@ -212,3 +217,7 @@ func (r *redisServiceImpl) Unlock(key string, value interface{}) error {
func (r *redisServiceImpl) Handler() *redis.Client {
return r.redisConn
}

func (r *redisServiceImpl) SyncPubSub() RedisPubSubService {
return r.pubsub
}

0 comments on commit 072280f

Please sign in to comment.