8000 GitHub - moypray/cache
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

moypray/cache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
< A5C3 div class="react-directory-truncate">llrb-stats.go
 
 
 
 
 
 
 
 

Repository files navigation

Cache

This is a cache system written by Golang and base on red black tree.

You can simply use this project to build a buffer/cache system for your own project or just use the red black tree to do some other interesting things.

FYI

This project is based on GoLLRB which was written by Petar Maymounkov.

Thanks for the great work of him and you can follow him on Twitter @maymounkov!

The origin project GoLLRB seems have slept for a long time and will no longer to be maintained, so I think it's better to open a new repo and keep it fresh.

If anyone has any objections, please let me know.

Installation

With a healthy Go language installed, simply run go get github.com/HuKeping/cache

Example

A simple case for int items.

package main

import (
    "fmt"
    "github.com/HuKeping/cache"
)

func Print(item cache.Item, buffer *cache.LLRB) bool {
    i, ok := item.(cache.Int)
    if !ok {
        return false
    }   
    fmt.Println(int(i))
    return true
}

func main() {
    buffer := cache.New()
    buffer.Add(cache.Int(1), cache.MOD_NOREPLACE)
    buffer.Add(cache.Int(2), cache.MOD_NOREPLACE)
    buffer.Add(cache.Int(3), cache.MOD_NOREPLACE)
    buffer.Add(cache.Int(4), cache.MOD_NOREPLACE)
    buffer.DeleteMin()
    buffer.Delete(cache.Int(4))
    buffer.AscendGreaterOrEqual(buffer.Min(), Print)
}

A simple case for string items and acsend order in a range

package main

import (
	"fmt"
	"github.com/HuKeping/cache"
)

func Print(item cache.Item, buffer *cache.LLRB) bool {
	i, ok := item.(cache.String)
	if !ok {
		return false
	}
	fmt.Println(cache.String(i))
	return true
}

func main() {
	buffer := cache.New()

	// For now it is not ordered.
	order := []cache.String{
		"ab", "aba", "abc", "a", "aa", "aaa", "b", "a-", "a!",
	}

	for _, i := range order {
		buffer.Add(cache.String(i), cache.MOD_REPLACE)
	}

	// We expect to get `ab`, `aba` and `abc`, otherwise something wrong happened.
	buffer.AscendRange(cache.String("ab"), cache.String("ac"), Print)
}

A quite interesting case for struct items.

package main

import (
	"fmt"
	"github.com/HuKeping/cache"
	"time"
)

type Var struct {
	Expiry time.Time `json:"expiry,omitempty"`
	ID     string    `json:"id",omitempty`
}

// We will order the node by `Time`
func (x Var) Less(than cache.Item) bool {
	return x.Expiry.Before(than.(Var).Expiry)
}

func PrintVar(item cache.Item, buffer *cache.LLRB) bool {
	i, ok := item.(Var)
	if !ok {
		return false
	}

	if i.Expiry.Before(time.Now()) {
		fmt.Printf("Item expired and to be deleted : %v\n", i)
		buffer.Delete(i)
	}
	return true
}

func main() {
	buffer := cache.New()
	var1 := Var{
		Expiry: time.Now().Add(time.Second * 10),
		ID:     "var1",
	}
	var2 := Var{
		Expiry: time.Now().Add(time.Second * 20),
		ID:     "var2",
	}
	var3 := Var{
		Expiry: time.Now().Add(time.Second * 30),
		ID:     "var3",
	}
	var4 := Var{
		Expiry: time.Now().Add(time.Second * 40),
		ID:     "var4",
	}
	var5 := Var{
		Expiry: time.Now().Add(time.Second * 50),
		ID:     "var5",
	}

	buffer.Add(var1, cache.MOD_REPLACE)
	buffer.Add(var2, cache.MOD_REPLACE)
	buffer.Add(var3, cache.MOD_REPLACE)
	buffer.Add(var4, cache.MOD_REPLACE)
	buffer.Add(var5, cache.MOD_REPLACE)

	go func() {
		for {
			time.Sleep(time.Second * 10)
			fmt.Printf("[VAR] Refreshing begin ...\n")
			buffer.AscendGreaterOrEqual(buffer.Min(), PrintVar)
			fmt.Printf("[VAR] Refreshing end...\n\n")
		}
	}()

	time.Sleep(time.Minute)
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%
0