2019-06-22 13:46:20 +02:00
package redis
import (
2019-08-22 13:46:39 +02:00
"bufio"
"errors"
2019-06-22 13:46:20 +02:00
"fmt"
"strconv"
"github.com/mediocregopher/radix/v3"
2019-08-22 13:46:39 +02:00
"github.com/mediocregopher/radix/v3/resp/resp2"
2019-06-22 13:46:20 +02:00
)
2020-06-27 12:53:16 +02:00
// radixPool an interface to complete both *radix.Pool and *radix.Cluster.
type radixPool interface {
Do ( a radix . Action ) error
Close ( ) error
}
2019-08-06 17:40:13 +02:00
// RadixDriver the Redis service based on the radix go client,
// contains the config and the redis pool.
type RadixDriver struct {
2019-06-22 13:46:20 +02:00
// Connected is true when the Service has already connected
Connected bool
2019-08-06 17:40:13 +02:00
// Config the read-only redis database config.
Config Config
2020-06-27 12:53:16 +02:00
pool radixPool
2019-06-22 13:46:20 +02:00
}
// Connect connects to the redis, called only once
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) Connect ( c Config ) error {
2019-06-22 13:46:20 +02:00
if c . Timeout < 0 {
c . Timeout = DefaultRedisTimeout
}
if c . Network == "" {
c . Network = DefaultRedisNetwork
}
if c . Addr == "" {
c . Addr = DefaultRedisAddr
}
if c . MaxActive == 0 {
c . MaxActive = 10
}
if c . Delim == "" {
c . Delim = DefaultDelim
}
2019-08-20 11:29:54 +02:00
var options [ ] radix . DialOpt
2019-06-22 13:46:20 +02:00
2020-05-17 02:25:32 +02:00
if c . TLSConfig != nil {
options = append ( options , radix . DialUseTLS ( c . TLSConfig ) )
}
2019-08-20 11:29:54 +02:00
if c . Password != "" {
options = append ( options , radix . DialAuthPass ( c . Password ) )
}
if c . Timeout > 0 {
options = append ( options , radix . DialTimeout ( c . Timeout ) )
}
2019-06-22 13:46:20 +02:00
2019-08-20 11:29:54 +02:00
if c . Database != "" { // *dialOpts.selectDb is not exported on the 3rd-party library,
// but on its `DialSelectDB` option it does this:
// do.selectDB = strconv.Itoa(db) -> (string to int)
// so we can pass that string as int and it should work.
dbIndex , err := strconv . Atoi ( c . Database )
if err == nil {
options = append ( options , radix . DialSelectDB ( dbIndex ) )
2019-06-22 13:46:20 +02:00
}
2019-08-20 11:29:54 +02:00
}
var connFunc radix . ConnFunc
2020-06-27 12:53:16 +02:00
/ * Note ( @ kataras ) : according to # 1545 the below does NOT work , and we should
use the Cluster instance itself to fire requests .
We need a separate ` radix.Cluster ` instance to do the calls ,
fortunally both Pool and Cluster implement the same Do and Close methods we need ,
so a new ` radixPool ` interface to remove any dupl code is used instead .
if len ( c . Clusters ) > 0 {
cluster , err := radix . NewCluster ( c . Clusters )
if err != nil {
// maybe an
// ERR This instance has cluster support disabled
return err
}
connFunc = func ( network , addr string ) ( radix . Conn , error ) {
topo := cluster . Topo ( )
node := topo [ rand . Intn ( len ( topo ) ) ]
return radix . Dial ( c . Network , node . Addr , options ... )
}
} else {
* /
2020-06-27 11:33:02 +02:00
connFunc = func ( network , addr string ) ( radix . Conn , error ) {
return radix . Dial ( c . Network , c . Addr , options ... )
2019-06-22 13:46:20 +02:00
}
2020-06-27 12:53:16 +02:00
var pool radixPool
if len ( c . Clusters ) > 0 {
poolFunc := func ( network , addr string ) ( radix . Client , error ) {
return radix . NewPool ( network , addr , c . MaxActive , radix . PoolConnFunc ( connFunc ) )
}
cluster , err := radix . NewCluster ( c . Clusters , radix . ClusterPoolFunc ( poolFunc ) )
if err != nil {
return err
}
pool = cluster
} else {
p , err := radix . NewPool ( c . Network , c . Addr , c . MaxActive , radix . PoolConnFunc ( connFunc ) )
if err != nil {
return err
}
pool = p
2019-06-22 13:46:20 +02:00
}
r . Connected = true
r . pool = pool
2019-08-06 17:40:13 +02:00
r . Config = c
2019-06-22 13:46:20 +02:00
return nil
}
// PingPong sends a ping and receives a pong, if no pong received then returns false and filled error
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) PingPong ( ) ( bool , error ) {
2019-06-22 13:46:20 +02:00
var msg string
err := r . pool . Do ( radix . Cmd ( & msg , "PING" ) )
if err != nil {
return false , err
}
return ( msg == "PONG" ) , nil
}
2019-08-06 17:40:13 +02:00
// CloseConnection closes the redis connection.
func ( r * RadixDriver ) CloseConnection ( ) error {
2019-06-22 13:46:20 +02:00
if r . pool != nil {
return r . pool . Close ( )
}
return ErrRedisClosed
}
// Set sets a key-value to the redis store.
2019-08-06 17:40:13 +02:00
// The expiration is setted by the secondsLifetime.
func ( r * RadixDriver ) Set ( key string , value interface { } , secondsLifetime int64 ) error {
2019-06-22 13:46:20 +02:00
var cmd radix . CmdAction
// if has expiration, then use the "EX" to delete the key automatically.
if secondsLifetime > 0 {
2020-07-19 06:09:14 +02:00
cmd = radix . FlatCmd ( nil , "SETEX" , key , secondsLifetime , value )
2019-06-22 13:46:20 +02:00
} else {
2020-07-19 06:09:14 +02:00
cmd = radix . FlatCmd ( nil , "SET" , key , value ) // MSET same performance...
2019-06-22 13:46:20 +02:00
}
return r . pool . Do ( cmd )
}
// Get returns value, err by its key
2019-08-17 09:06:20 +02:00
// returns nil and a filled error if something bad happened.
2020-07-19 06:09:14 +02:00
func ( r * RadixDriver ) Get ( key string /* full key */ ) ( interface { } , error ) {
2019-06-22 13:46:20 +02:00
var redisVal interface { }
mn := radix . MaybeNil { Rcv : & redisVal }
2020-07-19 06:09:14 +02:00
err := r . pool . Do ( radix . Cmd ( & mn , "GET" , key ) )
2019-06-22 13:46:20 +02:00
if err != nil {
return nil , err
}
if mn . Nil {
2019-10-24 17:57:05 +02:00
return nil , fmt . Errorf ( "%s: %w" , key , ErrKeyNotFound )
2019-06-22 13:46:20 +02:00
}
return redisVal , nil
}
// TTL returns the seconds to expire, if the key has expiration and error if action failed.
// Read more at: https://redis.io/commands/ttl
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) TTL ( key string ) ( seconds int64 , hasExpiration bool , found bool ) {
2019-06-22 13:46:20 +02:00
var redisVal interface { }
2020-07-19 06:09:14 +02:00
err := r . pool . Do ( radix . Cmd ( & redisVal , "TTL" , key ) )
2019-06-22 13:46:20 +02:00
if err != nil {
return - 2 , false , false
}
seconds = redisVal . ( int64 )
// if -1 means the key has unlimited life time.
hasExpiration = seconds > - 1
// if -2 means key does not exist.
found = seconds != - 2
return
}
2020-07-19 06:09:14 +02:00
func ( r * RadixDriver ) updateTTLConn ( key string /* full key */ , newSecondsLifeTime int64 ) error {
2019-06-22 13:46:20 +02:00
var reply int
2020-07-19 06:09:14 +02:00
err := r . pool . Do ( radix . FlatCmd ( & reply , "EXPIRE" , key , newSecondsLifeTime ) )
2019-06-22 13:46:20 +02:00
if err != nil {
return err
}
// https://redis.io/commands/expire#return-value
//
// 1 if the timeout was set.
// 0 if key does not exist.
if reply == 1 {
return nil
} else if reply == 0 {
return fmt . Errorf ( "unable to update expiration, the key '%s' was stored without ttl" , key )
} // do not check for -1.
return nil
}
// UpdateTTL will update the ttl of a key.
// Using the "EXPIRE" command.
// Read more at: https://redis.io/commands/expire#refreshing-expires
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) UpdateTTL ( key string , newSecondsLifeTime int64 ) error {
2019-06-22 13:46:20 +02:00
return r . updateTTLConn ( key , newSecondsLifeTime )
}
// UpdateTTLMany like `UpdateTTL` but for all keys starting with that "prefix",
// it is a bit faster operation if you need to update all sessions keys (although it can be even faster if we used hash but this will limit other features),
// look the `sessions/Database#OnUpdateExpiration` for example.
2020-07-19 06:09:14 +02:00
func ( r * RadixDriver ) UpdateTTLMany ( prefix string /* prefix is the sid */ , newSecondsLifeTime int64 ) error {
keys , err := r . getKeys ( "0" , prefix , true )
2019-06-22 13:46:20 +02:00
if err != nil {
return err
}
for _ , key := range keys {
if err = r . updateTTLConn ( key , newSecondsLifeTime ) ; err != nil { // fail on first error.
return err
}
}
return err
}
// GetAll returns all redis entries using the "SCAN" command (2.8+).
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) GetAll ( ) ( interface { } , error ) {
2019-06-22 13:46:20 +02:00
var redisVal [ ] interface { }
mn := radix . MaybeNil { Rcv : & redisVal }
err := r . pool . Do ( radix . Cmd ( & mn , "SCAN" , strconv . Itoa ( 0 ) ) ) // 0 -> cursor
if err != nil {
return nil , err
}
if mn . Nil {
return nil , err
}
return redisVal , nil
}
2019-08-22 13:46:39 +02:00
type scanResult struct {
cur string
keys [ ] string
}
2019-06-22 13:46:20 +02:00
2019-08-22 13:46:39 +02:00
func ( s * scanResult ) UnmarshalRESP ( br * bufio . Reader ) error {
var ah resp2 . ArrayHeader
if err := ah . UnmarshalRESP ( br ) ; err != nil {
return err
} else if ah . N != 2 {
return errors . New ( "not enough parts returned" )
}
2019-06-22 13:46:20 +02:00
2019-08-22 13:46:39 +02:00
var c resp2 . BulkString
if err := c . UnmarshalRESP ( br ) ; err != nil {
return err
2019-06-22 13:46:20 +02:00
}
2019-08-20 13:19:42 +02:00
2019-08-22 13:46:39 +02:00
s . cur = c . S
s . keys = s . keys [ : 0 ]
return ( resp2 . Any { I : & s . keys } ) . UnmarshalRESP ( br )
}
2020-07-19 06:09:14 +02:00
func ( r * RadixDriver ) getKeys ( cursor , prefix string , includeSID bool ) ( [ ] string , error ) {
2019-08-22 13:46:39 +02:00
var res scanResult
2020-07-19 06:09:14 +02:00
if ! includeSID {
prefix += r . Config . Delim // delim can be used for fast matching of only keys.
}
pattern := prefix + "*"
err := r . pool . Do ( radix . Cmd ( & res , "SCAN" , cursor , "MATCH" , pattern , "COUNT" , "300000" ) )
2019-08-22 13:46:39 +02:00
if err != nil {
2019-06-22 13:46:20 +02:00
return nil , err
}
2020-07-19 06:09:14 +02:00
if len ( res . keys ) == 0 {
2020-06-19 04:54:21 +02:00
return nil , nil
}
2020-07-19 06:09:14 +02:00
keys := res . keys [ 0 : ]
2019-08-22 13:46:39 +02:00
if res . cur != "0" {
2020-07-19 06:09:14 +02:00
moreKeys , err := r . getKeys ( res . cur , prefix , includeSID )
2019-08-22 13:46:39 +02:00
if err != nil {
return nil , err
}
keys = append ( keys , moreKeys ... )
}
2019-06-22 13:46:20 +02:00
return keys , nil
}
// GetKeys returns all redis keys using the "SCAN" with MATCH command.
// Read more at: https://redis.io/commands/scan#the-match-option.
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) GetKeys ( prefix string ) ( [ ] string , error ) {
2020-07-19 06:09:14 +02:00
return r . getKeys ( "0" , prefix , false )
2019-06-22 13:46:20 +02:00
}
// Delete removes redis entry by specific key
2019-08-06 17:40:13 +02:00
func ( r * RadixDriver ) Delete ( key string ) error {
2020-07-19 06:09:14 +02:00
err := r . pool . Do ( radix . Cmd ( nil , "DEL" , key ) )
2019-06-22 13:46:20 +02:00
return err
}