Skip to content

Commit 07e027a

Browse files
Added 3 examples on how to connect to the RedisAI Server. ( simplest way possible, with pool, and with SSL ) (#16)
* [add] Added 3 examples on how to connect to the RedisAI Server. ( simplest way possible, with pool, and with SSL )
1 parent f6b4ce7 commit 07e027a

File tree

2 files changed

+104
-49
lines changed

2 files changed

+104
-49
lines changed

redisai/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ type Client struct {
6565
ActiveConn redis.Conn
6666
}
6767

68-
// Connect intializes a Client
68+
// Connect establish an connection to the RedisAI Server.
69+
//
70+
//If a pool `*redis.Pool` is passed then it will be used to connect to the server.
71+
//
72+
// See the examples on how to connect with/without pool and on how to establish a secure SSL connection.
6973
func Connect(url string, pool *redis.Pool) (c *Client) {
7074
var cpool *redis.Pool = nil
7175
if pool == nil {

redisai/example_client_test.go

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,61 @@ import (
1111
"os"
1212
)
1313

14-
func getConnectionDetails() (host string, password string) {
15-
value, exists := os.LookupEnv("REDISAI_TEST_HOST")
16-
host = "localhost:6379"
17-
password = ""
18-
valuePassword, existsPassword := os.LookupEnv("REDISAI_TEST_PASSWORD")
19-
if exists && value != "" {
20-
host = value
21-
}
22-
if existsPassword && valuePassword != "" {
23-
password = valuePassword
14+
//Example of how to establish an connection from your app to the RedisAI Server
15+
func ExampleConnect() {
16+
17+
// Create a client.
18+
client := redisai.Connect("redis://localhost:6379", nil)
19+
20+
// Set a tensor
21+
// AI.TENSORSET foo FLOAT 2 2 VALUES 1.1 2.2 3.3 4.4
22+
_ = client.TensorSet("foo", redisai.TypeFloat, []int{2, 2}, []float32{1.1, 2.2, 3.3, 4.4})
23+
24+
// Get a tensor content as a slice of values
25+
// dt DataType, shape []int, data interface{}, err error
26+
// AI.TENSORGET foo VALUES
27+
_, _, fooTensorValues, err := client.TensorGetValues("foo")
28+
29+
if err != nil {
30+
log.Fatal(err)
2431
}
25-
return
32+
33+
fmt.Println(fooTensorValues)
34+
// Output: [1.1 2.2 3.3 4.4]
2635
}
2736

28-
func getTLSdetails() (tlsready bool, tls_cert string, tls_key string, tls_cacert string) {
29-
tlsready = false
30-
value, exists := os.LookupEnv("TLS_CERT")
31-
if exists && value != "" {
32-
info, err := os.Stat(value)
33-
if os.IsNotExist(err) || info.IsDir() {
34-
return
35-
}
36-
tls_cert = value
37-
} else {
38-
return
39-
}
40-
value, exists = os.LookupEnv("TLS_KEY")
41-
if exists && value != "" {
42-
info, err := os.Stat(value)
43-
if os.IsNotExist(err) || info.IsDir() {
44-
return
45-
}
46-
tls_key = value
47-
} else {
48-
return
49-
}
50-
value, exists = os.LookupEnv("TLS_CACERT")
51-
if exists && value != "" {
52-
info, err := os.Stat(value)
53-
if os.IsNotExist(err) || info.IsDir() {
54-
return
55-
}
56-
tls_cacert = value
57-
} else {
58-
return
37+
38+
//Example of how to establish an connection with a shared pool to the RedisAI Server
39+
func ExampleConnect_pool() {
40+
41+
host := "localhost:6379"
42+
password := ""
43+
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
44+
return redis.Dial("tcp", host, redis.DialPassword(password))
45+
}}
46+
47+
// Create a client.
48+
client := redisai.Connect("", pool)
49+
50+
// Set a tensor
51+
// AI.TENSORSET foo FLOAT 2 2 VALUES 1.1 2.2 3.3 4.4
52+
_ = client.TensorSet("foo", redisai.TypeFloat, []int{2, 2}, []float32{1.1, 2.2, 3.3, 4.4})
53+
54+
// Get a tensor content as a slice of values
55+
// dt DataType, shape []int, data interface{}, err error
56+
// AI.TENSORGET foo VALUES
57+
_, _, fooTensorValues, err := client.TensorGetValues("foo")
58+
59+
if err != nil {
60+
log.Fatal(err)
5961
}
60-
tlsready = true
61-
return
62+
63+
fmt.Println(fooTensorValues)
64+
// Output: [1.1 2.2 3.3 4.4]
6265
}
6366

64-
/*
65-
* Example of how to establish an SSL connection from your app to the RedisAI Server
66-
*/
67-
func ExampleConnect_TLS() {
67+
//Example of how to establish an SSL connection from your app to the RedisAI Server
68+
func ExampleConnect_ssl() {
6869
// Consider the following helper methods that provide us with the connection details (host and password)
6970
// and the paths for:
7071
// tls_cert - A a X.509 certificate to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted
@@ -132,3 +133,53 @@ func ExampleConnect_TLS() {
132133

133134
fmt.Println(fooTensorValues)
134135
}
136+
137+
func getConnectionDetails() (host string, password string) {
138+
value, exists := os.LookupEnv("REDISAI_TEST_HOST")
139+
host = "localhost:6379"
140+
password = ""
141+
valuePassword, existsPassword := os.LookupEnv("REDISAI_TEST_PASSWORD")
142+
if exists && value != "" {
143+
host = value
144+
}
145+
if existsPassword && valuePassword != "" {
146+
password = valuePassword
147+
}
148+
return
149+
}
150+
151+
func getTLSdetails() (tlsready bool, tls_cert string, tls_key string, tls_cacert string) {
152+
tlsready = false
153+
value, exists := os.LookupEnv("TLS_CERT")
154+
if exists && value != "" {
155+
info, err := os.Stat(value)
156+
if os.IsNotExist(err) || info.IsDir() {
157+
return
158+
}
159+
tls_cert = value
160+
} else {
161+
return
162+
}
163+
value, exists = os.LookupEnv("TLS_KEY")
164+
if exists && value != "" {
165+
info, err := os.Stat(value)
166+
if os.IsNotExist(err) || info.IsDir() {
167+
return
168+
}
169+
tls_key = value
170+
} else {
171+
return
172+
}
173+
value, exists = os.LookupEnv("TLS_CACERT")
174+
if exists && value != "" {
175+
info, err := os.Stat(value)
176+
if os.IsNotExist(err) || info.IsDir() {
177+
return
178+
}
179+
tls_cacert = value
180+
} else {
181+
return
182+
}
183+
tlsready = true
184+
return
185+
}

0 commit comments

Comments
 (0)