github.com/gogf/gf/v2@v2.7.4/database/gredis/gredis_redis.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gredis
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/gogf/gf/v2/container/gvar"
    13  	"github.com/gogf/gf/v2/errors/gcode"
    14  	"github.com/gogf/gf/v2/errors/gerror"
    15  )
    16  
    17  // Redis client.
    18  type Redis struct {
    19  	config *Config
    20  	localAdapter
    21  	localGroup
    22  }
    23  
    24  type (
    25  	localGroup struct {
    26  		localGroupGeneric
    27  		localGroupHash
    28  		localGroupList
    29  		localGroupPubSub
    30  		localGroupScript
    31  		localGroupSet
    32  		localGroupSortedSet
    33  		localGroupString
    34  	}
    35  	localAdapter        = Adapter
    36  	localGroupGeneric   = IGroupGeneric
    37  	localGroupHash      = IGroupHash
    38  	localGroupList      = IGroupList
    39  	localGroupPubSub    = IGroupPubSub
    40  	localGroupScript    = IGroupScript
    41  	localGroupSet       = IGroupSet
    42  	localGroupSortedSet = IGroupSortedSet
    43  	localGroupString    = IGroupString
    44  )
    45  
    46  const (
    47  	errorNilRedis = `the Redis object is nil`
    48  )
    49  
    50  const errorNilAdapter = `redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis`
    51  
    52  // initGroup initializes the group object of redis.
    53  func (r *Redis) initGroup() *Redis {
    54  	r.localGroup = localGroup{
    55  		localGroupGeneric:   r.localAdapter.GroupGeneric(),
    56  		localGroupHash:      r.localAdapter.GroupHash(),
    57  		localGroupList:      r.localAdapter.GroupList(),
    58  		localGroupPubSub:    r.localAdapter.GroupPubSub(),
    59  		localGroupScript:    r.localAdapter.GroupScript(),
    60  		localGroupSet:       r.localAdapter.GroupSet(),
    61  		localGroupSortedSet: r.localAdapter.GroupSortedSet(),
    62  		localGroupString:    r.localAdapter.GroupString(),
    63  	}
    64  	return r
    65  }
    66  
    67  // SetAdapter changes the underlying adapter with custom adapter for current redis client.
    68  func (r *Redis) SetAdapter(adapter Adapter) {
    69  	if r == nil {
    70  		panic(gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis))
    71  	}
    72  	r.localAdapter = adapter
    73  }
    74  
    75  // GetAdapter returns the adapter that is set in current redis client.
    76  func (r *Redis) GetAdapter() Adapter {
    77  	if r == nil {
    78  		return nil
    79  	}
    80  	return r.localAdapter
    81  }
    82  
    83  // Conn retrieves and returns a connection object for continuous operations.
    84  // Note that you should call Close function manually if you do not use this connection any further.
    85  func (r *Redis) Conn(ctx context.Context) (Conn, error) {
    86  	if r == nil {
    87  		return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
    88  	}
    89  	if r.localAdapter == nil {
    90  		return nil, gerror.NewCode(gcode.CodeNecessaryPackageNotImport, errorNilAdapter)
    91  	}
    92  	return r.localAdapter.Conn(ctx)
    93  }
    94  
    95  // Do send a command to the server and returns the received reply.
    96  // It uses json.Marshal for struct/slice/map type values before committing them to redis.
    97  func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
    98  	if r == nil {
    99  		return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
   100  	}
   101  	if r.localAdapter == nil {
   102  		return nil, gerror.NewCodef(gcode.CodeMissingConfiguration, errorNilAdapter)
   103  	}
   104  	return r.localAdapter.Do(ctx, command, args...)
   105  }
   106  
   107  // MustConn performs as function Conn, but it panics if any error occurs internally.
   108  func (r *Redis) MustConn(ctx context.Context) Conn {
   109  	c, err := r.Conn(ctx)
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  	return c
   114  }
   115  
   116  // MustDo performs as function Do, but it panics if any error occurs internally.
   117  func (r *Redis) MustDo(ctx context.Context, command string, args ...interface{}) *gvar.Var {
   118  	v, err := r.Do(ctx, command, args...)
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  	return v
   123  }
   124  
   125  // Close closes current redis client, closes its connection pool and releases all its related resources.
   126  func (r *Redis) Close(ctx context.Context) error {
   127  	if r == nil || r.localAdapter == nil {
   128  		return nil
   129  	}
   130  	return r.localAdapter.Close(ctx)
   131  }