DEV Community

Cover image for Golang in-memory key-value pair cache
Mathias Jiya
Mathias Jiya

Posted on

Golang in-memory key-value pair cache

Hello devs, I am excited to share my most recent project.
I started working on this Golang project about a week ago. And as you must have guessed already, yes, it's yet another Golang library :)
Fs-cache is a Golang-based caching solution that's simple and lightweight. It supports data caching with optional parameters for expiration. This library has 14 methods added to it to allow for different use cases of the library.
Fs-cache provides a quick way to store and retrieve frequently accessed data, significantly enhancing your application performance and reducing database queries / API calls.
Below is a sample code on how to use the library.

Installation

go get github.com/iqquee/fs-cache@latest
Enter fullscreen mode Exit fullscreen mode

Import

fscache "github.com/iqquee/fs-cache"
Enter fullscreen mode Exit fullscreen mode

Usage

Debug()

Debug() enables debug to get certain logs

fs := fscache.New()
// set if you want to get logs of activities
fs.Debug()
Enter fullscreen mode Exit fullscreen mode

Set()

Set() adds a new data into the in-memmory storage

fs := fscache.New()

// the third param is an optional param used to set the expiration time of the set data
if err := fs.Set("key1", "user1", 5*time.Minute); err != nil {
    fmt.Println("error setting key1:", err)
}
Enter fullscreen mode Exit fullscreen mode

Get()

Get() retrieves a data from the in-memmory storage

fs := fscache.New()

result, err := fs.Get("key1")
if err != nil {
    fmt.Println("error getting key 1:", err)
}

fmt.Println("key1:", result)
Enter fullscreen mode Exit fullscreen mode

SetMany()

SetMany() sets many data objects into memory for later access

fs := fscache.New()

testCase := []map[string]fscache.CacheData{
    {
        "key4": fscache.CacheData{
            Value:    "value4",
            Duration: time.Now().Add(time.Minute),
        },
        "key5": fscache.CacheData{
            Value: false,
        },
    },
}

setMany, err := fs.SetMany(testCase)
if err != nil {
    fmt.Println("error setMany:", err)
}
fmt.Println("setMany:", setMany)
Enter fullscreen mode Exit fullscreen mode

GetMany()

GetMany() retrieves datas with matching keys from the in-memmory storage

fs := fscache.New()

keys := []string{"key1", "key2"}

getMany := fs.GetMany(keys)
fmt.Println("getMany:", getMany)
Enter fullscreen mode Exit fullscreen mode

OverWrite()

OverWrite() updates an already set value using it key

fs := fscache.New()

if err := fs.OverWrite("key1", "overwrite1", 1*time.Minute); err != nil {
    fmt.Println("error overwriting:", err)
}
Enter fullscreen mode Exit fullscreen mode

OverWriteWithKey()

OverWriteWithKey() updates an already set value and key using the previously set key

fs := fscache.New()

if err := fs.OverWriteWithKey("previousKey", "newKey", "newValue", 1*time.Minute); err != nil {
    fmt.Println("error overWriteWithKey:", err)
}
Enter fullscreen mode Exit fullscreen mode

Del()

Del() deletes a data from the in-memmory storage

fs := fscache.New()

if err := fs.Del("key1"); err != nil {
    fmt.Println("error deleting key 1:", err)
}
Enter fullscreen mode Exit fullscreen mode

TypeOf()

TypeOf() returns the data type of a value

fs := fscache.New()

typeOf, err := fs.TypeOf("key1")
if err != nil {
    fmt.Println("error typeOf:", err)
}
fmt.Println("typeOf:", typeOf)
Enter fullscreen mode Exit fullscreen mode

Clear()

Clear() deletes all datas from the in-memmory storage

fs := fscache.New()

if err := fs.Clear(); err != nil {
    fmt.Println("error clearing all datas:", err)
}
Enter fullscreen mode Exit fullscreen mode

Size()

Size() retrieves the total data objects in the in-memmory storage

fs := fscache.New()

size := fs.Size()
fmt.Println("total size: ", size)
Enter fullscreen mode Exit fullscreen mode

Keys()

Keys() returns all the keys in the storage

fs := fscache.New()

keys := fs.Keys()
fmt.Println("keys: ", keys)
Enter fullscreen mode Exit fullscreen mode

Values()

Values() returns all the values in the storage

fs := fscache.New()

values := fs.Values()
fmt.Println("values: ", values)
Enter fullscreen mode Exit fullscreen mode

KeyValuePairs()

KeyValuePairs() returns an array of key-value pairs of all the data in the storage

fs := fscache.New()

keyValuePairs := fs.KeyValuePairs()
fmt.Println("keyValuePairs: ", keyValuePairs)
Enter fullscreen mode Exit fullscreen mode

This sample code above shows how easy it is to use this library in your Golang projects. You can check out fs-cache on GitHub.

Top comments (0)