-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: added redis keyspace notification #4
- Loading branch information
1 parent
072280f
commit 388b89f
Showing
6 changed files
with
121 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ go.work | |
.DS_Store | ||
|
||
# Main | ||
main/ | ||
main.go | ||
|
||
# Logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/main.go" | ||
} | ||
] | ||
} | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/main/main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package redisconn | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/go-redis/redis" | ||
) | ||
|
||
type RedisKeySpaceService interface { | ||
AddCallback(c func(message *redis.Message, err error)) RedisKeySpaceService | ||
Register() | ||
Unregister() | ||
PatternKeySpace() string | ||
PatternKeyEvent() string | ||
} | ||
|
||
type redisKeySpaceServiceImpl struct { | ||
client *redis.Client | ||
callback []func(message *redis.Message, err error) | ||
stop chan struct{} | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewRedisKeySpaceService(client *redis.Client) RedisKeySpaceService { | ||
return &redisKeySpaceServiceImpl{ | ||
client: client, | ||
callback: make([]func(event *redis.Message, err error), 0), | ||
stop: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) AddCallback(c func(message *redis.Message, err error)) RedisKeySpaceService { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
s.callback = append(s.callback, c) | ||
return s | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) Register() { | ||
go s.run() | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) Unregister() { | ||
close(s.stop) | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) PatternKeySpace() string { | ||
return "__keyspace@*__:*" | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) PatternKeyEvent() string { | ||
return "__keyevent@*__:*" | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) run() { | ||
pattern := []string{s.PatternKeySpace(), s.PatternKeyEvent()} | ||
pubsub := s.client.PSubscribe(pattern...) | ||
for { | ||
select { | ||
case <-s.stop: | ||
_ = pubsub.Close() | ||
return | ||
default: | ||
msg, err := pubsub.ReceiveMessage() | ||
s.hook(msg, err) | ||
} | ||
} | ||
} | ||
|
||
func (s *redisKeySpaceServiceImpl) hook(message *redis.Message, err error) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
for _, callback := range s.callback { | ||
callback(message, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters