mcacheZero

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 6 Imported by: 0

README

mcacheZero

Key/Value Store Caching System with LRU Algorithm.

This project is developed for personal used. You may use it by your own risk and purpose !!!

Example Usage: Write Always Mode

package main

import (
	"fmt"
	"github.com/authapon/mcachezero"
	"time"
	"errors"
)

func main() {
	c := mcachezero.New(2)  //make cache with size 2
	c.SetWriteAlways()  //default Write Mode - call write function every time set data
	c.SetWriteFunc(writer)   // define the write function
	c.SetReadFunc(reader)

	data, err := c.Get("a1")
	if err != nil {
		fmt.Printf("error\n")
	} else {
		fmt.Printf("Get a1 - %s\n", data.(string))
	}

	data, err = c.Get("a2")
	if err != nil {
		fmt.Printf("error\n")
	} else {
		fmt.Printf("Get a2 - %s\n", data.(string))
	}

	data, err = c.Get("a3")
	if err != nil {
		fmt.Printf("Error reading a3\n")
	} else {
		fmt.Printf("Get a3 - %s\n", data.(string))
	}

	fmt.Printf("Set a4 - ddddddd\n")
	c.Set("a4", "ddddddd")

	fmt.Printf("Set a5 - eeeeeee\n")
	c.Set("a5", "eeeeeee")

	time.Sleep(5*time.Hour)
}

func writer(key string, value interface{}) {
	fmt.Printf("Write %s - %s\n", key, value.(string))
}

func reader(key string)(interface{}, error) {
	if key == "a1" {
		return "aaaaaa", nil
	} else if key == "a2" {
		return "bbbbbb", nil
	} else {
		return nil, errors.New("Data not found")
	}
}

Output:

Get a1 - aaaaaa
Get a2 - bbbbbb
Error reading a3
Set a4 - ddddddd
Set a5 - eeeeeee
Write a4 - ddddddd
Write a5 - eeeeeee


Example Usage: Write Evict Mode

package main

import (
	"errors"
	"fmt"
	"github.com/authapon/mcachezero"
	"time"
)

func main() {
	c := mcachezero.New(2) //make cache with size 2
	c.SetWriteEvict()      //Write Evict Mode - call write function when data is evicted
	c.SetWriteFunc(writer) // define the write function
	c.SetReadFunc(reader)
	c.SetDeleteFunc(deleter)

	data, err := c.Get("a1")
	if err != nil {
		fmt.Printf("error\n")
	} else {
		fmt.Printf("Get a1 - %s\n", data.(string))
	}

	data, err = c.Get("a2")
	if err != nil {
		fmt.Printf("error\n")
	} else {
		fmt.Printf("Get a2 - %s\n", data.(string))
	}

	data, err = c.Get("a3")
	if err != nil {
		fmt.Printf("Error reading a3\n")
	} else {
		fmt.Printf("Get a3 - %s\n", data.(string))
	}

	fmt.Printf("Set a4 - ddddddd\n")
	c.Set("a4", "ddddddd")

	fmt.Printf("Set a5 - eeeeeee\n")
	c.Set("a5", "eeeeeee")

	fmt.Printf("Set a1 - zzzzzz\n")
	c.Set("a1", "zzzzzz")

	data, err = c.Get("a1")
	if err != nil {
		fmt.Printf("error\n")
	} else {
		fmt.Printf("Get a1 - %s\n", data.(string))
	}
	fmt.Printf("Sync !!!\n")
	c.Sync()

	fmt.Printf("Try to delete a1\n")
	c.Delete("a1")

	time.Sleep(5 * time.Hour)
}

func writer(key string, value interface{}) {
	fmt.Printf("Write %s - %s\n", key, value.(string))
}

func reader(key string) (interface{}, error) {
	if key == "a1" {
		return "aaaaaa", nil
	} else if key == "a2" {
		return "bbbbbb", nil
	} else {
		return nil, errors.New("Data not found")
	}
}

func deleter(key string) {
	fmt.Printf("Delete %s\n", key)
}

output:

Get a1 - aaaaaa
Get a2 - bbbbbb
Error reading a3
Set a4 - ddddddd
Set a5 - eeeeeee
Set a1 - zzzzzz
Get a1 - zzzzzz
Sync !!!
Write a4 - ddddddd
Try to delete a1
Write a1 - zzzzzz
Write a5 - eeeeeee
Delete a1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(size int) *Cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

func (*Cache) EvictAct

func (c *Cache) EvictAct()

func (*Cache) Flush

func (c *Cache) Flush()

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, error)

func (*Cache) Keys

func (c *Cache) Keys() []string

func (*Cache) Len

func (c *Cache) Len() int

func (*Cache) Purge

func (c *Cache) Purge()

func (*Cache) Remove

func (c *Cache) Remove(key string)

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

func (*Cache) SetDeleteFunc

func (c *Cache) SetDeleteFunc(fn deleteFuncType)

func (*Cache) SetExpireDuration

func (c *Cache) SetExpireDuration(expire time.Duration)

func (*Cache) SetReadFunc

func (c *Cache) SetReadFunc(fn readFuncType)

func (*Cache) SetWriteAlways

func (c *Cache) SetWriteAlways()

func (*Cache) SetWriteEvict

func (c *Cache) SetWriteEvict()

func (*Cache) SetWriteFunc

func (c *Cache) SetWriteFunc(fn writeFuncType)

func (*Cache) Sync

func (c *Cache) Sync()

func (*Cache) WriteWaiting

func (c *Cache) WriteWaiting()

Jump to

Keyboard shortcuts

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