Skip to content

Dganzh/zcache

Repository files navigation

zcache

quick start

package main

import (
	"fmt"
	"zcache"
)

func main() {
    cache := zcache.NewCache()
    v := 123
    cache.Set("a", v)
    if v != cache.Get("a") {
        fmt.Printf("Get invalid value: %+v", v)
    }
    cache.Del("a")
    if cache.Get("a") != nil {
		fmt.Println("Del failed")
    }
}

Multiple servers

server list:

  • localhost:5205
  • localhost:5206
  • localhost:5207

On server1 start one

c1 = NewDCache(
    "localhost:5205",
    []string{"localhost:5206", "localhost:5207"},
    WithLru(),
    WithSize(1000),
)

On server2 start other one

c2 = NewDCache(
    "localhost:5206",
    []string{"localhost:5205", "localhost:5207"},
    WithLru(),
    WithSize(1000),
)

On server3 start last

c3 = NewDCache(
    "localhost:5207",
    []string{"localhost:5205", "localhost:5206"},
    WithLru(),
    WithSize(1000),
)

On server1, set a value:

c1.Set("a", 123)

On another server, this key will be none:

c2.Get("a") == nil

Now, sever2 has newest value, you should delete it before set:

c2.Del("a")         // sync to server1 and server3
c2.Set("a", 456)

On server1:

c1.Get("a") == nil

TODO

  • add expire
  • add evict type(LFU\FIFO)
  • add Load
  • single fly
  • add statics
  • 参考redis的LRU改进

About

Lightweight KV cache

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages