当前位置:网站首页>Simple and easy-to-use cache library gcache

Simple and easy-to-use cache library gcache

2022-06-25 22:22:00 fifteen billion two hundred and thirty-one million one hundred

1. Preface

When developing , If you need to cache data temporarily , According to a certain elimination strategy , that gcache You must not miss . gcache golang Cache library for . It supports extensible Cache, You can choose LFU,LRU、ARC Equal elimination algorithm .

2. characteristic

gcache There are many characteristics :

  • Support expiration elimination algorithm Cache, such as LFU, LRU and ARC.
  • Goroutine Security .
  • Support event handler , Eliminate 、 eliminate 、 add to .( Optional )
  • Auto load cache , If it doesn't exist .( Optional )
  • … ….

See... For more features :gcache

3. Fast installation

direct get You can use .

$ go get -u https://github.com/bluele/gcache

4. A simple example

package main

import (
 "github.com/bluele/gcache"
 "fmt"
)

func main() {
 gc := gcache.New(20).
  LRU().
  Build()
 gc.Set("key""ok")
 value, err := gc.Get("key")
 if err != nil {
  panic(err)
 }
 fmt.Println("Get:", value)
}

perform , The console output is as follows :

Get: ok

5. Example of setting obsolescence time

package main

import (
 "github.com/bluele/gcache"
 "fmt"
 "time"
)

func main() {
 gc := gcache.New(20).
  LRU().
  Build()
 gc.SetWithExpire("key""ok", time.Second*10)
 value, _ := gc.Get("key")
 fmt.Println("Get:", value)

 // Wait for value to expire
 time.Sleep(time.Second*10)

 value, err := gc.Get("key")
 if err != nil {
  panic(err)
 }
 fmt.Println("Get:", value)
}

perform , The console output is as follows :

Get: ok
panic: Key not found.

goroutine 1 [running]:
main.main()
        /Users/laocheng/work/code/market-data-backend/utils/t/2.go:22 +0x21b
exit status 2

You can see , Success at first ; But after the timeout is set , Expired delete , Unable to get .

6. Examples of other algorithms

6.1 The least used (LFU)

  func main() {
    // size: 10
    gc := gcache.New(10).
      LFU().
      Build()
    gc.Set("key""value")
  }

6.2 The least used recently (LRU)

  func main() {
    // size: 10
    gc := gcache.New(10).
      LRU().
      Build()
    gc.Set("key""value")
  }

6.3 Adaptive replacement cache (ARC) stay LRU and LFU Between them , To improve the overall results .

  func main() {
    // size: 10
    gc := gcache.New(10).
      ARC().
      Build()
    gc.Set("key""value")
  }

7. add to hanlder Use

func main() {
  gc := gcache.New(2).
    AddedFunc(func(key, value interface{}) {
      fmt.Println("added key:", key)
    }).
    Build()
  for i := 0; i < 3; i++ {
    gc.Set(i, i*i)
  }
}

perform , The console output is as follows :

added key: 0
added key: 1
added key: 2

Can be in set It's time to do some extra processing .

6. summary

gcache It's a very simple , Easy to use cache Library , It supports LFU,LRU、ARC Equal elimination algorithm . If you have this requirement during development , Try it , I'm sure I'll like it !

Reference material :

原网站

版权声明
本文为[fifteen billion two hundred and thirty-one million one hundred ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252031182645.html