Introduction

The Redis client is implemented by the gredis component, which adopts a connection pool design at the bottom layer.

NoSQL Redis - 图1tip

To ensure universality and scalability, the gredis component uses the command channel method to execute Redis operations. When you are unsure how to pass parameters for the command channel, you can refer to the parameter passing of the terminal command line. In other words, all operations remain consistent with the parameter passing of command lines.

Usage:

Installation:

  1. go get -u github.com/gogf/gf/contrib/nosql/redis/v2

Reference:

  1. import (
  2. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  3. // other imported packages.
  4. )

Interface Documentation:

Brief Introduction:

gredis uses a connection pool to manage Redis connections. You can manage the connection pool properties through the Config configuration object or the Set* methods, and obtain pool statistics through the Stats method. gredis uses an interface-based design to decouple the underlying dependency on redis. It implements over 100+ common methods using a community component approach and provides a grouping method for managing interfaces.

NoSQL Redis - 图2warning

The gredis.Redis client object provides a Close method, which is used to close the Redis client (and also the client’s connection pool), not the connection object. Developers generally will not use it; non-advanced users should not use it.

Features

gredis has the following notable features:

  • Easy to use, powerful features
  • Unified configuration for components
  • Provides community component implementation of 100+ common methods
  • Supports both single instance and cluster operations
  • Supports all features of Redis services
  • Supports OpenTelemetry observability
  • Supports singleton objects and dynamic object creation
  • Interface design with high flexibility and extensibility

Documentation

📄️ Redis - ConfigurationUse the gredis component in the GoFrame framework for Redis configuration management. We recommend managing Redis configurations through configuration files, supporting both single instance and cluster configurations. Additionally, detailed instructions on various configuration items are provided, along with related code examples for reference.

📄️ Redis - ExamplesExamples of performing basic operations in Redis using the GoFrame framework, including Set/Get, SetEx, HSet/HGetAll, and HMSet/HMGet operations. These code examples demonstrate how to store and retrieve data through the Redis module of the GoFrame framework, suitable for beginners to learn how to implement Redis functions in the GoFrame framework environment. This example also reminds users that HMSET has been deprecated in Redis version 4.0.0 and above; HSET should be used.

📄️ Redis - DoIn applications built with the GoFrame framework, interact with Redis commands and automatically serialize and deserialize data. First, we explain the powerful extensibility of the Do method, which allows the execution of any Redis command. Then we show how to use map and struct to access and store data and simplify programming with JSON serialization. By combining the GoFrame framework and Redis, developers can manage data more efficiently.

📄️ Redis - ConnUse the Conn object of Redis in the GoFrame framework for long connection operations, such as publish/subscribe functions. Obtain the connection object through a connection pool for operations, while paying attention to connection object timeout issues and closure operations after use. The example code demonstrates implementing the publish/subscribe pattern through Conn, with the program printing data obtained from the Redis Server on the terminal.

📄️ Redis - InterfaceImplement an interface-designed Redis component using gredis in the GoFrame framework, which has strong flexibility and extensibility. By implementing a custom Redis Adapter, you can easily override the default implementation methods. The text provides detailed examples showing how to achieve log printing in the custom Do method and use it in business operations.