tryvium-travels/golang-cache-adapters

[FEATURE REQUEST] Add an adapter for in-memory cache

Closed this issue · 0 comments

In-Memory Cache Adapter (discussion in progress...)

The idea is to use create a simple cache adapter which just uses an internal map[string]interface{}

This would be useful, for example to test the library before adding actual caches (we are perfectly aware that an in-memory cache is not distributed and therefore does not scale)

Implementation proposal

Use a map[string]cacheItem object composed like the following struct

type cacheItem struct {
    Data interface{}
    lock sync.Mutex
    expiresAt time.Time
}

Then when you call any operation on a key we check for expiresAt value: if it is expired the operation returns ErrNotFound and deletes that key. THIS INTRODUCES A POTENTIAL MEMORY LEAK

The map must also use sync.Mutex objects to avoid concurrent writes and reads. (an alternative would be usage of functions from atomic package)

Open points

How do we add TTL avoiding memory leaks?

The idea is to use a mechanism in which we save the expiration time of a key and calling a goroutine which cleans expired keys, we are searching for alternative proposals since this does not seem to perfectly fit