Giter Site home page Giter Site logo

fs-cache's Introduction

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

  • Memdis storage

  • Memgodb storage

Installation

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

Import

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

Memdis storage

Memdis 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.Memdis().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.Memdis().Get("key1")
if err != nil {
	fmt.Println("error getting key 1:", err)
}

fmt.Println("key1:", result)

Memgodb storage

Memgodb gives you a MongoDB-like feature similarly as you would with a MondoDB database.

Persist()

Persist is used to write data to file. All data will be saved into a JSON file.

This method will make sure all your data are saved. A cronjob runs ever minute and writes your data into a JSON file to ensure data integrity

fs := fscache.New()

if err := fs.Memgodb().Persist(); err != nil {
	fmt.Println(err)
}

Insert()

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

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

// to insert single record
var user User
user.Name = "jane doe"
user.Age = 20

if err := fs.Memgodb().Collection(User{}).Insert(user); err != nil {
	fmt.Println(err)
}

// to insert multiple records
var users = []struct {
		Name string `json:"name"`
		Age  int    `json:"age"`
	}{
	{Name: "Jane doe",
		Age: 25},
	{Name: "Jane dice",
		Age: 20},
}

if err := fs.Memgodb().Collection("user").Insert(users); err != nil {
	fmt.Println(err)
}

InsertFromJsonFile()

InsertFromJsonFile adds records into the storage from a JSON file.

fs := fscache.New()

if err := fs.Memgodb().Collection("user").InsertFromJsonFile("path to JSON file"); err != nil {
	fmt.Println(err)
}

Filter()

Filter is used to filter records from the storage. It has two methods which are First() and All().

  • First()

First is a method available in Filter(), it returns the first matching record from the filter.

fs := fscache.New()

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

result, err := fs.Memgodb().Collection(User{}).Filter(filter).First()
if err != nil {
	fmt.Println(err)
}

fmt.Println(result)
  • All()

All is a method available in Filter(), it returns the all matching records from the filter.

fs := fscache.New()

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

// to get all records with matching filter from the storage
matchingRecords, err := fs.Memgodb().Collection(User{}).Filter(filter).All()
if err != nil {
	fmt.Println(err)
}
fmt.Println(matchingRecords)

// to get all records from the collection from the storage
allRecords, err := fs.Memgodb().Collection(User{}).Filter(nil).All()
if err != nil {
	fmt.Println(err)
}

fmt.Println(allRecords)

For an exhaustive documentation see the examples folder https://github.com/iqquee/fs-cache/tree/main/example

Contributions

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

fs-cache's People

Contributors

iqquee avatar ccoveille avatar dependabot[bot] avatar knbr13 avatar

Stargazers

Thenagi avatar Yiğithan avatar  avatar  avatar MacBobby Chibuzor avatar Hellozmc avatar  avatar

Watchers

 avatar  avatar

Forkers

ccoveille knbr13

fs-cache's Issues

Issue in Update()

// to insert multiple records
var bucket = []struct {
	Key1 string `json:"key1"`
	Key2 string `json:"key2"`
}{
{
	Key1: "Jane doe",
	Key2: "value1",
},
{
	Key1: "Jane dice",
	Key2: "value2",
},
}

fs := fscache.New()

if err := fs.Memgodb().Collection("bucket").Insert(bucket); err != nil {
    fmt.Println("ERR1:", err)
}

// to get all records from the collection from the storage
allRecords, err := fs.Memgodb().Collection("bucket").Filter(nil).All()
if err != nil {
    fmt.Println("ERR2:", err)
}

fmt.Println("Origin:", allRecords)

filter := map[string]interface{}{
    "key1": "Jane doe",
}
update := map[string]interface{}{
    "key2": "value3",
}

if err := fs.Memgodb().Collection("bucket").Update(filter, update).One(); err != nil {
    fmt.Println("ERR3:", err)
}

// to get all records from the collection from the storage
allRecords2, err := fs.Memgodb().Collection("bucket").Filter(nil).All()
if err != nil {
    fmt.Println("ERR4:", err)
}

fmt.Println("Updated:", allRecords2)

Result:

Origin: [
map[colName:buckets createdAt:... id:... key1:Jane doe key2:value1 updatedAt:<nil>]
map[colName:buckets createdAt:... id:... key1:Jane dice key2:value2 updatedAt:<nil>]
]
Updated: [
map[colName:buckets createdAt:... id:... key1:value3 key2:value1 updatedAt:...]
map[colName:buckets createdAt:... id:... key1:Jane dice key2:value2 updatedAt:<nil>]
]

Expected:

Origin: [
map[colName:buckets createdAt:... id:... key1:Jane doe key2:value1 updatedAt:<nil>]
map[colName:buckets createdAt:... id:... key1:Jane dice key2:value2 updatedAt:<nil>]
]
Updated: [
map[colName:buckets createdAt:... id:... key1:Jane doe key2:value3 updatedAt:...]
map[colName:buckets createdAt:... id:... key1:Jane dice key2:value2 updatedAt:<nil>]
]

Should replace the value of the key2 field with value3 for the first element whose key1 identifier is equal to John doe.
Instead, ignores the update key (key2), but uses the filter key (key1) to determine which field to update.

implement delete record on expiry

Yet to implement the functionality for automatically deleting a record when the duration has already expired. Although you can set a duration on a particular record with the Set(), the functionality of deleting the record when it has expired is yet to be implemented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.