GithubHelp home page GithubHelp logo

cache's Introduction


LRU Cache

Build Status GoDoc

Install

  1. go get github.com/chai2010/cache
  2. go run hello.go

Example

Simple GC Object

This is a simple example:

package main

import (
	"fmt"

	"github.com/chai2010/cache"
)

func main() {
	c := cache.NewLRUCache(100)
	defer c.Close()

	c.Set("key1", "value1", 1)
	value1 := c.Value("key1").(string)
	fmt.Println("key1:", value1)

	c.Set("key2", "value2", 1)
	value2 := c.Value("key2", "null").(string)
	fmt.Println("key2:", value2)

	value3 := c.Value("key3", "null").(string)
	fmt.Println("key3:", value3)

	value4 := c.Value("key4") // value4 is nil
	fmt.Println("key4:", value4)

	fmt.Println("Done")
}

Output:

key1: value1
key2: value2
key3: null
key4: <nil>
Done

Non GC Object

Support non GC object, such as os.File or some cgo memory.

package main

import (
	"fmt"

	"github.com/chai2010/cache"
)

func main() {
	c := cache.NewLRUCache(10)
	defer c.Close()

	id0 := c.NewId()
	id1 := c.NewId()
	id2 := c.NewId()
	fmt.Println("id0:", id0)
	fmt.Println("id1:", id1)
	fmt.Println("id2:", id2)

	// add new
	v1 := "data:123"
	h1 := c.Insert("123", "data:123", len("data:123"), func(key string, value interface{}) {
		fmt.Printf("deleter(%q:%q)\n", key, value)
	})

	// fetch ok
	v2, h2, ok := c.Lookup("123")
	assert(ok)
	assert(h2 != nil)

	// remove
	c.Erase("123")

	// fetch failed
	_, h3, ok := c.Lookup("123")
	assert(!ok)
	assert(h3 == nil)

	// h1&h2 still valid!
	fmt.Printf("user1(%s)\n", v1)
	fmt.Printf("user2(%s)\n", v2.(string))

	// release h1
	// because the h2 handle the value, so the deleter is not ivoked!
	h1.Close()

	// invoke the deleter
	fmt.Println("invoke deleter(123) begin")
	h2.Close()
	fmt.Println("invoke deleter(123) end")

	// add new
	h4 := c.Insert("abc", "data:abc", len("data:abc"), func(key string, value interface{}) {
		fmt.Printf("deleter(%q:%q)\n", key, value)
	})
	// release h4
	// because the cache handle the value, so the deleter is not ivoked!
	h4.Close()

	// cache length
	length := c.Length()
	assert(length == 1)

	// cache size
	size := c.Size()
	assert(size == 8, "size:", size)

	// add h5
	// this will cause the capacity(10) overflow, so the h4 deleter will be invoked
	fmt.Println("invoke deleter(h4) begin")
	h5 := c.Insert("456", "data:456", len("data:456"), func(key string, value interface{}) {
		fmt.Printf("deleter(%q:%q)\n", key, value)
	})
	fmt.Println("invoke deleter(h4) end")

	// must release all handles
	h5.Close()

	// stats
	fmt.Println("StatsJSON:", c.StatsJSON())

	// done
	fmt.Println("Done")
}

func assert(v bool, a ...interface{}) {
	if !v {
		panic(fmt.Sprint(a...))
	}
}

Output:

id0: 1
id1: 2
id2: 3
user1(data:123)
user2(data:123)
invoke deleter(123) begin
deleter("123":"data:123")
invoke deleter(123) end
invoke deleter(h4) begin
deleter("abc":"data:abc")
invoke deleter(h4) end
StatsJSON: {
        "Length": 1,
        "Size": 8,
        "Capacity": 10,
        "OldestAccess": "2015-08-21 18:00:24.0119469 +0800 CST"
}
Done
deleter("456":"data:456")

BUGS

Report bugs to [email protected].

Thanks!

cache's People

Contributors

chai2010 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cache's Issues

add example for realtime worker

Shared cache:

dataCache := cache.NewLRUCache(32)
defer dataCache.Close()

todoCache := cache.NewLRUCache(32)
defer todoCache.Close()

Forground user:

// if cache missing, push to todoCache
if v := dataCache.Value(key); v == nil {
    todoCache.PushFront(key, func() {
        // load data slowly
        // put data to dataCache
    }, 1)
}

Background worker:

for {
    if h := todoCache.PopFront(); h != nil {
        h.Value().(func())() // do the work
        h.Release()
    }
}

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.