首頁 > 後端開發 > Golang > 實現具有 TTL 和磁碟持久性的高效能緩存

實現具有 TTL 和磁碟持久性的高效能緩存

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2024-08-14 10:39:10
原創
917 人瀏覽過

Go High-Performance Cache with TTL and Disk Persistence

一、簡介

加速您的 Golang 項目,無需每次開始新項目時設定資料庫的麻煩。厭倦了從頭開始配置資料庫?只是為了面對新的問題嗎?別再猶豫了 在這篇部落格中,我們將研究 Golang 快取庫,它支援 TTL磁碟持久性雜湊資料類型

GoSwift

2.前提條件

  • Golang基礎知識
  • 了解典型快取的工作原理

三、特點

  1. 設定與取得指令
  2. 更新指令
  3. 刪除指令
  4. 存在指令
  5. 支援 TTL
  6. 支援磁碟保存(快照)
  7. 支援雜湊資料型別(Hset、Hget、HgetAll、HMset)
  8. 安全鎖定

設定和獲取命令

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import (

    "fmt"

    "github.com/leoantony72/goswift"

)

 

func main(){

    cache := goswift.NewCache()

 

    // Value 0 indicates no expiry

    cache.Set("key", "value", 0)

 

    val, err := cache.Get("key")

    if err !=nil{

        fmt.Println(err)

        return

    }

    fmt.Println("key", val)

}

登入後複製

更新命令

1

2

3

4

5

6

7

// Update value

// @Update(key string, val interface{}) error

err = cache.Update("key","value2")

if err != nil{

    fmt.Println(err)

    return

}

登入後複製

Del 指令 && 存在指令

1

2

3

4

5

6

7

8

// Delete command

// @Del(key string)

cache.Del("key")

 

// Exist command

// @Exists(key string) bool

value = cache.Exists("key")

fmt.Println(value) // returns false

登入後複製

支持TTL

1

2

3

4

5

6

7

8

9

10

// Set Value with Expiry

// @Set(key string, val interface{}, exp int)

// Here expiry is set to 1sec

cache.Set("key","value",1000)

 

// Hset command

// @Hset(key, field string, value interface{}, exp int)

// in this case the "key" expires in 1sec

cache.Hset("key","name","value",1000)

cache.Hset("key","age",18,1000)

登入後複製

支援Hash資料類型(Hset、Hget、HgetAll、HMset)

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

// Hset command

// @Hset(key, field string, value interface{}, exp int)

// in this case the "key" expires in 1sec

cache.Hset("key","name","value",1000)

cache.Hset("key","age",18,1000)

 

 

// HMset command

// @HMset(key string, d interface{}, exp int) error

// Set a Hash by passing a Struct/Map

// ---by passing a struct---

type Person struct{

    Name  string

    Age   int

    Place string

}

 

person1 := &Person{Name:"bob",Age:18,Place:"NYC"}

err = cache.HMset("key",person1)

if err != nil{

    fmt.Println(err)

    return

}

 

// ---by passing a map---

person2 := map[string]interface{Name:"john",Age:18,Place:"NYC"}

err = cache.HMset("key",person2)

if err != nil{

    fmt.Println(err)

    return

}

 

 

// Hget command

// @HGet(key, field string) (interface{}, error)

// get individual fields in Hash

data,err := cache.HGet("key","field")

if err != nil{

    fmt.Println(err)

    return

}

fmt.Println(data)

 

// HgetAll command

// @HGetAll(key string) (map[string]interface{}, error)

// gets all the fields with value in a hash key

// retuns a map[string]interface{}

data,err = cache.HGetAll("key")

if err != nil{

    fmt.Println(err)

    return

}

登入後複製

快照

1

2

3

4

5

opt := goswift.CacheOptions{

        EnableSnapshots:  true,

        SnapshotInterval: time.Second*5,

    }

c := goswift.NewCache(opt)

登入後複製

這將每 5 秒拍攝一次資料快照並將其儲存到 Snapshot.data 檔案中。預設情況下,快照已停用,如果未提供 SnapshotInterval,則預設值為 5 秒。

注意: 如果 EnableSnapshot 為 false,則不會匯入檔案中儲存的資料

錯誤處理

1

2

3

4

5

6

const (

    ErrKeyNotFound   = "key does not Exists"

    ErrFieldNotFound = "field does not Exists"

    ErrNotHashvalue  = "not a Hash value/table"

    ErrHmsetDataType = "invalid data type, Expected Struct/Map"

)

登入後複製

這些是編寫程式碼時可能出現的常見錯誤。這些變數為您提供了一個清晰且簡單的錯誤比較方法來確定錯誤。

1

2

3

4

5

6

data,err := cache.Get("key")

if err != nil {

    if err.Error() == goswift.ErrKeyNotFound {

        //do something

}

}   

登入後複製

快取過期的內部工作原理

每 3 秒就會呼叫 **sweaper ** 函數來清除雜湊表中的過期值。我們維護一個指向哈希圖的最小堆。頂部元素將是具有最小 TTL 的鍵。我們遍歷樹,直到 TTL 大於當前時間。

概括

我不建議您在生產中使用它! ! ,但請隨意在您的小型專案中使用它。請嘗試一下,如果遇到錯誤,請在 GitHub 儲存庫上提出問題。

電子郵件:leoantony102@gmail.com
Github:https://github.com/leoantony72
倉庫:https://github.com/leoantony72/goswift

以上是實現具有 TTL 和磁碟持久性的高效能緩存的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板