fscache

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2024 License: MIT Imports: 11 Imported by: 0

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

  • 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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound key not found
	ErrKeyNotFound = errors.New("key not found")
	// ErrKeyExists key already exists
	ErrKeyExists = errors.New("key already exist")
)
View Source
var (
	// ErrRecordNotFound record not found
	ErrRecordNotFound = errors.New("record not found")
	// ErrFilterParams filter params cannot be nil
	ErrFilterParams = errors.New("filter params cannot be nil")
)
View Source
var (
	// MemgodbStorage storage instance
	MemgodbStorage []any
)

Functions

This section is empty.

Types

type Cache

type Cache struct {
	MemdisInstance  Memdis
	MemgodbInstance Memgodb
	// contains filtered or unexported fields
}

Cache object

func (*Cache) Debug

func (c *Cache) Debug(w io.Writer)

Debug() enables debug to get certain logs

func (*Cache) Memdis added in v1.1.0

func (c *Cache) Memdis() *Memdis

KeyValue returns methods for key-value pair storage

func (*Cache) Memgodb added in v1.1.0

func (c *Cache) Memgodb() *Memgodb

Memgodb returns methods for Memgodb-like storage

type Collection added in v1.1.0

type Collection struct {
	// contains filtered or unexported fields
}

Collection object

func (*Collection) Delete added in v1.1.0

func (c *Collection) Delete(filter map[string]any) *Delete

Delete is used to delete a new record from the storage. It has two methods which are One() and Many().

func (*Collection) Filter added in v1.1.0

func (c *Collection) Filter(filter map[string]any) *Filter

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

func (*Collection) Insert added in v1.1.0

func (c *Collection) Insert(obj any) error

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

func (*Collection) InsertFromJsonFile added in v1.2.1

func (c *Collection) InsertFromJsonFile(fileLocation string) error

InsertFromJsonFile adds records into the storage from a JSON file.

func (*Collection) Update added in v1.1.0

func (c *Collection) Update(filter, obj map[string]any) *Update

Update is used to update an existing record in the storage. It has a method which is One().

type Delete added in v1.1.0

type Delete struct {
	// contains filtered or unexported fields
}

Delete object implements One() and All()

func (*Delete) All added in v1.1.0

func (d *Delete) All() error

All is a method available in Delete(), it deletes matching records from the filter and returns an error if any.

func (*Delete) One added in v1.1.0

func (d *Delete) One() error

One is a method available in Delete(), it deletes a record and returns an error if any.

type Filter added in v1.1.0

type Filter struct {
	// contains filtered or unexported fields
}

Filter object implements One() and All()

func (*Filter) All added in v1.1.0

func (f *Filter) All() ([]map[string]any, error)

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

func (*Filter) First added in v1.1.0

func (f *Filter) First() (map[string]any, error)

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

type Memdis added in v1.1.0

type Memdis struct {
	// contains filtered or unexported fields
}

Memdis object instance

func (*Memdis) Clear added in v1.1.0

func (md *Memdis) Clear() error

Clear() deletes all data from the in-memory storage

func (*Memdis) Del added in v1.1.0

func (md *Memdis) Del(key string) error

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

func (*Memdis) Get added in v1.1.0

func (md *Memdis) Get(key string) (any, error)

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

func (*Memdis) GetMany added in v1.1.0

func (md *Memdis) GetMany(keys []string) []map[string]any

GetMany() retrieves data with matching keys from the in-memory storage

func (*Memdis) KeyValuePairs added in v1.1.0

func (md *Memdis) KeyValuePairs() []map[string]any

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

func (*Memdis) Keys added in v1.1.0

func (md *Memdis) Keys() []string

Keys() returns all the keys in the storage

func (*Memdis) OverWrite added in v1.1.0

func (md *Memdis) OverWrite(key string, value any, duration ...time.Duration) error

OverWrite() updates an already set value using it key

func (*Memdis) OverWriteWithKey added in v1.1.0

func (md *Memdis) OverWriteWithKey(prevkey, newKey string, value any, duration ...time.Duration) error

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

func (*Memdis) Set added in v1.1.0

func (md *Memdis) Set(key string, value any, duration ...time.Duration) error

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

func (*Memdis) SetMany added in v1.1.0

func (md *Memdis) SetMany(data []map[string]MemdisData) ([]map[string]any, error)

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

func (*Memdis) Size added in v1.1.0

func (md *Memdis) Size() int

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

func (*Memdis) TypeOf added in v1.1.0

func (md *Memdis) TypeOf(key string) (string, error)

TypeOf() returns the data type of a value

func (*Memdis) Values added in v1.1.0

func (md *Memdis) Values() []any

Values() returns all the values in the storage

type MemdisData added in v1.1.0

type MemdisData struct {
	Value    any
	Duration time.Time
}

MemdisData object

type Memgodb added in v1.1.0

type Memgodb struct {
	// contains filtered or unexported fields
}

Memgodb object instance

func (*Memgodb) Collection added in v1.1.0

func (mg *Memgodb) Collection(col any) *Collection

Collection defines the collection(table) name to perform an operation on it

func (*Memgodb) LoadDefault added in v1.1.0

func (mg *Memgodb) LoadDefault() error

LoadDefault is used to load data from the JSON file saved on the server using Persist() if any.

func (*Memgodb) Persist added in v1.1.0

func (mg *Memgodb) Persist() error

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

type Operations

type Operations interface {
	// Debug() enables debug to get certain logs
	Debug(io.Writer)

	// Memdis gives you a Redis-like feature similarly as you would with a Redis database
	Memdis() *Memdis
	// Memgodb gives you a MongoDB-like feature similarly as you would with a MondoDB database
	Memgodb() *Memgodb
}

Operations lists all available operations on the fs-cache

func New

func New() Operations

New initializes an instance of the in-memory storage cache

type Persist added in v1.1.0

type Persist struct {
	Error error
}

Persist objects implemented Persist() used to persist inserted records

type Update added in v1.1.0

type Update struct {
	// contains filtered or unexported fields
}

Update object implements One() and All()

func (*Update) One added in v1.1.0

func (u *Update) One() error

One is a method available in Update(), it updates matching records from the filter, makes the necessary updates and returns an error if any.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL