Skip to content

Instantly share code, notes, and snippets.

@davidbirdsong
Created June 15, 2016 23:36
Show Gist options
  • Save davidbirdsong/fab9ccdc6e16a61081631e432e5d5229 to your computer and use it in GitHub Desktop.
Save davidbirdsong/fab9ccdc6e16a61081631e432e5d5229 to your computer and use it in GitHub Desktop.
demonstrate ordering of calls to leveldb.Cache
package main
import (
"github.com/syndtr/goleveldb/leveldb/cache"
"fmt"
"sync"
"time"
)
var cCache = cache.NewCache(nil)
func main() {
var initial_val = "handle1 setter"
getfunc := func() (int, cache.Value) {
return 1, initial_val
}
wg := &sync.WaitGroup{}
handle1 := cCache.Get(0, 0, getfunc)
fmt.Println("cache pop")
wg.Add(1)
go func() {
fmt.Println("waiting to release")
time.Sleep(time.Second * 3)
fmt.Println("first release start")
handle1.Release()
fmt.Println("first release finish")
wg.Done()
}()
wg.Add(1)
go func() {
fmt.Println("delete call start")
cCache.Delete(0, 0, func() {
fmt.Println("delete func firing going to sleep")
time.Sleep(time.Second * 1)
/*
cCache.Get(0, 0, func() (int, cache.Value) {
fmt.Println("getter from delete running ")
return 1, "delete"
})
*/
fmt.Println("delete fires and blocks 3 secs")
time.Sleep(time.Second * 3)
fmt.Println("delete returns")
})
fmt.Println("delete call done")
wg.Done()
}()
getfunc = func() (int, cache.Value) {
return 1, "new value"
}
for {
//<-time.After(time.Millisecond * 10)
if handle2 := cCache.Get(0, 0, getfunc); handle2 == nil {
break
} else if s, ok := handle2.Value().(string); !ok {
panic(fmt.Errorf("WTF!"))
} else {
//fmt.Printf("cache value is: %s\n", s)
handle2.Release()
if s != initial_val {
fmt.Printf("ending loop after cache rewrite: %s\n", s)
break
}
}
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment