Created
May 9, 2023 16:30
-
-
Save benhenryhunter/0f0b7e9d53ed0f0cdd02e95ad661345f to your computer and use it in GitHub Desktop.
Redis stuff
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
package main | |
import ( | |
"context" | |
"fmt" | |
"time" | |
"github.com/redis/go-redis/v9" | |
) | |
func main() { | |
rdb, err := connectRedis(":6379", 10) | |
if err != nil { | |
panic(err) | |
} | |
ctx := context.Background() | |
v, err := rdb.SetArgs(ctx, "test-2", "test", redis.SetArgs{ | |
Mode: "NX", | |
Get: true, | |
}).Result() | |
if err != nil && err != redis.Nil { | |
panic(err) | |
} | |
fmt.Println(v) | |
v2, err := rdb.SetArgs(ctx, "test-2", "test2", redis.SetArgs{ | |
Mode: "NX", | |
Get: true, | |
}).Result() | |
if err != nil && err != redis.Nil { | |
panic(err) | |
} | |
fmt.Println(v2) | |
} | |
func connectRedis(redisURI string, connectionPoolLimit int) (*redis.Client, error) { | |
redisClient := redis.NewClient(&redis.Options{ | |
PoolSize: connectionPoolLimit, | |
Addr: redisURI, | |
MinIdleConns: connectionPoolLimit / 2, | |
PoolTimeout: 1 * time.Minute, | |
ReadTimeout: 1 * time.Second, | |
WriteTimeout: 1 * time.Second, | |
ContextTimeoutEnabled: true, | |
}) | |
// if _, err := redisClient.Ping(context.Background()).Result(); err != nil { | |
// // unable to connect to redis | |
// return nil, err | |
// } | |
return redisClient, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment