One caching API, Multiple backends

OneCache - A Go caching Library

Coverage Status Build Status

Installation

$ go get -u github.com/adelowo/onecache

Supported cache stores

  • InMemory
  • Filesystem
  • Memcached
  • Redis

OneCache also comes with garbage collection. This is used by the filesystem and memory adapter to purge out expired items automatically. Please refer to the examples

Examples containing all adapters can be found here

var store onecache.Store

store = filesystem.MustNewFSStore("/home/adez/onecache_tmp")

err := store.Set("profile", []byte("Lanre"), time.Second*60)

if err != nil {
	fmt.Println(err)
	return
}

value,err := store.Get("profile")
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(value))

Some adapters like the filesystem and memory have a Garbage collection implementation. All that is needed to call is store.GC(). Ideally, this should be called in a ticker.C.

LICENSE

MIT

Owner
Lanre Adelowo
(Not) A clown
Lanre Adelowo
Comments
  • Would []byte be preferable?

    Would []byte be preferable?

    Instead of interacting via empty interface, what are your thoughts on using []byte? The user would take on the onus of [de]serialization and the skip the guessing game around type

    Cool project. I'll consider using it

  • Generates a open file error instead of a onecache.ErrCacheMiss

    Generates a open file error instead of a onecache.ErrCacheMiss

    https://github.com/adelowo/onecache/blob/ae397c7322a160a3f0e9ffba976417260bd732ec/filesystem/fs.go#L117

    I was going to submit a pull request but could not figure out exactly how to fix it given some issues I was running into getting it set up on GoLand so I could both edit and debug it (hard to edit and debug an external library in my IDE w/o renaming all the import paths.)

    Anyway, without this is makes error handling harder than it needs to be.

  • In memory.go you are using lock.RLock for both reads and writes

    In memory.go you are using lock.RLock for both reads and writes

    You should be using lock.Lock() for writes (Set, Delete, etc.) and lock.RLock() for reads (Get)

    See https://github.com/adelowo/onecache/blob/master/memory/memory.go#L24 and https://github.com/adelowo/onecache/blob/master/memory/memory.go#L41

    Otherwise use a regular sync.Mutex()

  • Delete expired items in background

    Delete expired items in background

    It will be good if you will delete expired items in background. If client will add all items with ExpiresAt and read only several items you will have storage that will grow infinitely.

  • fatal error: concurrent map read and map write

    fatal error: concurrent map read and map write

    In get call we are getting panic error randomly, here is the stack trace

    goroutine 1549 [running]: runtime.throw(0xc451b2, 0x21) /usr/local/go/src/runtime/panic.go:617 +0x72 fp=0xc000b16600 sp=0xc000b165d0 pc=0x42bd92 runtime.mapaccess2_faststr(0xb51c20, 0xc00017f6b0, 0xc0009bc460, 0x49, 0xc00017f688, 0xc0001c8700) /usr/local/go/src/runtime/map_faststr.go:116 +0x4a9 fp=0xc000b16670 sp=0xc000b16600 pc=0x412589 github.com/adelowo/onecache/memory.(*InMemoryStore).Delete(0xc00017f680, 0xc00029a400, 0x40, 0x49, 0xc0001c87d8) /jenkins/workspace/Vega_1.0.0_Nightly_Build/pkg/mod/github.com/adelowo/[email protected]/memory/memory.go:84 +0x70 fp=0xc000b166b0 sp=0xc000b16670 pc=0x59f330 github.com/adelowo/onecache/memory.(*InMemoryStore).Get(0xc00017f680, 0xc00029a400, 0x40, 0x10, 0x10, 0xb22820, 0xb, 0xc000b16768) /jenkins/workspace/Vega_1.0.0_Nightly_Build/pkg/mod/github.com/adelowo/[email protected]/memory/memory.go:73 +0x16c fp=0xc000b16708 sp=0xc000b166b0 pc=0x59f20c

    Added quote in below code snipet where its failing

    func (i *InMemoryStore) Get(key string) ([]byte, error) { i.lock.RLock()

    item := i.data[i.keyfn(key)]
    if item == nil {
    	i.lock.RUnlock()
    	return nil, onecache.ErrCacheMiss
    }
    
    if item.IsExpired() {
    	i.lock.RUnlock()
    	**
    

    i.Delete(key)

    ** return nil, onecache.ErrCacheMiss }

    i.lock.RUnlock()
    return copyData(item.Data), nil
    

    }

  • Introduction of options

    Introduction of options

    This is needed since in some places the user cannot configure a few things e.g cache key ( #19 ). we default to an internal implementation based on the adapter.

    See #20

  • Fix race condition in tests

    Fix race condition in tests

    In the memory store) -- The items are accessed directly in a test suite without making use of the lock, hence there's a race condition on running tests with -race

    There is also something similar in the fs store

  • memory: store item instead of serialized data

    memory: store item instead of serialized data

    Previously stored the serialized data for each key so it needed to be deserialized to check if it had expired. ie. GC needed to deserialize the whole corpus to do its job.

    This change updates the code to store *onecache.Item. The ExpiresAt attribute can then be used directly to check expires.

    During Set calls the data is copied before save and during Get calls the data is copied before return. This allows the caller to modify their data without issue, as they were allowed to before.

  • Serializer should bail out fast if the type is already a byte array [Details inside]

    Serializer should bail out fast if the type is already a byte array [Details inside]

    https://github.com/adelowo/onecache/blob/master/utils.go#L31-L34

    If the type is a []byte, the serializer should return it as is rather than try encoding it with encoding/gob. I have no idea if encoding the bytes would cause an issue but it seems like a nice addition to have in the codebase

  • Version 2.5

    Version 2.5

    There are a few changes I would like to add to the package.

    • [x] Introduction of options so we can have something like NewWithOpts ( This would fix #19 anyways )

    • [x] Removal of Extend and Get ( Too much of a breaking change ??? )

    • ~~[ ] Allow for multiple servers for the redis adapter . ( Nice to have )~~

Related tags
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

Dec 31, 2022
MySQL to Redis caching made easy

redisql MySQL to Redis caching made easy

Sep 4, 2022
Redis caching layer for Cloudflare KV in Golang
Redis caching layer for Cloudflare KV in Golang

Redis caching layer for Cloudflare KV in Golang

Dec 21, 2022
Multi-level caching service in Go
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

Nov 9, 2022
POC de caching en Go en utilisant go-redis/cache

Test-web POC de caching en Go en utilisant go-redis/cache, cette lib permet d'avoir un cache local et un cache redis (appel cache local puis cache red

Nov 19, 2021
Design and Implement an in-memory caching library for general use

Cache Implementation in GoLang Problem Statement Design and Implement an in-memory caching library for general use. Must Have Support for multiple Sta

Dec 28, 2021
A simple generic in-memory caching layer

sc sc is a simple in-memory caching layer for golang. Usage Wrap your function with sc - it will automatically cache the values for specified amount o

Jul 2, 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
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

May 31, 2021
Cachy is a simple and lightweight in-memory cache api.
Cachy is a simple and lightweight in-memory cache api.

cachy Table of Contents cachy Table of Contents Description Features Structure Configurability settings.json default values for backup_file_path Run o

Apr 24, 2022
A REST-API service that works as an in memory key-value store with go-minimal-cache library.

A REST-API service that works as an in memory key-value store with go-minimal-cache library.

Aug 25, 2022
In Memory cache in GO Lang Api

In memory key-value store olarak çalışan bir REST-API servisi Standart Kütüphaneler kullanılmıştır Özellikler key ’i set etmek için bir endpoint key ’

Dec 16, 2021
a wrapper around BadgerDB providing a simple API.

Carbon Cache A wrapper around BadgerDB providing a simple API. NOTE This package is provided "as is" with no guarantee. Use it at your own risk and al

Sep 27, 2022
a key-value store with multiple backends including leveldb, badgerdb, postgresql

Overview goukv is an abstraction layer for golang based key-value stores, it is easy to add any backend provider. Available Providers badgerdb: Badger

Jan 5, 2023
Telego is Telegram Bot API library for Golang with full API implementation (one-to-one)
Telego is Telegram Bot API library for Golang with full API implementation (one-to-one)

Telego • Go Telegram Bot API Telego is Telegram Bot API library for Golang with full API implementation (one-to-one) The goal of this library was to c

Jan 5, 2023
JWT login microservice with plugable backends such as OAuth2, Google, Github, htpasswd, osiam, ..
JWT login microservice with plugable backends such as OAuth2, Google, Github, htpasswd, osiam, ..

loginsrv loginsrv is a standalone minimalistic login server providing a JWT login for multiple login backends. ** Attention: Update to v1.3.0 for Goog

Dec 24, 2022
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.

Darkroom - Yet Another Image Proxy Introduction Darkroom combines the storage backend and the image processor and acts as an Image Proxy on your image

Dec 6, 2022
Bayesian text classifier with flexible tokenizers and storage backends for Go

Shield is a bayesian text classifier with flexible tokenizer and backend store support Currently implemented: Redis backend English tokenizer Example

Nov 25, 2022
Generate CRUD gRPC backends from single YAML description.
Generate CRUD gRPC backends from single YAML description.

Pike Generate CRUD gRPC backends from single YAML description. Check out Playground! Pike generates: gRPC Protobuf service description with basic Crea

Dec 23, 2022
Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.

sessions gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends. The key features are: Simple API: us

Dec 28, 2022