Categorygithub.com/jiyamathias/fs-cache
modulepackage
1.4.3
Repository: https://github.com/jiyamathias/fs-cache.git
Documentation: pkg.go.dev

# README

fs-cache

fs-cache provides a quick way to store and retrieve frequently accessed data, significantly enhancing your application performance and reducing database queries / API calls.

Features

  • KeyStore storage

  • DataStore storage

Installation

go get github.com/jiyamathias/fs-cache@latest

Import

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

KeyStore storage

KeyStore gives you a Redis-like feature similarly as you would with a Redis database.

Set()

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

fs := fscache.New()

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

Get()

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

fs := fscache.New()

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

fmt.Println("key1:", result)

DataStore storage

DataStore gives you an SQL/NoSQL-like feature.

Create()

Create is used to insert a new record into the storage.

type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}
fs := fscache.New()

var user User
user.Name = "jane doe"
user.Age = 20

if err := fs.DataStore().Namespace(User{}).Create(user); err != nil {
	fmt.Println(err)
}
  • First()

First is used when you expect a unique record and decodes it into the param object.

fs := fscache.New()

// filter out record of age 35
filter := map[string]interface{}{
	"age": 35,
}

var response User
if err := fs.DataStore().Namespace(User{}).First(filter, &response); err != nil {
	fmt.Println(err)
}

fmt.Println(response)
  • Find()

Find is used when you expect multiple records and decodes it into the param object.

fs := fscache.New()

// filter out records with of age 35
filter := map[string]interface{}{
	"age": 35,
}

var response []User
if err := fs.DataStore().Namespace(User{}).Find(filter, &response); err != nil {
	fmt.Println(err)
}

fmt.Println(response)
  • Sync() - MySQL DB

You can use the Sync method to synchronize the records in the cache to your live sql database.

fs := fscache.New()

ns := fs.DataStore().Namespace(User{})
db := gorm.Open(nil, &gorm.Config{})
ns.ConnectSQLDB(db).Sync(1 * time.Second)
  • Sync() - Mongo DB

You can use the Sync method to synchronize the records in the cache to your live mongodb.

fs := fscache.New()

ns := fs.DataStore().Namespace(User{})
db := &mongo.Database{} // you mongodb database connections
ns.ConnectMongoDB(db).Sync(context.Background(), 1 * time.Second)

Contributions

Anyone can contribute to this library ;). So, feel free to improve on and add new features. I await your pull requests.

# Functions

New initializes an instance of the in-memory storage cache.

# Constants

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

# Variables

ErrFilterParams filter params cannot be nil.
ErrKeyExists key already exists.
ErrKeyNotFound key not found.
ErrRecordNotFound record not found.
MemgodbStorage storage instance.

# Structs

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

# Interfaces

No description provided by the author

# Type aliases

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