-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple golang example with redigo lib for work with Redis
- Loading branch information
Showing
19 changed files
with
4,236 additions
and
0 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
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,7 @@ | ||
KEYS "*" | ||
GET some | ||
SET some 42 | ||
INCR some | ||
DEL some | ||
EXPIRE 10 | ||
LRANGE list 0 3 |
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,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() | ||
} |
177 changes: 177 additions & 0 deletions
177
examples/golang/cource/06g_databases/vendor/github.com/gomodule/redigo/LICENSE
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
examples/golang/cource/06g_databases/vendor/github.com/gomodule/redigo/redis/commandinfo.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.