You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"log""github.com/xyproto/simpleredis/v2"
)
funcmain() {
// Check if the redis service is upiferr:=simpleredis.TestConnection(); err!=nil {
log.Fatalln("Could not connect to Redis. Is the service up and running?")
}
// For checking if a Redis server on a specific host:port is up.// simpleredis.TestConnectionHost("localhost:6379")// Create a connection pool, connect to the given redis serverpool:=simpleredis.NewConnectionPool()
// Use this for connecting to a different redis host/port// pool := simpleredis.NewConnectionPoolHost("localhost:6379")// For connecting to a different redis host/port, with a password// pool := simpleredis.NewConnectionPoolHost("password@redishost:6379")// Close the connection pool right after this function returnsdeferpool.Close()
// Create a list named "greetings"list:=simpleredis.NewList(pool, "greetings")
// Add "hello" to the list, check if there are errorsiflist.Add("hello") !=nil {
log.Fatalln("Could not add an item to list!")
}
// Get the last item of the listifitem, err:=list.GetLast(); err!=nil {
log.Fatalln("Could not fetch the last item from the list!")
} else {
log.Println("The value of the stored item is:", item)
}
// Remove the listiflist.Remove() !=nil {
log.Fatalln("Could not remove the list!")
}
}
Testing
Redis must be up and running locally for the go test tests to work.
Timeout issues
If there are timeout issues when connecting to Redis, try consulting the Redis latency doctor on the server by running redis-cli and then latency doctor.