Skip to content

Commit

Permalink
Merge pull request #334 from COS301-SE-2024/feat/backend/switching-fr…
Browse files Browse the repository at this point in the history
…om-bigcache-to-go-redis

Feat/backend/switching from bigcache to go redis
  • Loading branch information
waveyboym authored Aug 22, 2024
2 parents b831e09 + 02dd82d commit be356ee
Show file tree
Hide file tree
Showing 31 changed files with 2,704 additions and 2,478 deletions.
4 changes: 3 additions & 1 deletion frontend/occupi-mobile4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ expo-env.d.ts

.env

coverage/
coverage/

.gradle/
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file modified occupi-backend/configs/centrifugo.config.json.gpg
Binary file not shown.
36 changes: 36 additions & 0 deletions occupi-backend/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const (
MiddelwareAT = "MIDDLEWARE_AT"
MiddlewareTarget = "MIDDLEWARE_TARGET"
MiddlewareAgentService = "MW_AGENT_SERVICE"
RedisUsername = "REDIS_USERNAME"
RedisPassword = "REDIS_PASSWORD"
RedisHost = "REDIS_HOST"
RedisPort = "REDIS_PORT"
)

// init viper
Expand Down Expand Up @@ -495,3 +499,35 @@ func GetMiddlewareAgentService() string {
}
return service
}

func GetRedisUsername() string {
username := viper.GetString(RedisUsername)
if username == "" {
username = "REDIS_USERNAME"
}
return username
}

func GetRedisPassword() string {
password := viper.GetString(RedisPassword)
if password == "" {
password = "REDIS_PASSWORD"
}
return password
}

func GetRedisHost() string {
host := viper.GetString(RedisHost)
if host == "" {
host = "REDIS_HOST"
}
return host
}

func GetRedisPort() string {
port := viper.GetString(RedisPort)
if port == "" {
port = "REDIS_PORT"
}
return port
}
Binary file modified occupi-backend/configs/config.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.deployed.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.localhost.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/prod.yaml.gpg
Binary file not shown.
17 changes: 12 additions & 5 deletions occupi-backend/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-webauthn/webauthn/webauthn"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/ipinfo/go/v2/ipinfo/cache"
"github.com/redis/go-redis/v9"

"context"
"fmt"
Expand Down Expand Up @@ -70,24 +71,30 @@ func ConnectToDatabase(args ...string) *mongo.Client {
}

// Create cache
func CreateCache() *bigcache.BigCache {
func CreateCache() *redis.Client {
if GetEnv() == "devlocalhost" || GetEnv() == "devdeployed" || GetEnv() == "devlocalhostdocker" {
fmt.Printf("Cache is disabled in %s mode\n", GetEnv())
logrus.Printf("Cache is disabled in %s mode\n", GetEnv())
return nil
}

config := bigcache.DefaultConfig(time.Duration(GetCacheEviction()) * time.Second) // Set the eviction time to 5 seconds
config.CleanWindow = time.Duration(GetCacheEviction()/2) * time.Second // Set the cleanup interval to 5 seconds
cache, err := bigcache.New(context.Background(), config)
redisUsername := GetRedisUsername()
redisPassword := GetRedisPassword()
redisHost := GetRedisHost()
redisPort := GetRedisPort()

url := fmt.Sprintf("redis://%s:%s@%s:%s/0?protocol=3", redisUsername, redisPassword, redisHost, redisPort)
opts, err := redis.ParseURL(url)
if err != nil {
logrus.Fatal(err)
}

client := redis.NewClient(opts)

fmt.Println("Cache created!")
logrus.Info("Cache created!")

return cache
return client
}

// Create cache for sessions
Expand Down
Binary file modified occupi-backend/configs/test.yaml.gpg
Binary file not shown.
23 changes: 23 additions & 0 deletions occupi-backend/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ services:
condition: service_healthy
centrifugo-dev:
condition: service_started
go-redis-dev:
condition: service_healthy

rabbitmq-dev:
image: rabbitmq:latest
Expand Down Expand Up @@ -56,10 +58,31 @@ services:
soft: 262144
hard: 262144

go-redis-dev:
image: redis:latest
container_name: go-redis-dev
ports:
- "11001:6379"
restart: on-failure:5
environment:
REDIS_USERNAME: $REDIS_USERNAME
REDIS_PASSWORD: $REDIS_PASSWORD
volumes:
- redis-dev:/data
networks:
- webnet
healthcheck:
test: redis-cli ping
interval: 30s
timeout: 30s
retries: 3

volumes:
rabbitmq-dev:
centrifugo-dev:
redis-dev:

networks:
webnet:
external: true

15 changes: 14 additions & 1 deletion occupi-backend/docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ services:
soft: 262144
hard: 262144

go-redis-dev-local-local:
image: redis:latest
container_name: go-redis-dev-local-local
ports:
- "11001:6379"
restart: on-failure:5
environment:
REDIS_USERNAME:
REDIS_PASSWORD:
volumes:
- redis-dev-local-local:/data

volumes:
rabbitmq-dev-local-local:
centrifugo-dev-local-local:
centrifugo-dev-local-local:
redis-dev-local-local:
20 changes: 20 additions & 0 deletions occupi-backend/docker-compose.localdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
condition: service_healthy
centrifugo-dev-local:
condition: service_started
go-redis-dev-local:
condition: service_healthy

rabbitmq-dev-local:
image: rabbitmq:latest
Expand Down Expand Up @@ -44,6 +46,24 @@ services:
soft: 262144
hard: 262144

go-redis-dev-local:
image: redis:latest
container_name: go-redis-dev-local
ports:
- "11000:6379"
restart: on-failure:5
environment:
REDIS_USERNAME:
REDIS_PASSWORD:
volumes:
- redis-dev-local:/data
healthcheck:
test: redis-cli ping
interval: 30s
timeout: 30s
retries: 3

volumes:
rabbitmq-dev-local:
centrifugo-dev-local:
redis-dev-local:
22 changes: 22 additions & 0 deletions occupi-backend/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ services:
condition: service_healthy
centrifugo-prod:
condition: service_started
go-redis-prod:
condition: service_healthy

rabbitmq-prod:
image: rabbitmq:latest
Expand Down Expand Up @@ -56,9 +58,29 @@ services:
soft: 262144
hard: 262144

go-redis-prod:
image: redis:latest
container_name: go-redis-prod
ports:
- "11000:6379"
restart: on-failure:5
environment:
REDIS_USERNAME: $REDIS_USERNAME
REDIS_PASSWORD: $REDIS_PASSWORD
volumes:
- redis-prod:/data
networks:
- webnet
healthcheck:
test: redis-cli ping
interval: 30s
timeout: 30s
retries: 3

volumes:
rabbitmq-prod:
centrifugo-prod:
redis-prod:

networks:
webnet:
Expand Down
4 changes: 4 additions & 0 deletions occupi-backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ require (

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fluent/fluent-logger-golang v1.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-redis/redismock/v9 v9.2.0 // indirect
github.com/go-webauthn/x v0.1.12 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand All @@ -53,6 +56,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
8 changes: 8 additions & 0 deletions occupi-backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/centrifugal/gocent/v3 v3.3.0 h1:xVkqMMtBiGcvV3OGqlTlayWdJoorNoVBQ3X9THKLe14=
github.com/centrifugal/gocent/v3 v3.3.0/go.mod h1:8YWDQG3sX0X1g+BaotihbhawPs6zyYGUxUEk8Ng5a2g=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
Expand All @@ -24,6 +26,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fluent/fluent-logger-golang v1.9.0 h1:zUdY44CHX2oIUc7VTNZc+4m+ORuO/mldQDA7czhWXEg=
github.com/fluent/fluent-logger-golang v1.9.0/go.mod h1:2/HCT/jTy78yGyeNGQLGQsjF3zzzAuy6Xlk6FCMV5eU=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down Expand Up @@ -61,6 +65,8 @@ github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw=
github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0=
github.com/go-webauthn/webauthn v0.11.0 h1:2U0jWuGeoiI+XSZkHPFRtwaYtqmMUsqABtlfSq1rODo=
github.com/go-webauthn/webauthn v0.11.0/go.mod h1:57ZrqsZzD/eboQDVtBkvTdfqFYAh/7IwzdPT+sPWqB0=
github.com/go-webauthn/x v0.1.12 h1:RjQ5cvApzyU/xLCiP+rub0PE4HBZsLggbxGR5ZpUf/A=
Expand Down Expand Up @@ -159,6 +165,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
Expand Down
Loading

0 comments on commit be356ee

Please sign in to comment.