Skip to content

dmytro-vovk/go-mcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mcache

.github/workflows/ci.yaml Coverage Status Go Report Card Reference

Yet another in-memory cache library with expiration.

Uses generics and does not use periodic check for expired items. Instead, it uses internal ordered queue to range items in order of expiration and just sleeps until it is time to expire an item. thus having minimal computational overhead.

Installation

go get github.com/dmytro-vovk/go-mcache

Usage

// Create a new instance with string keys and int values
c := mcache.New[string, int]()

// Set couple values
c.Set("one", 1, time.Minute)
c.Set("two", 2, time.Hour)

// Get a value
if value, ok := c.Get("one"); ok {
	fmt.Printf("Got value: %v", value)
}

// Get value and delete it from the cache
if value, ok := GetAndDelete("two"); ok {
    fmt.Printf("Got and deleted value: %v", value)
}

// Try getting non-existing value
if _, ok := Get("two"); !ok {
    fmt.Print("Value no longer cached")
}

// Set different value for the key keeping the same TTL
if c.Update("one", 101) {
    fmt.Print("Value was updated")
}

// Replace the value getting the previous one
if value, ok := c.Swap("one", 202); ok {
    fmt.Printf("Previous value was %v", value)
}

// Delete a value
if c.Delete("one") {
    fmt.Print("The value is deleted")
}

// Change the value TTL 
if c.Refresh("one", time.Hour) {
    fmt.Print("TTL for the key is set to one hour")
}

// Let's see how many values we have
fmt.Printf("We have %d values", c.Len())

// Let's free some memory by evicting some data
if evicted := c.Evict(10); evicted > 0 {
    fmt.Printf("Evicted %d values", evicted)
}

// Value expires
c.Set("gone fast", 1000, time.Millisecond)
time.Sleep(time.Millisecond)
if _, ok := c.Get("gone fast"); !ok {
    fmt.Print("the value is gone!")
}

See examples directory for more.

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/dmytro-vovk/go-mcache
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkCacheSet
BenchmarkCacheSet-12       	 2389052	       485.8 ns/op	     243 B/op	       2 allocs/op
BenchmarkCacheGet
BenchmarkCacheGet-12       	72900222	        15.56 ns/op	       2 B/op	       0 allocs/op
BenchmarkCacheAddGet
BenchmarkCacheAddGet-12    	  781284	      1662 ns/op	     607 B/op	      10 allocs/op