Cookies Psst! Do you accept cookies?

We use cookies to enhance and personalise your experience.
Please accept our cookies. Checkout our Cookie Policy for more information.

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

Import

fscache "github.com/iqquee/fs-cache"

Usage

Debug()

Debug() enables debug to get certain logs

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

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)
}

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)

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)

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)

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)
}

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)
}

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)
}

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)

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)
}

Size()

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

fs := fscache.New()

size := fs.Size()
fmt.Println("total size: ", size)

Keys()

Keys() returns all the keys in the storage

fs := fscache.New()

keys := fs.Keys()
fmt.Println("keys: ", keys)

Values()

Values() returns all the values in the storage

fs := fscache.New()

values := fs.Values()
fmt.Println("values: ", values)

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)

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.

Last Stories

What's your thoughts?

Please Register or Login to your account to be able to submit your comment.