Home  >  Article  >  Backend Development  >  The practice of using cache to speed up database access efficiency in Golang.

The practice of using cache to speed up database access efficiency in Golang.

WBOY
WBOYOriginal
2023-06-20 10:12:101542browse

Practice of using cache to accelerate database access efficiency in Golang

As web applications become more and more complex, access to the database becomes more and more frequent. Accessing the database is usually a very time-consuming operation, especially when the amount of data is large. In order to improve the efficiency of database access, strategies such as caching can be used to optimize database access.

This article will introduce the practice of using cache to speed up database access in Golang. We will use Golang as the development language, Redis as the cache server, and MySQL as the database server for experiments.

1. Set up the environment

Before we start, we need to set up the environment. First install Golang and MySQL and Redis servers, which will not be described here.

Then install the Go driver for local Redis and MySQL in Golang:

go get github.com/go-redis/redis/v8
go get github.com/go-sql-driver/mysql

2. Write the code

Next, we write the code to implement caching to accelerate database access.

The first is the code for database access. We defined a global variable called DB for connecting to MySQL. Then, we defined a function getUserByID to query a user's information from MySQL:

package main
 
import (
   "database/sql"
   "fmt"
   "log"
 
   _ "github.com/go-sql-driver/mysql"
)
 
var DB *sql.DB
 
type User struct {
   ID       int
   Username string
   Password string
   Age      int
}
 
func init() {
   db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test?charset=utf8")
   if err != nil {
      log.Fatal("Open mysql failed,err:", err)
      return
   }
   DB = db
   fmt.Println("Connect to mysql success")
}
 
func getUserByID(id int) (*User, error) {
   var user User
   query := "SELECT id, username, password, age FROM users WHERE id=?"
   err := DB.QueryRow(query, id).Scan(&user.ID, &user.Username, &user.Password, &user.Age)
   if err != nil {
      log.Println(err)
      return nil, err
   }
   return &user, nil
}

Then, we added caching logic to the getUserByID function. Specifically, we first try to read the requested user information from the Redis cache through the getUserByID function. If there is no information record for the user in Redis, the user information is read from MySQL and stored in Redis for next access. If the user information is recorded in Redis, the user information is returned directly from Redis:

package main
 
import (
   "database/sql"
   "encoding/json"
   "fmt"
   "log"
   "strconv"
 
   "github.com/go-redis/redis/v8"
   _ "github.com/go-sql-driver/mysql"
)
 
var DB *sql.DB
var RedisClient *redis.Client
 
type User struct {
   ID       int
   Username string
   Password string
   Age      int
}
 
func init() {
   db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test?charset=utf8")
   if err != nil {
      log.Fatal("Open mysql failed,err:", err)
      return
   }
   DB = db
   fmt.Println("Connect to mysql success")
 
   RedisClient = redis.NewClient(&redis.Options{
      Addr: "127.0.0.1:6379",
   })
   pong, err := RedisClient.Ping(RedisClient.Context()).Result()
   if err != nil {
      panic(err)
      return
   }
   fmt.Println("Connect to redis success: ", pong)
}
 
func getUserByID(id int) (*User, error) {
   var user User
   key := "user-" + strconv.Itoa(id)
 
   // 1.尝试从Redis中读取用户信息
   val, err := RedisClient.Get(RedisClient.Context(), key).Result()
   if err == redis.Nil {
      fmt.Println("Cache miss")
   } else if err != nil {
      log.Println("Get from Redis fail:", err)
   } else {
      fmt.Println("Get from Redis:", val)
      if err := json.Unmarshal([]byte(val), &user); err != nil { // 将json字符串转换为结构体
         log.Panicln("Unmarshal to user fail:", err)
      }
      return &user, nil
   }
 
   // 2.如果Redis中没有,从MySQL中查询
   query := "SELECT id, username, password, age FROM users WHERE id=?"
   err = DB.QueryRow(query, id).Scan(&user.ID, &user.Username, &user.Password, &user.Age)
   if err != nil {
      log.Println(err)
      return nil, err
   }
 
   // 3.然后更新Redis缓存
   val, err = json.Marshal(user) // 将结构体转换为json字符串
   if err != nil {
      log.Panicln("Marshal user fail:", err)
   }
   err = RedisClient.Set(RedisClient.Context(), key, val, 0).Err()
   if err != nil {
      log.Panicln("Cache to Redis fail:", err)
   }
   return &user, nil
}

3. Test

We have completed the writing of the cache logic. Now let's test this code and see how it works.

First of all, our test program needs to read user information from MySQL for the first time:

func main() {
   id := 1
   user, err := getUserByID(id)
   if err != nil {
      log.Fatal(err)
      return
   }
   fmt.Printf("User info: id=%d, username=%s, password=%s, age=%d
",
      user.ID, user.Username, user.Password, user.Age)
}

Run the program, the output is as follows:

Cache miss
User info: id=1, username=kirito, password=123456, age=18

You can see that the program reads from MySQL The user information is taken out and stored in the Redis cache.

After querying the same user for the second time, the program will read the user information from Redis instead of accessing the MySQL database:

func main() {
   id := 1
   user, err := getUserByID(id)
   if err != nil {
      log.Fatal(err)
      return
   }
   fmt.Printf("User info: id=%d, username=%s, password=%s, age=%d
",
      user.ID, user.Username, user.Password, user.Age)
}

Run the program and the output will be as follows:

Get from Redis: {"ID":1,"Username":"kirito","Password":"123456","Age":18}
User info: id=1, username=kirito, password=123456, age=18

You can see that the program reads user information directly from Redis without accessing the MySQL database, which proves the practicality of caching.

Summary

In this article, we introduced the practice of using the Redis caching mechanism in Golang to optimize database access efficiency. By writing the getUserByID function, when querying user information, we first try to read the information from Redis to speed up the query. If the user information does not exist in Redis, we read it from MySQL and store it in Redis. After the data is cached, when the program accesses the same data multiple times, it can read directly from the cache without accessing the database each time.

It should be noted that the use of cache needs to avoid the generation of dirty data. For CRUD operations on data, the cache also needs to follow the data operations to ensure the consistency of the data in the cache and database.

In general, using the caching mechanism can greatly improve the performance of the program, and is especially effective when processing large amounts of data. This strategy is also very practical in high-concurrency systems, and developers are recommended to try it.

The above is the detailed content of The practice of using cache to speed up database access efficiency in Golang.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn