Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ go get stathat.com/c/consistent

git submodule update --init
cd rocksdb && make static_lib

go build -o cache-benchmark/cache-benchmark cache-benchmark/main.go
go build -o client/client client/main.go

sudo apt-get install make g++ libz-dev libsnappy-dev libboost-dev liblz4-dev libzstd-dev
3 changes: 2 additions & 1 deletion cache-benchmark/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"./cacheClient"
"flag"
"fmt"
"math/rand"
"strings"
"time"

"github.com/stuarthu/go-implement-your-cache-server/cache-benchmark/cacheClient"
)

type statistic struct {
Expand Down
3 changes: 2 additions & 1 deletion chapter1/server/http/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package http

import (
"../cache"
"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/cache"

"net/http"
)

Expand Down
4 changes: 2 additions & 2 deletions chapter1/server/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"./cache"
"./http"
"github.com/stuarthu/go-implement-your-cache-server/chapter1/server/cache"
"github.com/stuarthu/go-implement-your-cache-server/chapter1/server/http"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions chapter1/test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ curl 127.0.0.1:12345/cache/testkey -XDELETE

curl 127.0.0.1:12345/status

./cache-benchmark -type http -n 100000 -r 100000 -t set
./cache-benchmark/cache-benchmark -type http -n 100000 -r 100000 -t set

./cache-benchmark -type http -n 100000 -r 100000 -t get
./cache-benchmark/cache-benchmark -type http -n 100000 -r 100000 -t get

redis-benchmark -c 1 -n 100000 -d 1000 -t set,get -r 100000
6 changes: 3 additions & 3 deletions chapter2/server/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"./cache"
"./http"
"./tcp"
"github.com/stuarthu/go-implement-your-cache-server/chapter2/server/cache"
"github.com/stuarthu/go-implement-your-cache-server/chapter2/server/http"
"github.com/stuarthu/go-implement-your-cache-server/chapter2/server/tcp"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion chapter2/server/tcp/new.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package tcp

import (
"../cache"
"net"

"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/cache"
)

type Server struct {
Expand Down
10 changes: 5 additions & 5 deletions chapter2/test/test.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
./client -c set -k testkey -v testvalue
./client/client -c set -k testkey -v testvalue

./client -c get -k testkey
./client/client -c get -k testkey

curl 127.0.0.1:12345/status

./client -c del -k testkey
./client/client -c del -k testkey

curl 127.0.0.1:12345/status

./cache-benchmark -type tcp -n 100000 -r 100000 -t set
./cache-benchmark/cache-benchmark -type tcp -n 100000 -r 100000 -t set

./cache-benchmark -type tcp -n 100000 -r 100000 -t get
./cache-benchmark/cache-benchmark -type tcp -n 100000 -r 100000 -t get
1 change: 0 additions & 1 deletion chapter3/server/cache/cache.go

This file was deleted.

8 changes: 8 additions & 0 deletions chapter3/server/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cache

type Cache interface {
Set(string, []byte) error
Get(string) ([]byte, error)
Del(string) error
GetStat() Stat
}
1 change: 0 additions & 1 deletion chapter3/server/cache/inmemory.go

This file was deleted.

46 changes: 46 additions & 0 deletions chapter3/server/cache/inmemory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cache

import "sync"

type inMemoryCache struct {
c map[string][]byte
mutex sync.RWMutex
Stat
}

func (c *inMemoryCache) Set(k string, v []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
tmp, exist := c.c[k]
if exist {
c.del(k, tmp)
}
c.c[k] = v
c.add(k, v)
return nil
}

func (c *inMemoryCache) Get(k string) ([]byte, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.c[k], nil
}

func (c *inMemoryCache) Del(k string) error {
c.mutex.Lock()
defer c.mutex.Unlock()
v, exist := c.c[k]
if exist {
delete(c.c, k)
c.del(k, v)
}
return nil
}

func (c *inMemoryCache) GetStat() Stat {
return c.Stat
}

func newInMemoryCache() *inMemoryCache {
return &inMemoryCache{make(map[string][]byte), sync.RWMutex{}, Stat{}}
}
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cache

// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"

type rocksdbCache struct {
Expand Down
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb_del.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
// #include <stdlib.h>
// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"
import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
// #include <stdlib.h>
// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"
import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb_getstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
// #include <stdlib.h>
// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"
import (
"regexp"
Expand Down
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cache

// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"
import "runtime"

Expand Down
2 changes: 1 addition & 1 deletion chapter3/server/cache/rocksdb_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
// #include <stdlib.h>
// #include "rocksdb/c.h"
// #cgo CFLAGS: -I${SRCDIR}/../../../rocksdb/include
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lpthread -lsnappy -lstdc++ -lm -O3
// #cgo LDFLAGS: -L${SRCDIR}/../../../rocksdb -lrocksdb -lz -lbz2 -llz4 -lzstd -lpthread -lsnappy -lstdc++ -lm -O3
import "C"
import (
"errors"
Expand Down
1 change: 0 additions & 1 deletion chapter3/server/cache/stat.go

This file was deleted.

19 changes: 19 additions & 0 deletions chapter3/server/cache/stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cache

type Stat struct {
Count int64
KeySize int64
ValueSize int64
}

func (s *Stat) add(k string, v []byte) {
s.Count += 1
s.KeySize += int64(len(k))
s.ValueSize += int64(len(v))
}

func (s *Stat) del(k string, v []byte) {
s.Count -= 1
s.KeySize -= int64(len(k))
s.ValueSize -= int64(len(v))
}
1 change: 0 additions & 1 deletion chapter3/server/http

This file was deleted.

59 changes: 59 additions & 0 deletions chapter3/server/http/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package http

import (
"io/ioutil"
"log"
"net/http"
"strings"
)

type cacheHandler struct {
*Server
}

func (h *cacheHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
key := strings.Split(r.URL.EscapedPath(), "/")[2]
if len(key) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
m := r.Method
if m == http.MethodPut {
b, _ := ioutil.ReadAll(r.Body)
if len(b) != 0 {
e := h.Set(key, b)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
}
}
return
}
if m == http.MethodGet {
b, e := h.Get(key)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(b) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write(b)
return
}
if m == http.MethodDelete {
e := h.Del(key)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}

func (s *Server) cacheHandler() http.Handler {
return &cacheHandler{s}
}
21 changes: 21 additions & 0 deletions chapter3/server/http/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package http

import (
"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/cache"

"net/http"
)

type Server struct {
cache.Cache
}

func (s *Server) Listen() {
http.Handle("/cache/", s.cacheHandler())
http.Handle("/status", s.statusHandler())
http.ListenAndServe(":12345", nil)
}

func New(c cache.Cache) *Server {
return &Server{c}
}
29 changes: 29 additions & 0 deletions chapter3/server/http/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package http

import (
"encoding/json"
"log"
"net/http"
)

type statusHandler struct {
*Server
}

func (h *statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
b, e := json.Marshal(h.GetStat())
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(b)
}

func (s *Server) statusHandler() http.Handler {
return &statusHandler{s}
}
7 changes: 4 additions & 3 deletions chapter3/server/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"./cache"
"./http"
"./tcp"
"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/cache"
"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/http"
"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/tcp"

"flag"
"log"
)
Expand Down
1 change: 0 additions & 1 deletion chapter3/server/tcp

This file was deleted.

29 changes: 29 additions & 0 deletions chapter3/server/tcp/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tcp

import (
"net"

"github.com/stuarthu/go-implement-your-cache-server/chapter3/server/cache"
)

type Server struct {
cache.Cache
}

func (s *Server) Listen() {
l, e := net.Listen("tcp", ":12346")
if e != nil {
panic(e)
}
for {
c, e := l.Accept()
if e != nil {
panic(e)
}
go s.process(c)
}
}

func New(c cache.Cache) *Server {
return &Server{c}
}
Loading