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