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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/*

# AWS User-specific
.idea/**/aws.xml
Expand Down Expand Up @@ -115,3 +116,8 @@ fabric.properties
.idea/**/azureSettings.xml

# End of https://www.toptal.com/developers/gitignore/api/goland

raft_data
./raft_data/*
./raft_data
/raft_data
4 changes: 4 additions & 0 deletions .idea/gCache.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Go parameters
GOCMD := go
GOBUILD := $(GOCMD) build
GOTEST := $(GOCMD) test
GORUN := $(GOCMD) run
GOBIN := $(shell pwd)/bin
LeaderPort := ":3000"
BINARY_NAME := gCache
RandomPort := $(shell shuf -i 5000-9999 -n 1)

.PHONY: all build run run-follower test clean

all: build

build:
$(GOBUILD) -o "$(GOBIN)/$(BINARY_NAME)"

run:
$(GOBIN)/$(BINARY_NAME) --listenaddr $(LeaderPort)

frun:
$(GOBIN)/$(BINARY_NAME) --leaderaddr $(LeaderPort) --listenaddr :$(RandomPort)

crun:
go run client/main.go

lb:
haproxy -f haproxy.cfg

test:
$(GOTEST)

clean:
rm -rf $(GOBIN)/$(BINARY_NAME)
5 changes: 2 additions & 3 deletions client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/ragoob/gCache/pkg/client"
"github.com/ragoob/gCache/pkg/grpc/client"
"github.com/spf13/cobra"
"os"
"strings"
Expand Down Expand Up @@ -33,7 +33,7 @@ var rootCmd = &cobra.Command{
}

func Execute() {
rootCmd.Flags().StringVarP(&serverAddr, "serveraddr", "s", ":3000", "Server address")
rootCmd.Flags().StringVarP(&serverAddr, "serveraddr", "s", ":8080", "Server address")
defer gClient.Close()

if err := rootCmd.Execute(); err != nil {
Expand All @@ -43,7 +43,6 @@ func Execute() {
}

func runRoot(cmd *cobra.Command, args []string) {

scanner := bufio.NewScanner(os.Stdin)
welcomePrint()

Expand Down
1 change: 1 addition & 0 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (

func main() {
cmd.Execute()

}
15 changes: 14 additions & 1 deletion cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *SetCmd) GetBytes() []byte {
binary.Write(buf, binary.LittleEndian, int32(len(c.Val)))
binary.Write(buf, binary.LittleEndian, c.Val)
binary.Write(buf, binary.LittleEndian, int32(c.Duration))
binary.Write(buf, binary.LittleEndian, c.Replication)
binary.Write(buf, binary.LittleEndian, c.Duration)
return buf.Bytes()
}

Expand Down Expand Up @@ -193,3 +193,16 @@ func parseJoinCommand(r io.Reader) *JoinCmd {
binary.Read(r, binary.LittleEndian, &cmd.Addr)
return cmd
}

func ParseSetCommand(r io.Reader) (*SetCmd, error) {
var cmd Command
if err := binary.Read(r, binary.LittleEndian, &cmd); err != nil {
return nil, err
}
switch cmd {
case Set:
return parseSetCommand(r), nil
default:
return nil, fmt.Errorf("invalid command")
}
}
14 changes: 14 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
type DB interface {
Set([]byte, []byte, time.Duration) error
Get([]byte) ([]byte, error)
Store() map[string][]byte
Clear()
}

type Cache struct {
Expand Down Expand Up @@ -43,3 +45,15 @@ func (c *Cache) Set(key []byte, val []byte, duration time.Duration) error {

return nil
}

func (c *Cache) Store() map[string][]byte {
return c.store
}

func (c *Cache) Clear() {
c.lock.Lock()
defer c.lock.Unlock()

// Clear the existing store
c.store = make(map[string][]byte)
}
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@ module github.com/ragoob/gCache
go 1.20

require (
github.com/Jille/raft-grpc-leader-rpc v1.1.0
github.com/Jille/raft-grpc-transport v1.4.0
github.com/Jille/raftadmin v1.2.0
github.com/google/uuid v1.3.0
github.com/hashicorp/raft v1.5.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.7.0
golang.org/x/net v0.9.0
google.golang.org/grpc v1.56.2
google.golang.org/protobuf v1.31.0
)

require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-msgpack v1.1.5 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
)
Loading