|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/golang/groupcache" |
| 10 | +) |
| 11 | + |
| 12 | +import "C" |
| 13 | + |
| 14 | +// temporary storage to be able to set data |
| 15 | +var Store = map[string]string{} |
| 16 | + |
| 17 | +var peers *groupcache.HTTPPool = nil |
| 18 | +var cache *groupcache.Group = nil |
| 19 | +var srv *http.Server = nil |
| 20 | + |
| 21 | +//export cache_set |
| 22 | +func cache_set(key *C.char, value *C.char) *C.char { |
| 23 | + // set the value in the cache. |
| 24 | + |
| 25 | + // groupcache does not have a way to set a value in the cache (pass through cache), |
| 26 | + // so we fake it by setting value in global store and then getting the value immediately. |
| 27 | + // Ideally, this is switched out with a backend that retrieves the value you want to cache. |
| 28 | + var gkey = C.GoString(key) |
| 29 | + var gvalue = C.GoString(value) |
| 30 | + Store[gkey] = gvalue |
| 31 | + var data string |
| 32 | + cache.Get(context.TODO(), gkey, groupcache.StringSink(&data)) |
| 33 | + delete(Store, gkey) |
| 34 | + return C.CString(data) |
| 35 | +} |
| 36 | + |
| 37 | +//export cache_get |
| 38 | +func cache_get(key *C.char) *C.char { |
| 39 | + // get the value from the cache. |
| 40 | + var data string |
| 41 | + cache.Get(context.TODO(), C.GoString(key), groupcache.StringSink(&data)) |
| 42 | + return C.CString(data) |
| 43 | +} |
| 44 | + |
| 45 | +//export setup |
| 46 | +func setup(addr *C.char, baseUrl *C.char) { |
| 47 | + // setup the cache server. |
| 48 | + done := make(chan bool) |
| 49 | + go func() { |
| 50 | + log.Printf("Setting up cache server node at %s...\n", C.GoString(addr)) |
| 51 | + peers = groupcache.NewHTTPPool(C.GoString(baseUrl)) |
| 52 | + cache = groupcache.NewGroup("Cache", 64<<20, groupcache.GetterFunc( |
| 53 | + func(ctx groupcache.Context, key string, dest groupcache.Sink) error { |
| 54 | + v, ok := Store[key] |
| 55 | + if !ok { |
| 56 | + return errors.New("cache key not found") |
| 57 | + } |
| 58 | + dest.SetBytes([]byte(v)) |
| 59 | + return nil |
| 60 | + })) |
| 61 | + router := http.NewServeMux() |
| 62 | + router.Handle("/", http.HandlerFunc(peers.ServeHTTP)) |
| 63 | + srv := &http.Server{Addr: C.GoString(addr), Handler: router} |
| 64 | + close(done) |
| 65 | + if err := srv.ListenAndServe(); err != nil { |
| 66 | + log.Fatal("ListenAndServe: ", err) |
| 67 | + } |
| 68 | + }() |
| 69 | + <-done |
| 70 | + // do it in a go routine so we don't block |
| 71 | + log.Printf("Running cache server node at %s.\n", C.GoString(addr)) |
| 72 | +} |
| 73 | + |
| 74 | +//export initialized |
| 75 | +func initialized() *C.char { |
| 76 | + // check if cache is initialized. If it is, then we can use it. |
| 77 | + var flag string |
| 78 | + if cache != nil { |
| 79 | + flag = "1" |
| 80 | + } else { |
| 81 | + flag = "0" |
| 82 | + } |
| 83 | + return C.CString(flag) |
| 84 | +} |
| 85 | + |
| 86 | +func main() {} |
0 commit comments