Categorygithub.com/InjectiveLabs/injective-cache
modulepackage
0.0.4
Repository: https://github.com/injectivelabs/injective-cache.git
Documentation: pkg.go.dev

# README

Cache and cache

2 types of cache under 1 interface

SimpleCache (V2)

Simple cache interface allows to store and retrieve arbitrary types from the cache.

There are 2 implementations of the SimpleCache interface:

  • SimpleRedisCache
  • TypedLibCache

Redis Simple Cache

With the Redis Simple Cache, you can choose the way to encode and decode the data to be stored in the cache. If you don't provide a codec, the default codec will be used, which is the json codec.

Typed Lib Cache

With this cache you can store any type in memory, without having to serialize the data, with the caveat that you have to declare the type when you create the cache.

Resource Coalescing Cache

Resource Coalescing Cache is a cache that allows you to coalesce the requests for the same resource, this means that if there are multiple requests for the same resource, only one request will be made to the backend, and the rest of the requests will wait for the first request to finish.

It includes a cache, so once the first request finishes, it will set the value in the cache and, while is valid, new requests will return the value from the cache.

New Redis cache

cache, err := NewRedisCache(context.Background(), "localhost:6379", 5*time.Second)
if err != nil {
    panic(err)
}

cache.Set("key1", []byte("hello1"))
cache.Set("key2", []byte("hello2"))

v, err := cache.Get("key2")
if err != nil {
    panic(err)
}

fmt.Println(v)

New libcache (in-process cache)

cache, err := NewLibcache(0, 5*time.Second)
if err != nil {
    panic(err)
}

cache.Set("key1", []byte("hello1"))
cache.Set("key2", []byte("hello2"))

v, err := cache.Get("key2")
if err != nil {
    panic(err)
}

fmt.Println(v)

# Functions

Get is a generic helper to retrieve any type of value from a TTLCache.
No description provided by the author
NewMockTTLCache creates a new mock instance.
No description provided by the author
No description provided by the author
NewRedisSimpleCache creates a new RedisSimpleCache instance.
NewResourceCoalescingCache creates a new ResourceCoalescingCache.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
MockTTLCache is a mock of TTLCache interface.
MockTTLCacheMockRecorder is the mock recorder for MockTTLCache.
RedisSimpleCache is a redis cache implementation using go-redis/v8.
ResourceCoalescingCache is a cache that coalesces multiple requests for the same resource into a single request to prevent cache stampedes.
No description provided by the author

# Interfaces

No description provided by the author
Codec is an interface that allows encoding and decoding of byte slices.
TTLCache is a cache interface that allows any type of key and value as long as the key is hashable and the value is serializable.