Skip to content

Commit

Permalink
🔇 silent changes: updated codebase and added example #6 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 7, 2024
1 parent efef7ea commit b147590
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 148 deletions.
25 changes: 12 additions & 13 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ name: Go

on:
push:
branches: [ "master" ]
branches: ["master"]
tags:
- "v*"
pull_request:
branches: [ "master" ]
branches: ["master"]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
# - name: Test
# run: go test -v ./...

create-release:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -57,4 +56,4 @@ jobs:
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
209 changes: 74 additions & 135 deletions README.md
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
```
132 changes: 132 additions & 0 deletions example/redisconn_test.go
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
}
}
Loading

0 comments on commit b147590

Please sign in to comment.