Fast in-memory key:value store/cache with TTL

MCache library

Build Status Go Report Card GoDoc

go-mcache - this is a fast key:value storage. Its major advantage is that, being essentially a thread-safe .

map[string]interface{}

with expiration times, it doesn't need to serialize, and quick removal of expired keys.

Installation

~ $ go get -u github.com/OrlovEvgeny/go-mcache

Example a Pointer value (vary fast method)

type User struct {
	Name string
	Age  uint
	Bio  string
}

func main() {
	//Start mcache instance
	MCache = mcache.New()

	//Create custom key
	key := "custom_key1"
	//Create example struct
	user := &User{
		Name: "John",
		Age:  20,
		Bio:  "gopher 80 lvl",
	}

	//args - key, &value, ttl (or you need never delete, set ttl is mcache.TTL_FOREVER)
	err := MCache.Set(key, user, time.Minute*20)
	if err != nil {
		log.Fatal(err)
	}

	if data, ok := MCache.Get(key); ok {
		objUser:= data.(*User)
		fmt.Printf("User name: %s, Age: %d, Bio: %s\n", objUser.Name, objUser.Age, objUser.Bio)			
	}
}

Performance Benchmarks

goos: darwin
goarch: amd64
BenchmarkWrite-4          200000              7991 ns/op 
BenchmarkRead-4          1000000              1716 ns/op 
BenchmarkRW-4             300000              9894 ns/op

What should be done

  • the possibility of closing
  • r/w benchmark statistics
  • rejection of channels in safeMap in favor of sync.Mutex (there is an opinion that it will be faster)

License:

MIT

Similar Resources

Package mafsa implements Minimal Acyclic Finite State Automata in Go, essentially a high-speed, memory-efficient, Unicode-friendly set of strings.

MA-FSA for Go Package mafsa implements Minimal Acyclic Finite State Automata (MA-FSA) with Minimal Perfect Hashing (MPH). Basically, it's a set of str

Oct 27, 2022

An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Dec 28, 2022

My clean Go solution that's faster than 100%, takes up less memory than 100%.

Remove-element My very clean Go solution that's faster than 100% of all solutions on Leetcode. Leetcode Challenge: "Given an integer array nums and an

Dec 24, 2021

A Go implementation of an in-memory bloom filter, with support for boltdb and badgerdb as optional data persistent storage.

Sprout A bloom filter is a probabilistic data structure that is used to determine if an element is present in a set. Bloom filters are fast and space

Jul 4, 2022

A memory-efficient trie for testing the existence/prefixes of string only(for now).

Succinct Trie A memory-efficient trie for testing the existence/prefixes of string only(for now). Install go get -u github.com/nobekanai/sutrie Docume

Mar 10, 2022

ZSet is an in-memory Redis like sorted set datastructure

zset Getting Started Installing To start using hash, install Go and run go get: $ go get -u github.com/arriqaaq/zset This will retrieve the library. U

Oct 13, 2022

Null Types, Safe primitive type conversion and fetching value from complex structures.

Typ Typ is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang Feat

Sep 26, 2022

Surprisingly space efficient trie in Golang(11 bits/key; 100 ns/get).

Surprisingly space efficient trie in Golang(11 bits/key; 100 ns/get).

Slim - surprisingly space efficient data types in Golang Slim is collection of surprisingly space efficient data types, with corresponding serializati

Jan 2, 2023

Fast ring-buffer deque (double-ended queue)

deque Fast ring-buffer deque (double-ended queue) implementation. For a pictorial description, see the Deque diagram Installation $ go get github.com/

Dec 26, 2022
Comments
  • Cache seems to be a singleton [resolved]

    Cache seems to be a singleton [resolved]

    Try this:

    a, b := mcache.New(), mcache.New()
    a.Set("foo", 123, time.Second)
    val, ok := b.Get("foo")
    fmt.Println(val, ok)
    

    Which will produce "123 ok". It seems you cannot have multiple caches. This should either be documented clearly or fixed.

  • Fix a bug that objects remain in storage.

    Fix a bug that objects remain in storage.

    I found a rare case bug that objects remain in a storage in spite of gc working.

    We can sometimes see by the following.

    import (
    	"fmt"
    	"time"
    	"strconv"
    	"math/big"
    	"github.com/OrlovEvgeny/go-mcache"
    )
    
    const (
    	N = 4000
    )
    
    func test() {
    	driver := mcache.StartInstance()
    
    	var interval time.Duration
    	var i int64
    
    	fmt.Printf("Storage has %d object\n", driver.Len())
    
    	i = 0
    	for true {
    		if i % 1000 == 0 {
    			fmt.Printf("It is working... %d\n", i)
    		}
    
    		interval = time.Millisecond * 1
    
    		time.Sleep(interval)
    
    		driver.Set(strconv.FormatInt(i, 10), big.NewInt(i), interval)
    
    		i++
    
    		if i >= N {
    			break
    		}
    	}
    
    	fmt.Printf("Finished setting %d objects\n", N)
    
    	fmt.Printf("Storage currently has %d object\n", driver.Len())
    
    	time.Sleep(time.Second * 10)
    
    	l := driver.Len()
    	if l != 0 {
    		fmt.Printf("%d objects remain\n", l)
    	} else {
    		fmt.Println("Nothing remain")
    	}
    }
    
    func main() {
    	test()
    }
    

    Please confirm the behavior above, and check my code.


    Performance

    Before

    BenchmarkWrite-4          200000              8370 ns/op                                                                                                                                                    
    BenchmarkRead-4          1000000              1710 ns/op                                                                                                                                                    
    BenchmarkRW-4             300000              9412 ns/op
    

    After

    BenchmarkWrite-4          200000              7991 ns/op 
    BenchmarkRead-4          1000000              1716 ns/op 
    BenchmarkRW-4             300000              9894 ns/op
    
  • README: fix example code

    README: fix example code

    • add "header" sections (package and import)
    • add missing word "struct" in the User type declaration
    • replace "new" with composite literal to get rid of unnecessary assignments
    • add missing returned error receiver to MCache.Set and MCache.SetPointer
    • fix MCache name where methods Get and GetPointer are called
    • fix typo objUser,Bio -> objUser.Bio
    • last log.Println should be Printf in order to replace "%s" with the key value
  • Apply for maintainer

    Apply for maintainer

    I did a very detailed analysis of the source code of the entire project. The project has not been maintained for a long time. Can I join the maintenance because it is used in our project? Can you invite me? Thank you

When storing a value in a Go interface allocates memory on the heap.

Go interface values This repository deep dives Go interface values, what they are, how they work, and when storing a value in a Go interface allocates

Dec 16, 2022
A thread safe map which has expiring key-value pairs

~ timedmap ~ A map which has expiring key-value pairs. go get github.com/zekroTJA/timedmap Intro This package allows to set values to a map which will

Dec 29, 2022
A fast little LRU cache for Go

tinylru A fast little LRU cache. Getting Started Installing To start using tinylru, install Go and run go get: $ go get -u github.com/tidwall/tinylru

Dec 24, 2022
dagger is a fast, concurrency safe, mutable, in-memory directed graph library with zero dependencies
dagger is a fast, concurrency safe, mutable, in-memory directed graph library with zero dependencies

dagger is a blazing fast, concurrency safe, mutable, in-memory directed graph implementation with zero dependencies

Dec 19, 2022
Cache Slow Database Queries
Cache Slow Database Queries

Cache Slow Database Queries This package is used to cache the results of slow database queries in memory or Redis. It can be used to cache any form of

Dec 19, 2022
Golang LRU cache

golang-lru This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the cache in Groupcache. Documentation Fu

Dec 29, 2022
☔️ A complete Go cache library that brings you multiple ways of managing your caches
☔️ A complete Go cache library that brings you multiple ways of managing your caches

Gocache Guess what is Gocache? a Go cache library. This is an extendable cache library that brings you a lot of features for caching data. Overview He

Jan 1, 2023
A skip list of arbitrary elements that can be filtered using roaring bitmaps stored in an LRU cache

Skipfilter This package provides a data structure that combines a skiplist with a roaring bitmap cache. This library was created to efficiently filter

Aug 4, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Jan 8, 2023
BTree provides a simple, ordered, in-memory data structure for Go programs.

BTree implementation for Go This package provides an in-memory B-Tree implementation for Go, useful as an ordered, mutable data structure. The API is

Dec 30, 2022