Concurrency-safe Go caching library with expiration capabilities and access counters

Related tags

hacktoberfest
Overview

cache2go

Latest Release Build Status Coverage Status Go ReportCard GoDoc

Concurrency-safe golang caching library with expiration capabilities.

Installation

Make sure you have a working Go environment (Go 1.2 or higher is required). See the install instructions.

To install cache2go, simply run:

go get github.com/muesli/cache2go

To compile it from source:

cd $GOPATH/src/github.com/muesli/cache2go
go get -u -v
go build && go test -v

Example

package main

import (
	"github.com/muesli/cache2go"
	"fmt"
	"time"
)

// Keys & values in cache2go can be of arbitrary types, e.g. a struct.
type myStruct struct {
	text     string
	moreData []byte
}

func main() {
	// Accessing a new cache table for the first time will create it.
	cache := cache2go.Cache("myCache")

	// We will put a new item in the cache. It will expire after
	// not being accessed via Value(key) for more than 5 seconds.
	val := myStruct{"This is a test!", []byte{}}
	cache.Add("someKey", 5*time.Second, &val)

	// Let's retrieve the item from the cache.
	res, err := cache.Value("someKey")
	if err == nil {
		fmt.Println("Found value in cache:", res.Data().(*myStruct).text)
	} else {
		fmt.Println("Error retrieving value from cache:", err)
	}

	// Wait for the item to expire in cache.
	time.Sleep(6 * time.Second)
	res, err = cache.Value("someKey")
	if err != nil {
		fmt.Println("Item is not cached (anymore).")
	}

	// Add another item that never expires.
	cache.Add("someKey", 0, &val)

	// cache2go supports a few handy callbacks and loading mechanisms.
	cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {
		fmt.Println("Deleting:", e.Key(), e.Data().(*myStruct).text, e.CreatedOn())
	})

	// Remove the item from the cache.
	cache.Delete("someKey")

	// And wipe the entire cache table.
	cache.Flush()
}

To run this example, go to examples/mycachedapp/ and run:

go run mycachedapp.go

You can find a few more examples here. Also see our test-cases in cache_test.go for further working examples.

Issues
  • Callback queue

    Callback queue

    i've added a simple fifo queue for the callbacks. requested in #19

    opened by trapajim 7
  • update smallestDuration

    update smallestDuration

    opened by ck89119 6
  • Support deploy in distribution environment?

    Support deploy in distribution environment?

    Support deploy in distribution environment at current version?

    opened by wwhai 5
  • go1.8 can't iterate over map while writing to it

    go1.8 can't iterate over map while writing to it

    I upgrade golang to 1.8 and encounter this panic while using cache2go:

    goroutine 363453 [running]: runtime.throw(0x94121a, 0x26) /usr/local/go/src/runtime/panic.go:596 +0x95 fp=0xc420513de0 sp=0xc420513dc0 runtime.mapiternext(0xc420513f68) /usr/local/go/src/runtime/hashmap.go:737 +0x7ee fp=0xc420513e90 sp=0xc420513de0 github.com/muesli/cache2go.(*CacheTable).expirationCheck(0xc4201d2540) /Users/geomantic/Documents/develop/gopath/src/github.com/muesli/cache2go/cachetable.go:111 +0x336 fp=0xc4205 13fd8 sp=0xc420513e90 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc420513fe0 sp=0xc420513fd8 created by github.com/muesli/cache2go.(*CacheTable).expirationCheck.func1 /Users/geomantic/Documents/develop/gopath/src/github.com/muesli/cache2go/cachetable.go:137 +0x3e

    bug 
    opened by ZGeomantic 3
  • Question about SetAddedItemCallback

    Question about SetAddedItemCallback

    hello 请看如下函数,是不是应该将这这段注释掉 func (table *CacheTable) SetAddedItemCallback(f func(*CacheItem)) {

    //if len(table.addedItem) > 0 {
    //	table.RemoveAddedItemCallbacks()
    //}
    table.Lock()
    defer table.Unlock()
    table.addedItem = append(table.addedItem, f)
    

    }

    opened by NicholasLiuWei 3
  • Double check whether the table exists or not when creating new one

    Double check whether the table exists or not when creating new one

    Just like the double-check pattern when implementing Singleton, see https://en.wikipedia.org/wiki/Double-checked_locking

    opened by emac 3
  • fix missing dots

    fix missing dots

    Package Testing of go 1.11 is more strict about arguments checking to print-like functions. We will get the error bellow if we left args without dots.

    go test -v
    # github.com/muesli/cache2go
    ./cachetable.go:330: missing ... in args forwarded to print-like function
    FAIL    github.com/muesli/cache2go [build failed]
    

    OS version: manjaro 18.0.0-rc go 1.11.1

    opened by Jnchk 3
  • [BUG] Fatal ERROR when concurrent map write and read

    [BUG] Fatal ERROR when concurrent map write and read

    go version

    go version go1.9 darwin/amd64
    

    log

    fatal error: concurrent map iteration and map write
    
    goroutine 177777 [running]:
    runtime.throw(0xbeb3a9, 0x26)
    	/go1.9/src/runtime/panic.go:605 +0x95 fp=0xc422b05610 sp=0xc422b055f0 pc=0x42e9f5
    runtime.mapiternext(0xc422b05768)
    	/go1.9/src/runtime/hashmap.go:778 +0x6f1 fp=0xc422b056a8 sp=0xc422b05610 pc=0x40d0a1
    github.com/muesli/cache2go.(*CacheTable).expirationCheck(0xc42007e060)
    	/src/github.com/muesli/cache2go/cachetable.go:111 +0x247 fp=0xc422b057d8 sp=0xc422b056a8 pc=0x578d97
    runtime.goexit()
    	/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc422b057e0 sp=0xc422b057d8 pc=0x45f7b1
    created by github.com/muesli/cache2go.(*CacheTable).expirationCheck.func1
    	/src/github.com/muesli/cache2go/cachetable.go:137 +0x3e
    
    duplicate 
    opened by daymade 3
  • addInternal()  call expirationCheck() is not a good idea

    addInternal() call expirationCheck() is not a good idea

    when create a cachetable to init expirationCheck() function , maybe better than call it in addInternal function

    opened by brightyuan 1
  • CacheTable.Add() should  not use  “defer table.unlock ”

    CacheTable.Add() should not use “defer table.unlock ”

    CacheTable.Add() function ,must not use "defer table.Unlock()" because addInternal() func will unlock table, that will make duplicate deletion exception

    opened by brightyuan 0
  • concurrent cache management

    concurrent cache management

    I was unable to manage cache concurrently as mentioned in examples/concurrent-cache-manager/concurrent-cache-manager.go

    This PR fixes issue #43

    opened by lakshanwd 1
  • how about expire an item on the timestamp being added into the table other then the last time being accessed

    how about expire an item on the timestamp being added into the table other then the last time being accessed

    how about expire an item on the timestamp being added into the table other then the last time being accessed.

    opened by datainsect 1
  • [bug]: delete operation in Foreach causes a deadlock

    [bug]: delete operation in Foreach causes a deadlock

    test code:

    package main
    
    import (
    	"fmt"
    	"time"
    
    	"github.com/muesli/cache2go"
    )
    
    func main() {
    	store := cache2go.Cache("test")
    	store.AddAboutToDeleteItemCallback(func(v *cache2go.CacheItem) {
    		fmt.Println("delete:", v)
    	})
    
    	key := "test1"
    	store.Add(key, time.Second*1800, "asdfasdfasd")
    	fmt.Println("fitst delete:")
    	store.Delete(key)
    
    	store.Add(key, time.Second*1800, "asdfasdfasd")
    
    	go func() {
    		time.Sleep(time.Second * 2)
    		fmt.Println("goroutine delete:")
    
    		store.Foreach(func(key interface{}, v *cache2go.CacheItem) {
    			fmt.Println("start")
    			store.Delete(key)
    			fmt.Println("end")
    		})
    	}()
    
    	time.Sleep(time.Second * 10)
    }
    

    Foreach polls for all, while Rlock is released only after the Foreach execution completes, resulting in a deadlock when deleted in Foreach

    opened by RealLiuSha 3
  • is item.RLock() among table.items‘s traversal in expirationCheck() necessary?

    is item.RLock() among table.items‘s traversal in expirationCheck() necessary?

    because of the existing of table.Lock(),a table items‘s traversal is not concurrent, so I think item.RLock() may be not needed.

    opened by Deep-Coder-zhui 3
  • expirationCheck  optimization ,use lazy  and timeAfter check , just like redis key expired policy

    expirationCheck optimization ,use lazy and timeAfter check , just like redis key expired policy

    now expirationCheck will Iter hole map,it will cost more cpu

    opened by liulanghai 0
  • if registered dataload, Value() not thread safe

    if registered dataload, Value() not thread safe

    // Value returns an item from the cache and marks it to be kept alive. You can
    // pass additional arguments to your DataLoader callback function.
    func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error) {
    	table.RLock()
    	r, ok := table.items[key]
    	loadData := table.loadData
    	table.RUnlock()
    
    	if ok {
    		// Update access counter and timestamp.
    		r.KeepAlive()
    		return r, nil
    	}
    
    	// Item doesn't exist in cache. Try and fetch it with a data-loader.
    	if loadData != nil {
    		item := loadData(key, args...)
    		if item != nil {
    			table.Add(key, item.lifeSpan, item.data)
    			return item, nil
    		}
    
    		return nil, ErrKeyNotFoundOrLoadable
    	}
    
    	return nil, ErrKeyNotFound
    }
    
    opened by winxxp 2
  • Allow for setting an upper limit on memory usage

    Allow for setting an upper limit on memory usage

    When a system is under high load the cache might grow to the point when it's consuming all available memory on the system it's running.

    Consider adding some kind of upper limit to how much resources are occupied e.g. max items or max memory used. Memory usage could be calculated for structs using unsafe.Sizeof if the security requirements are accepting.

    opened by gurre 2
Releases(v0.2)
Owner
Christian Muehlhaeuser
Geek, Gopher, Software Developer, Maker, Opensource Advocate, Tech Enthusiast, Photographer, Board and Card Gamer
Christian Muehlhaeuser
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

GCache Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. Features Supports expirable Cache, LFU, LRU and ARC. Goroutine safe. S

Jun Kimura 318 May 31, 2021
☔️ 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

Vincent Composieux 952 Sep 16, 2021
Concurrency-safe Go caching library with expiration capabilities and access counters

cache2go Concurrency-safe golang caching library with expiration capabilities. Installation Make sure you have a working Go environment (Go 1.2 or hig

Christian Muehlhaeuser 1.6k Sep 20, 2021
An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.

go-cache go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major

Patrick Mylund Nielsen 5.4k Sep 18, 2021
slab allocator in go

go-slab - slab allocator in go A slab allocator library in the Go Programming Language. Who is this for This library may be interesting to you if you

null 325 Aug 17, 2021
Efficient cache for gigabytes of data written in Go.

BigCache Fast, concurrent, evicting in-memory cache written to keep big number of entries without impact on performance. BigCache keeps entries on hea

Allegro Tech 5.1k Sep 20, 2021
go 两级缓存方案,支持分布式

g2cache 介绍 分布式多级缓存方案g2cache 软件架构 软件架构说明 主要内容: 模块 功能 特点 注意 local interface 本机内存高速缓存 纳秒,毫秒级别响应速度;有效期较短 实现LocalCache接口,注意控制内存 out interface 外部高速缓存

null 12 Sep 12, 2021
groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.

groupcache Summary groupcache is a distributed caching and cache-filling library, intended as a replacement for a pool of memcached nodes in many case

Go 10.7k Sep 23, 2021
An embedded key/value database for Go.

bbolt bbolt is a fork of Ben Johnson's Bolt key/value store. The purpose of this fork is to provide the Go community with an active maintenance and de

etcd-io 4.8k Sep 16, 2021
Redis caching layer for Cloudflare KV in Golang

Redis caching layer for Cloudflare KV in Golang

Dinesh Sonachalam 19 Sep 16, 2021
Multi-level caching service in Go

IgoVIUM Multi-level caching service in Go. Specifically: Distributed in-memory cache (L1) DB-based cache (L2) Long term historization on persistent vo

Andrea 5 Sep 4, 2021
MySQL to Redis caching made easy

redisql MySQL to Redis caching made easy

Tarek Ali 51 Sep 20, 2021
A memcached proxy that manages data chunking and L1 / L2 caches

Rend: Memcached-Compatible Server and Proxy Rend is a proxy whose primary use case is to sit on the same server as both a memcached process and an SSD

Netflix, Inc. 1.1k Sep 23, 2021
gdcache is a pure non-intrusive distributed cache library implemented by golang

gdcache is a pure non-intrusive distributed cache library implemented by golang, you can use it to implement your own distributed cache

Jovan 4 Sep 1, 2021
LFU Redis implements LFU Cache algorithm using Redis as data storage

LFU Redis cache library for Golang LFU Redis implements LFU Cache algorithm using Redis as data storage LFU Redis Package gives you control over Cache

Mohamed Shapan 6 Apr 23, 2021
go-pmem is a project that adds native persistent memory support to Go.

Introduction go-pmem is a project that adds native persistent memory support to Go. This is achieved through a combination of language extensions, com

Jerrin Shaji George 63 Aug 7, 2021
Go memcache client package

Description This is a memcache client package for the Go programming language. The following commands are implemented: get (single key) set, add, repl

Krzysztof Kliś 65 Feb 11, 2021
Go Memcached client library #golang

About This is a memcache client library for the Go programming language (http://golang.org/). Installing Using go get $ go get github.com/bradfitz/gom

Brad Fitzpatrick 1.4k Sep 24, 2021