This is a lightweight, feature-rich Redis client for Go. It provides a simple and efficient way to interact with Redis servers, supporting various Redis commands and connection options with RESP v2.
- Simple and intuitive API
- Support for multiple Redis commands
- Connection pooling (placeholder, not yet implemented)
- Custom connection functions
- SSL/TLS support (placeholder, not yet implemented)
- Authentication support
- Flexible configuration options
- Socket timeout and keep-alive settings
To install the Redis client, use the following command:
go get github.com/Re1nGer/go_redis_client@v1.2.0
To connect to a Redis server, use the NewClient
function:
import "github.com/re1nger/redis-client-go"
client, err := redisclient.NewClient("localhost", 6379)
if err != nil {
// Handle error
}
defer client.Close()
You can provide additional options when connecting:
client, err := redisclient.NewClient("localhost", 6379,
redisclient.WithUsername("myuser"),
redisclient.WithPassword("mypassword"),
redisclient.DecodeResponses(true),
redisclient.WithClientName("my-app"),
redisclient.WithSocketConnectTimeout(5 * time.Second),
redisclient.WithSocketTimeout(10 * time.Second),
redisclient.WithSocketKeepAlive(true, 30 * time.Second),
)
if err != nil {
// Handle error
}
defer client.Close()
You can also provide a custom connection function:
customConnFunc := func() (net.Conn, error) {
// Your custom connection logic here
return net.Dial("tcp", "localhost:6379")
}
client, err := redisclient.NewClient("localhost", 6379,
redisclient.WithCustomConnectFunc(customConnFunc),
)
if err != nil {
// Handle error
}
defer client.Close()
To execute Redis commands, use corresponding command names as methods:
// Using a specific command method
result, err := client.Set("mykey", "myvalue")
if err != nil {
// Handle error
}
// Getting a value
value, err := client.Get("mykey")
if err != nil {
// Handle error
}
The client provides a flexible way to use the SET command with various options:
opts := redisclient.NewSetOpts().WithNX().WithEX(60)
result, err := client.Set("mykey", "myvalue", opts)
if err != nil {
// Handle error
}
To execute any command use Do
method:
// Using the general Do method
result, err := client.Do("SET", "mykey", "myvalue")
if err != nil {
// Handle error
}
The client supports the following Redis commands:
- GET
- SET (with options: NX, XX, EX, PX, EXAT, PXAT, KEEPTTL, GET)
- SETNX
- SETRANGE
- STRLEN
- APPEND
- DECR
- DECRBY
- GETDEL
- GETEX
- GETRANGE
- GETSET
- INCR
- INCRBY
- INCRYBYFLOAT
- LCS
- MGET
- MSET
- MSETNX
- PSETEX
- LPOP
- LINDEX
- LINSERT
- LLEN
- LMOVE
- LMPOP - pending
- LPOS
- LPUSH
- LPUSHX
- LRANGE
- LREM
- LSET
- LTRIM
- RPOP
- RPOPLPUSH - deprecated
- RPUSH
- RPUSHX
- SADD
- SCARD
- SDIFF
- SDIFFSTORE
- SINTER
- SINTERCARD
- SINTERSTORE
- SISMEMBER
- SMEMBERS
- SMISMEMBER
- SMOVE
- SPOP
- SRANDMEMBER
- SREM
- SSCAN
- SUNION
- SUNIONSTORE
- HDEL
- HSET
- HGET
- HEXISTS
- HGETALL
- HLEN
- HEXPIRETIME
- HINCRBY
- HINCRBYFLOAT
- HMGET
- HPERSIST
- HSTRLEN
- HEXPIRE
- BITCOUNT
- BITFIELD
- BITFIELD_RO
- BITOP
- GETBIT
- SETBIT
- DEL
- DUMP
- AUTH
- CLIENT CACHING
- CLIENT GETNAME
- CLIENT GETREDIR
- CLIENT ID
- CLIENT INFO
- CLIENT KILL
- CLIENT LIST
- CLIENT NO-NOEVICT
- CLIENT NO-TOUCH
- CLIENT PAUSE
- CLIENT REPLY
- CLIENT SETINFO
- CLIENT SETNAME
- CLIENT TRACKING
- CLIENT TRACKINGINFO
- CLIENT UNBLOCK
- CLIENT UNPAUSE
- ECHO
- PING
- QUIT (deprecated)
- ACL CAT
- ACL DELUSER
- ACL DRYRUN
- ACL GENPASS
- ACL GETUSER
- ACL LIST
- ACL LOAD
- ACL SAVE
- ACL SETUSER
- ACL USERS
- ACL WHOAMI
- BGREWRITEAOF
- BGSAVE
- COMMAND
- COMMAND COUNT
- COMMAND DOCS
- COMMAND GETKEYS
- COMMAND GETKEYSANDFLAGS
- COMMAND INFO
- COMMAND LIST
- CONFIG GET
- CONFIG RESETSTAT
- CONFIG REWRITE
- CONFIG SET
- DBSIZE
- FAILOVER
- FLUSHALL
- FLUSHDB
- INFO
- LASTSAVE
- LATENCY DOCTOR
- LATENCY GRAPH
- LATENCY HISTOGRAM
- LATENCY HISTORY
- LATENCY LATEST
- LATENCY RESET
- MEMORY DOCTOR
- MEMORY MALLOC-STATS
- MEMORY PURGE
- MEMORY STATS
- MEMORY USAGE
- MODULE UNLOAD
- MONITOR
- PSYNC
- REPLCONF
- REPLICAOF
- ROLE
- SAVE
- SLOWLOG LEN
- SLOWLOG RESET
- SWAPDB
- SYNC
- TIME
More commands will be added in future updates.
Tests are being added as well
The client supports various configuration options:
WithUsername(username string)
: Set the username for authenticationWithPassword(password string)
: Set the password for authenticationWithClientName(clientName string)
: Set a custom client nameWithCustomConnectFunc(connFunc RedisConnectFunc)
: Provide a custom connection functionWithSocketConnectTimeout(duration time.Duration)
: Set the timeout for connecting to the Redis serverWithSocketTimeout(duration time.Duration)
: Set the timeout for socket operationsWithSocketKeepAlive(enabled bool, interval time.Duration)
: Enable and set the interval for TCP keep-alive packets
The client returns errors for various scenarios, including connection failures, authentication errors, and Redis command errors. Always check the returned error and handle it appropriately in your application.
Contributions to this Redis client are welcome! Here's how you can contribute:
- Fork the repository
- Create a new branch for your feature or bug fix
- Write your code and tests
- Ensure all tests pass
- Submit a pull request with a clear description of your changes
- Implement connection pooling
- Add support for more Redis commands
- Implement SSL/TLS support
- Add more comprehensive error handling
- Add benchmarks tests
- Implement Context to add timeouts and command cancellations
This Redis client is released under the MIT License. See the LICENSE file for details.
This client is a work in progress and may not be suitable for production use without further testing and development. Use at your own risk.
For bug reports, feature requests, or general questions, please open an issue on the GitHub repository.