首頁 > 後端開發 > Golang > 如何處理 Golang 快取失效的情況?

如何處理 Golang 快取失效的情況?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2024-06-02 17:09:02
原創
747 人瀏覽過

在處理 Golang 中的快取失效時,可以遵循以下策略:使用時間戳標記快取項,並在過期時取得新資料。使用鎖,當協程取得快取項目時對快取進行加鎖,並在快取項目不存在或過期時解鎖快取並取得新資料。

如何处理 Golang 缓存失效的情况?

如何處理 Golang 快取失效的情況?

在 Golang 程式中使用快取時,應對快取失效的情況至關重要,以確保資料的一致性和可靠性。以下是兩種處理策略:

1. 使用時間戳記

  • #將快取項目與時間戳記關聯。
  • 當快取項目過期時,取得新資料並更新快取。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

type CacheItem struct {

    Value interface{}

    Timestamp time.Time

}

 

var cache = make(map[string]CacheItem)

 

func SetCache(key string, value interface{}) {

    cache[key] = CacheItem{Value: value, Timestamp: time.Now()}

}

 

func GetCache(key string) (interface{}, bool) {

    item, ok := cache[key]

    if ok && time.Since(item.Timestamp) < time.Second*30 {

        return item.Value, true

    }

    return nil, false

}

登入後複製

2. 使用鎖定

  • 當一個協程取得快取項目時,對快取進行加鎖。
  • 如果快取項目不存在或過期,則解鎖快取並取得新資料。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

var cache = sync.Map{}

 

func SetCache(key string, value interface{}) {

    cache.Store(key, value)

}

 

func GetCache(key string) (interface{}, bool) {

    if value, ok := cache.Load(key); ok {

        return value, true

    }

 

    lock := sync.Mutex{}

    lock.Lock()

    defer lock.Unlock()

 

    if value, ok := cache.Load(key); ok {

        return value, true

    }

 

    newValue, err := fetchNewValue(key)

    if err == nil {

        cache.Store(key, newValue)

    }

 

    return newValue, err == nil

}

登入後複製

實戰案例

假設我們在一個 RESTful API 中使用快取來儲存使用者的詳細資訊。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

package main

 

import (

    "encoding/json"

    "fmt"

    "net/http"

    "time"

 

    "github.com/go-redis/redis/v8"

)

 

var redisClient = redis.NewClient(&redis.Options{})

 

type User struct {

    ID   int

    Name string

}

 

func main() {

    // 添加缓存处理

    http.HandleFunc("/users/:id", func(w http.ResponseWriter, r *http.Request) {

        var user User

        id := r.URL.Path[len("/users/"):]

 

        // 尝试从缓存中获取用户数据

        cachedUser, ok := GetCache(id)

        if ok {

            // 缓存命中

            fmt.Fprintf(w, "User from cache: %+v", cachedUser)

            return

        }

 

        // 缓存未命中

        // 从数据库中获取用户数据

        err := db.Get(id, &user)

        if err != nil {

            // 数据库中不存在此用户

            fmt.Fprintf(w, "User not found")

            return

        }

 

        // 设置缓存,有效期为 30 秒

        SetCache(id, user, time.Second*30)

        fmt.Fprintf(w, "User from database: %+v", user)

    })

 

    http.ListenAndServe(":8080", nil)

}

 

func GetCache(key string) (User, bool) {

    result, err := redisClient.Get(key).Bytes()

    if err != nil {

        return User{}, false

    }

 

    var user User

    err = json.Unmarshal(result, &user)

    if err != nil {

        return User{}, false

    }

 

    return user, true

}

 

func SetCache(key string, user User, expiration time.Duration) {

    jsonBytes, err := json.Marshal(user)

    if err != nil {

        return

    }

 

    err = redisClient.Set(key, jsonBytes, expiration).Err()

    if err != nil {

        return

    }

}

登入後複製

以上是如何處理 Golang 快取失效的情況?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
golang web mvc 框架該怎麼選
來自於 1970-01-01 08:00:00
0
0
0
使用 golang 還有必要使用 nginx 麼?
來自於 1970-01-01 08:00:00
0
0
0
golang - goroutine 洩漏
來自於 1970-01-01 08:00:00
0
0
0
golang - mac配置gocode + vim自動補齊
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板