-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
efef7ea
commit b147590
Showing
4 changed files
with
245 additions
and
148 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
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,139 +1,78 @@ | ||
# RedisConn | ||
|
||
## Example Redis Pub.Sub | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-redis/redis" | ||
"github.com/sivaosorg/govm/logger" | ||
"github.com/sivaosorg/govm/redisx" | ||
"github.com/sivaosorg/redisconn" | ||
) | ||
|
||
func main() { | ||
client, s := redisconn.NewClient(*redisx.GetRedisConfigSample().SetPassword("Tm5@P@ssw0rd").SetDebugMode(false)) | ||
logger.Infof("state connection: %v", s) | ||
if !s.IsConnected { | ||
panic(s.Error) | ||
} | ||
scanner := bufio.NewScanner(os.Stdin) | ||
ps := redisconn.NewRedisPubSub(client) | ||
pubsubs, err := ps.Subscribe("chat") | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
for _, pubsub := range pubsubs { | ||
go func(ps *redis.PubSub) { | ||
for { | ||
msg, err := ps.ReceiveMessage() | ||
if err != nil { | ||
panic(err) | ||
} | ||
logger.Infof("%s: %s", msg.Channel, msg.Payload) | ||
} | ||
}(pubsub) | ||
} | ||
}() | ||
logger.Infof("Type your message and hit enter to send a chat message!") | ||
for { | ||
fmt.Print("> ") | ||
if !scanner.Scan() { | ||
break | ||
} | ||
if scanner.Text() == "exit" || scanner.Text() == "quit" { | ||
break | ||
} | ||
// Publish the chat message to the "chat" channel | ||
err := ps.Publish("chat", scanner.Text()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
// Unsubscribe from multiple channels and close the connection | ||
err = ps.Unsubscribe("chat") | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = ps.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
# redisconn | ||
|
||
![GitHub contributors](https://img.shields.io/github/contributors/sivaosorg/gocell) | ||
![GitHub followers](https://img.shields.io/github/followers/sivaosorg) | ||
![GitHub User's stars](https://img.shields.io/github/stars/pnguyen215) | ||
|
||
A Golang Redis connector library with features for Redis Pub/Sub, key-value operations, and distributed locking. | ||
|
||
## Table of Contents | ||
|
||
- [redisconn](#redisconn) | ||
- [Table of Contents](#table-of-contents) | ||
- [Introduction](#introduction) | ||
- [Prerequisites](#prerequisites) | ||
- [Installation](#installation) | ||
- [Modules](#modules) | ||
- [Running Tests](#running-tests) | ||
- [Tidying up Modules](#tidying-up-modules) | ||
- [Upgrading Dependencies](#upgrading-dependencies) | ||
- [Cleaning Dependency Cache](#cleaning-dependency-cache) | ||
|
||
## Introduction | ||
|
||
Welcome to the Redis Connector for Go repository! This library provides a set of tools for seamless interaction with Redis in your Go applications. It includes features for Redis Pub/Sub, key-value operations, and distributed locking. | ||
|
||
## Prerequisites | ||
|
||
Golang version v1.20 | ||
|
||
## Installation | ||
|
||
- Latest version | ||
|
||
```bash | ||
go get -u github.com/sivaosorg/redisconn@latest | ||
``` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/go-redis/redis" | ||
"github.com/sivaosorg/govm/logger" | ||
"github.com/sivaosorg/govm/redisx" | ||
"github.com/sivaosorg/redisconn" | ||
) | ||
|
||
func main() { | ||
client, s := redisconn.NewClient(*redisx.GetRedisConfigSample().SetPassword("Tm5@P@ssw0rd").SetDebugMode(false)) | ||
logger.Infof("state connection: %v", s) | ||
if !s.IsConnected { | ||
panic(s.Error) | ||
} | ||
ps := redisconn.NewRedisPubSub(client) | ||
pubSubs, err := ps.Subscribe("channel_1", "channel_2", "channel_3") | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
for _, pubsub := range pubSubs { | ||
go func(ps *redis.PubSub) { | ||
for { | ||
msg, err := ps.ReceiveMessage() | ||
if err != nil { | ||
panic(err) | ||
} | ||
logger.Infof("%s: %s", msg.Channel, msg.Payload) | ||
} | ||
}(pubsub) | ||
} | ||
}() | ||
|
||
// Publish a message to the subscribed channels | ||
err = ps.Publish("channel_1", "content1") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ps.Publish("channel_2", "content2") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ps.Publish("channel_3", "content3") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ps.Publish("channel_1", "content4") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Unsubscribe from multiple channels and close the connection | ||
err = ps.Unsubscribe("channel_1", "channel_2", "channel_3") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ps.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
- Use a specific version (tag) | ||
|
||
```bash | ||
go get github.com/sivaosorg/redisconn@v0.0.1 | ||
``` | ||
|
||
## Modules | ||
|
||
Explain how users can interact with the various modules. | ||
|
||
### Running Tests | ||
|
||
To run tests for all modules, use the following command: | ||
|
||
```bash | ||
make test | ||
``` | ||
|
||
### Tidying up Modules | ||
|
||
To tidy up the project's Go modules, use the following command: | ||
|
||
```bash | ||
make tidy | ||
``` | ||
|
||
### Upgrading Dependencies | ||
|
||
To upgrade project dependencies, use the following command: | ||
|
||
```bash | ||
make deps-upgrade | ||
``` | ||
|
||
### Cleaning Dependency Cache | ||
|
||
To clean the Go module cache, use the following command: | ||
|
||
```bash | ||
make deps-clean-cache | ||
``` |
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,132 @@ | ||
package example | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-redis/redis" | ||
"github.com/sivaosorg/govm/dbx" | ||
"github.com/sivaosorg/govm/logger" | ||
"github.com/sivaosorg/govm/redisx" | ||
"github.com/sivaosorg/redisconn" | ||
) | ||
|
||
func createConn() (*redisconn.Redis, dbx.Dbx) { | ||
return redisconn.NewClient(*redisx.GetRedisConfigSample().SetDebugMode(true).SetPassword("Tm5@P@ssw0rd")) | ||
} | ||
|
||
func TestConn(t *testing.T) { | ||
_, s := createConn() | ||
logger.Infof("Redis connection status: %v", s) | ||
} | ||
|
||
func TestGetKeys(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisService(r.GetConn()) | ||
keys := svc.ListKeys() | ||
logger.Infof("Current all keys: %v", keys) | ||
} | ||
|
||
func TestGetKeysNearExpired(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisService(r.GetConn()) | ||
keys := svc.ListKeysNearExpired() | ||
logger.Infof("Current all keys near expiring: %v", keys) | ||
} | ||
|
||
func TestSetKey(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisService(r.GetConn()) | ||
err := svc.Set("_key_123", 123, time.Second*10) | ||
if err != nil { | ||
logger.Errorf("Setting key on redis got an error", err) | ||
return | ||
} | ||
logger.Successf("Set key on redis successfully") | ||
} | ||
|
||
func TestGetKey(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisService(r.GetConn()) | ||
value, err := svc.Get("_key_123") | ||
if err != nil { | ||
logger.Errorf("Getting key on redis got an error", err) | ||
return | ||
} | ||
logger.Infof("Value: %v", value) | ||
} | ||
|
||
func TestRemoveKey(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisService(r.GetConn()) | ||
err := svc.Delete("_key_123") | ||
if err != nil { | ||
logger.Errorf("Removing key on redis got an error", err) | ||
return | ||
} | ||
logger.Successf("Removed key on redis successfully") | ||
} | ||
|
||
func TestPublish(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisPubSub(r.GetConn()) | ||
err := svc.Publish("channel_1", 123) | ||
if err != nil { | ||
logger.Errorf("Publishing message on redis got an error", err) | ||
return | ||
} | ||
logger.Infof("Published message on redis successfully") | ||
} | ||
|
||
func TestConsume(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisPubSub(r.GetConn()) | ||
subs, err := svc.Subscribe("channel_1") | ||
if err != nil { | ||
logger.Errorf("Subscribing message on redis got an error", err) | ||
return | ||
} | ||
go func() { | ||
for _, sub := range subs { | ||
go func(ps *redis.PubSub) { | ||
for { | ||
msg, err := ps.ReceiveMessage() | ||
if err != nil { | ||
logger.Errorf("Subscribing message on channel %v got an error", err, msg.Channel) | ||
return | ||
} | ||
logger.Infof("%s: %s", msg.Channel, msg.Payload) | ||
} | ||
}(sub) | ||
} | ||
}() | ||
|
||
// Unsubscribe from multiple channels and close the connection | ||
err = svc.Unsubscribe("channel_1") | ||
if err != nil { | ||
logger.Errorf("Unsubscribing message on redis got an error", err) | ||
return | ||
} | ||
err = svc.Close() | ||
if err != nil { | ||
logger.Errorf("Closing message on redis got an error", err) | ||
return | ||
} | ||
} | ||
|
||
func TestConsumeCallback(t *testing.T) { | ||
r, _ := createConn() | ||
svc := redisconn.NewRedisPubSub(r.GetConn()) | ||
callback := func(msg *redis.Message, err error) { | ||
if err != nil { | ||
logger.Errorf("Subscribing message on channel %v got an error", err, msg.Channel) | ||
return | ||
} | ||
logger.Infof("%s: %s", msg.Channel, msg.Payload) | ||
} | ||
err := svc.SubscribeWith(callback, false, "channel_1") | ||
if err != nil { | ||
logger.Errorf("Subscribing message on redis got an error", err) | ||
return | ||
} | ||
} |
Oops, something went wrong.