github.com/wangyougui/gf/v2@v2.6.5/database/gredis/gredis_adapter.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 ) 14 15 // Adapter is an interface for universal redis operations. 16 type Adapter interface { 17 AdapterGroup 18 AdapterOperation 19 } 20 21 // AdapterGroup is an interface managing group operations for redis. 22 type AdapterGroup interface { 23 GroupGeneric() IGroupGeneric 24 GroupHash() IGroupHash 25 GroupList() IGroupList 26 GroupPubSub() IGroupPubSub 27 GroupScript() IGroupScript 28 GroupSet() IGroupSet 29 GroupSortedSet() IGroupSortedSet 30 GroupString() IGroupString 31 } 32 33 // AdapterOperation is the core operation functions for redis. 34 // These functions can be easily overwritten by custom implements. 35 type AdapterOperation interface { 36 // Do send a command to the server and returns the received reply. 37 // It uses json.Marshal for struct/slice/map type values before committing them to redis. 38 Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) 39 40 // Conn retrieves and returns a connection object for continuous operations. 41 // Note that you should call Close function manually if you do not use this connection any further. 42 Conn(ctx context.Context) (conn Conn, err error) 43 44 // Close closes current redis client, closes its connection pool and releases all its related resources. 45 Close(ctx context.Context) (err error) 46 } 47 48 // Conn is an interface of a connection from universal redis client. 49 type Conn interface { 50 ConnCommand 51 52 // Do send a command to the server and returns the received reply. 53 // It uses json.Marshal for struct/slice/map type values before committing them to redis. 54 Do(ctx context.Context, command string, args ...interface{}) (result *gvar.Var, err error) 55 56 // Close puts the connection back to connection pool. 57 Close(ctx context.Context) (err error) 58 } 59 60 // ConnCommand is an interface managing some operations bound to certain connection. 61 type ConnCommand interface { 62 // Subscribe subscribes the client to the specified channels. 63 // https://redis.io/commands/subscribe/ 64 Subscribe(ctx context.Context, channel string, channels ...string) ([]*Subscription, error) 65 66 // PSubscribe subscribes the client to the given patterns. 67 // 68 // Supported glob-style patterns: 69 // - h?llo subscribes to hello, hallo and hxllo 70 // - h*llo subscribes to hllo and heeeello 71 // - h[ae]llo subscribes to hello and hallo, but not hillo 72 // 73 // Use \ to escape special characters if you want to match them verbatim. 74 // 75 // https://redis.io/commands/psubscribe/ 76 PSubscribe(ctx context.Context, pattern string, patterns ...string) ([]*Subscription, error) 77 78 // ReceiveMessage receives a single message of subscription from the Redis server. 79 ReceiveMessage(ctx context.Context) (*Message, error) 80 81 // Receive receives a single reply as gvar.Var from the Redis server. 82 Receive(ctx context.Context) (result *gvar.Var, err error) 83 }