Skip to content

octu0/concache

Repository files navigation

concache

MIT License GoDoc Go Report Card Releases

concache is in-memory key:value cache. concache provides thread-safe map[string]interface{} with expiration times. concache a high-performance solution to this by sharding the map with minimal time spent waiting for locks.

Documentation

https://godoc.org/github.com/octu0/concache

Installation

$ go get github.com/octu0/concache

Example

import(
  "time"
  "github.com/octu0/concache"
)

func main(){
  cache := concache.New(
    concache.WithDefaultExpiration(5 * time.Second),
    concache.WithCleanupInterval(10 * time.Minute),
  )

  cache.Set("hello", "123", 1 * time.Second)
  cache.SetDefault("world", "456")
  if value, ok := cache.Get("hello"); ok {
    println("hello " + value.(string))
  }
  if value, ok := cache.Get("world"); ok {
    println("world " + value.(string))
  }

  time.Sleep(1 * time.Second)

  if _, ok := cache.Get("hello"); ok != true {
    println("hello expired")
  }
  if _, ok := cache.Get("world"); ok {
    println("world not expired")
  }
}

License

MIT, see LICENSE file for details.