Skip to content

Commit

Permalink
Added simple golang example with redigo lib for work with Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Nov 4, 2024
1 parent 477c571 commit 3f2efee
Show file tree
Hide file tree
Showing 19 changed files with 4,236 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/golang/cource/06g_databases/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.2

require (
github.com/georgysavva/scany v1.2.2
github.com/gomodule/redigo v1.9.2
github.com/jackc/pgx/v4 v4.18.3
go.mongodb.org/mongo-driver v1.17.1
)
Expand Down
2 changes: 2 additions & 0 deletions examples/golang/cource/06g_databases/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s=
github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
Expand Down
7 changes: 7 additions & 0 deletions examples/golang/cource/06g_databases/redis.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
KEYS "*"
GET some
SET some 42
INCR some
DEL some
EXPIRE 10
LRANGE list 0 3
83 changes: 83 additions & 0 deletions examples/golang/cource/06g_databases/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fmt"
// "time"
"github.com/gomodule/redigo/redis"
)

func RedisSimple() {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
defer conn.Close()

_, err = conn.Do("SET", "somekey1", 42)
if err != nil {
fmt.Println(err)
}
get, _ := redis.Int(conn.Do("GET", "somekey1"))
fmt.Printf("Somekey1: %d\n", get)
incr, _ := redis.Int(conn.Do("INCR", "somekey1"))
fmt.Printf("Somekey1: %d\n", incr)
// conn.Do("EXPIRE", "somekey1", 3)
// time.Sleep(4 * time.Second)
// get2, _ := conn.Do("GET", "somekey1")
// fmt.Printf("Somekey1: %d\n", get2)
}

func RedisScan() {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
defer func() {
if err = conn.Close(); err != nil {
panic(err)
}
}()

iter := 0
var keys []string
for {
if array, err := redis.Values(conn.Do("SCAN", iter)); err != nil {
panic(err)
} else {
iter, _ = redis.Int(array[0], nil)
keys, _ = redis.Strings(array[1], nil)
}
fmt.Println(keys)

if iter == 0 {
break
}
}
}

func RedisListType() {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
defer func() {
if err = conn.Close(); err != nil {
panic(err)
}
}()

conn.Do("RPUSH", "somelist", "one")
conn.Do("RPUSH", "somelist", "two")
conn.Do("RPUSH", "somelist", "three")

vars, _ := redis.Values(conn.Do("LRANGE", "somelist", 0, 4))
fmt.Println(redis.String(vars[0], nil))
fmt.Println(redis.String(vars[1], nil))
fmt.Println(redis.String(vars[2], nil))
}

func main() {
RedisSimple()
RedisListType()
RedisScan()
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f2efee

Please sign in to comment.