项目作者: ataul443

项目描述 :
In-memory cache written in go.
高级语言: Go
项目地址: git://github.com/ataul443/sweep.git
创建时间: 2020-11-11T13:47:13Z
项目社区:https://github.com/ataul443/sweep

开源协议:MIT License

下载


Sweep

Sweep is an in-memory cache. It uses sharding techinque to store millions of entries inside it. Even with millions of entries inside it the GC pause takes only a few milliseconds.

Note: It is a toy project, not intended to be used in production. I was practicing for an LLD interview and I ended up writing this.

Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/ataul443/sweep"
  5. )
  6. func main() {
  7. cache := sweep.Default()
  8. defer cache.Close()
  9. err := cache.Put("pikachu", []byte("value of pikachu"))
  10. if err != nil {
  11. panic(err)
  12. }
  13. val, err := cache.Get("pikachu")
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Println(string(val))
  18. }

How GC Pause minimized ?

  1. Starting GC Pause benchmark....
  2. Going to put 20000000 entries in sweep.
  3. Time took in inserting 20000000 entries in sweep: 18.93 seconds
  4. GC Pause took: 85 milliseconds
  5. Total entries in cache: 20000000

Instead of storing (key, val) pair directly in map[string]*entry, Sweep stores the val in a queue and stores the index in a map[uint64]uint64 as an (key, index) pair. Large map containing pointers causes huge GC pause time, however this is not same with map containing integers. You can read up more about this at issue-9477.