Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "fmt"
- "strings"
- "github.com/go-redis/redis"
- )
- func main() {
- fmt.Println("Hello!")
- rdb := redis.NewClusterClient(&redis.ClusterOptions{
- Addrs: []string{":6379", ":6380",
- ":6381", "7379", ":7380", ":7381"},
- })
- // Check if usual operations work.
- err := rdb.SAdd(context.Background(),
- "lol{a}", "suck", "my", "dick").Err()
- handleError(err)
- set, err := rdb.SMembers(context.Background(),
- "lol{a}").Result()
- handleError(err)
- fmt.Println("lol{a}:", strings.Join(set, ", "))
- // Check if transactions work.
- // Keys should have the same hashtag in {}
- // in order to perform transactions and scripts on them.
- tx := rdb.TxPipeline()
- err = tx.SAdd(context.Background(),
- "kek{a}", "fuck", "shit").Err()
- handleError(err)
- err = tx.SUnionStore(context.Background(),
- "lol{a}", "kek{a}", "lol{a}").Err()
- handleError(err)
- err = tx.Del(context.Background(),
- "kek{a}").Err()
- handleError(err)
- _, err = tx.Exec(context.Background())
- handleError(err)
- err = tx.Close()
- handleError(err)
- set, err = rdb.SMembers(context.Background(),
- "lol{a}").Result()
- handleError(err)
- fmt.Println("lol{a}:", strings.Join(set, ", "))
- // Check if Lua scripts work.
- script := `redis.call('SADD', KEYS[1], ARGV[1], ARGV[2], ARGV[3])
- return redis.call('SMEMBERS', KEYS[1])`
- data, err := rdb.Eval(context.Background(), script,
- []string{"foo"}, "bar", "fizz", "buzz").Result()
- handleError(err)
- elems := data.([]interface{})
- set = make([]string, len(elems))
- for i, elem := range elems {
- set[i] = elem.(string)
- }
- fmt.Println("Set:", strings.Join(set, ", "))
- }
- func handleError(err error) {
- if err != nil {
- panic(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement