github.com/gogf/gf/v2@v2.7.4/database/gredis/gredis_redis_group_generic.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  	"time"
    12  
    13  	"github.com/gogf/gf/v2/container/gvar"
    14  )
    15  
    16  // IGroupGeneric manages generic redis operations.
    17  // Implements see redis.GroupGeneric.
    18  type IGroupGeneric interface {
    19  	Copy(ctx context.Context, source, destination string, option ...CopyOption) (int64, error)
    20  	Exists(ctx context.Context, keys ...string) (int64, error)
    21  	Type(ctx context.Context, key string) (string, error)
    22  	Unlink(ctx context.Context, keys ...string) (int64, error)
    23  	Rename(ctx context.Context, key, newKey string) error
    24  	RenameNX(ctx context.Context, key, newKey string) (int64, error)
    25  	Move(ctx context.Context, key string, db int) (int64, error)
    26  	Del(ctx context.Context, keys ...string) (int64, error)
    27  	RandomKey(ctx context.Context) (string, error)
    28  	DBSize(ctx context.Context) (int64, error)
    29  	Keys(ctx context.Context, pattern string) ([]string, error)
    30  	Scan(ctx context.Context, cursor uint64, option ...ScanOption) (uint64, []string, error)
    31  	FlushDB(ctx context.Context, option ...FlushOp) error
    32  	FlushAll(ctx context.Context, option ...FlushOp) error
    33  	Expire(ctx context.Context, key string, seconds int64, option ...ExpireOption) (int64, error)
    34  	ExpireAt(ctx context.Context, key string, time time.Time, option ...ExpireOption) (int64, error)
    35  	ExpireTime(ctx context.Context, key string) (*gvar.Var, error)
    36  	TTL(ctx context.Context, key string) (int64, error)
    37  	Persist(ctx context.Context, key string) (int64, error)
    38  	PExpire(ctx context.Context, key string, milliseconds int64, option ...ExpireOption) (int64, error)
    39  	PExpireAt(ctx context.Context, key string, time time.Time, option ...ExpireOption) (int64, error)
    40  	PExpireTime(ctx context.Context, key string) (*gvar.Var, error)
    41  	PTTL(ctx context.Context, key string) (int64, error)
    42  }
    43  
    44  // CopyOption provides options for function Copy.
    45  type CopyOption struct {
    46  	DB      int  // DB option allows specifying an alternative logical database index for the destination key.
    47  	REPLACE bool // REPLACE option removes the destination key before copying the value to it.
    48  }
    49  
    50  type FlushOp string
    51  
    52  const (
    53  	FlushAsync FlushOp = "ASYNC" // ASYNC: flushes the databases asynchronously
    54  	FlushSync  FlushOp = "SYNC"  // SYNC: flushes the databases synchronously
    55  )
    56  
    57  // ExpireOption provides options for function Expire.
    58  type ExpireOption struct {
    59  	NX bool // NX -- Set expiry only when the key has no expiry
    60  	XX bool // XX -- Set expiry only when the key has an existing expiry
    61  	GT bool // GT -- Set expiry only when the new expiry is greater than current one
    62  	LT bool // LT -- Set expiry only when the new expiry is less than current one
    63  }
    64  
    65  // ScanOption provides options for function Scan.
    66  type ScanOption struct {
    67  	Match string // Match -- Specifies a glob-style pattern for filtering keys.
    68  	Count int    // Count -- Suggests the number of keys to return per scan.
    69  	Type  string // Type -- Filters keys by their data type. Valid types are "string", "list", "set", "zset", "hash", and "stream".
    70  }
    71  
    72  // doScanOption is the internal representation of ScanOption.
    73  type doScanOption struct {
    74  	Match *string
    75  	Count *int
    76  	Type  *string
    77  }
    78  
    79  // ToUsedOption converts fields in ScanOption with zero values to nil. Only fields with values are retained.
    80  func (scanOpt *ScanOption) ToUsedOption() doScanOption {
    81  	var usedOption doScanOption
    82  
    83  	if scanOpt.Match != "" {
    84  		usedOption.Match = &scanOpt.Match
    85  	}
    86  	if scanOpt.Count != 0 {
    87  		usedOption.Count = &scanOpt.Count
    88  	}
    89  	if scanOpt.Type != "" {
    90  		usedOption.Type = &scanOpt.Type
    91  	}
    92  
    93  	return usedOption
    94  }