How to store and cache local tools in the distributed deployment environment of the gin framework in Go language - Redis container implementation (11)

Redis is an open source in-memory data storage system that provides a fast and scalable key-value storage solution.

Redis supports a variety of data structures, including strings, hash tables, lists, sets, and sorted sets. It is commonly used for several purposes including caching, message brokering, queuing, and real-time applications.

The main features of Redis include:
1. Fast: Redis is stored in memory and can perform read and write operations at the microsecond level. It uses efficient data structures and algorithms to provide fast performance.
2. Persistence: Redis supports data persistence and can save data to disk so that it can be restored after restarting.
3. Diverse data structures: Redis supports a variety of data structures, such as strings, hash tables, lists, sets and ordered sets. This makes it very flexible and can be adapted to a variety of different application scenarios.
4. High availability: Redis supports master-slave replication and distributed architecture, which can provide high availability and scalability.
5. Publish/Subscribe: Redis supports publish/subscribe mode, which can be used to build a messaging system to achieve real-time data push and event notification.

To use Redis, you need to install the Redis server and use the corresponding client library to connect to and operate the Redis server. Common Redis client libraries include the official Redis-cli of Redis, redis-py of Python, ioredis of Node.js, etc.

How to install Redis server in window environment?

 

If you want to use a Redis installer to simplify the installation process of Redis, you can try to use the Microsoft Developer Toolkit (MSOpenTech Redis) officially recommended by Redis. Here are the steps to install Redis on Windows via MSOpenTech Redis Installer:

1. Go to the official Redis website: https://redis.io/ and click on the " Download " menu.
2. Under the Windows column, click the "MS Open Tech" link.
3. On the GitHub page, find and click the latest version of the `Redis-xxxzip` file to download the Redis installer. Please note that `xxx` represents the version number.
4. After the download is complete, unzip the file to the location you want to install it (for example `C:\Redis`).
5. In the Redis decompression directory, you can see a folder named `Redis-xxx` (version number) and enter the folder.
6. In this folder, you can find a batch file named `install_service.bat`. Run this file with administrator rights.
7. The installer will prompt you to enter the port number used for the Redis service. The default is 6379 (you can also choose another port number).
8. Wait for the installer to complete the installation of Redis.
9. After the installation is complete, you can use the Windows "Services" manager to check and start the Redis service, or use the `redis-cli` command on the command line to interact with Redis.

This installer makes it easier for you to install and manage Redis on Windows. Hope this method is helpful to you. If you have any further questions, please feel free to ask.

As a supplement, if the above installation method is not suitable for you, you can take a look:

 Index of /release/

 

In fact, Redis officially does not provide an installation file named "redis-x86-6x.msi". There are two main versions of the installation program officially provided by Redis: 32-bit and 64-bit.

To install Redis 6.x version on 32-bit Windows operating system, please follow the steps below:

  1. Go to the Redis official website: https://redis.io/ and click the "Download" menu.

  2. Under the Windows column, find and click the "MS Open Tech" link.

  3. On the GitHub page, find and click the latest version of Redis-x.x.x.zipthe file to download the Redis compressed package. Please note that x.x.xrepresents the version number.

  4. Once the download is complete, unzip the file to the location you wish to install it (for example  C:\Redis).

  5. Open the unzipped Redis folder and you will see some files and folders. Among them, redis-server.exeit is the server program of Redis.

  6. Hold down the Shift key, then right-click in an empty space and select "Open command window here" or "Open PowerShell window here."

  7. In the command line or PowerShell window that pops up, run the following command to start the Redis server:

    redis-server.exe redis.windows.conf
    

    This will start the Redis server on the default port (6379).

  8. After the Redis server is successfully started, some log information will be displayed in the window, including the Redis version number, port number, etc.

For 64-bit Windows operating systems, you can follow the same steps to install Redis. You only need to download the Redis compressed package for 64-bit systems.

Note:  Download window 5.0.14 and install it in one piece

Remember to configure the system environment variables, otherwise it cannot be used. For usage effects, please refer to:

 This is to determine whether the redis service is started after installation. It can also be started with the net start redis command line.

In this case, the installation is successful. 

Through Redis, you can implement various operations related to data, such as reading and writing data, setting expiration time, adding, deleting, modifying, and checking operations, etc. It also provides advanced features such as transactions, Lua scripts, pipelines, sorting, and range queries. Whether it is building a cache system or implementing functions such as real-time statistics, rankings, or message queues, Redis is a very powerful and flexible tool.

So how to use Redis in the gin box?

1. Replace the original cookie container middleware with the one created by Redis (go get -u github.com/go-redis/redis/v5)

 go get github.com/gomodule/redigo@latest

go get github.com/gin-contrib/sessions/redis

After installation, the code inside the main.go file

package main

import (
	"project/routers"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	// Create template for router html template
	r.LoadHTMLGlob("*templates/**/**/*")

	// 设置 session 中间件
	// store := cookie.NewStore([]byte("secret")) // 设置 Session 密钥
	// r.Use(sessions.Sessions("mysession", store))

	// 换成 redis 容器存储
	store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret")) // redis 容器注册中间件
	/*
		一个参数代表了位数int
		第二参数代表传输协议(tcp、udp)
		第三参数代表 redis的链接地址,如果是外网就是ip地址加端口号
		第四参数代表 redis服务的访问密码,如果本地未设置那么是空
		第五参数代表 redis容器的保存数据的秘钥(这里的值用来内部加密了!)
	*/
	r.Use(sessions.Sessions("mysessionRedis", store))

	r.GET("/", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{
			"message": "Hello, world!",
		})
	})

	routers.AdminRoutersInit(r)
	routers.DefaultRoutersInit(r)

	r.Run(":8081")

}

The above is that after changing the storage data container, the middleware part of the storage transformation and other session parts are not used. This change is more in line with the distributed system access of big data.

 

Hope this helps! If you have any other questions, please feel free to ask.

Guess you like

Origin blog.csdn.net/A_LWIEUI_Learn/article/details/131585066