Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/backend/add caching and preloading images #244

Merged
merged 4 commits into from
Jul 27, 2024
Merged
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
4 changes: 3 additions & 1 deletion frontend/occupi-mobile4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ google-services.json
expo-env.d.ts
# @end expo-cli

.env
.env

coverage/
121 changes: 112 additions & 9 deletions occupi-backend/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// unmarshal the user from the cache
var user models.User
userData, err := appsession.Cache.Get(email)
userData, err := appsession.Cache.Get(UserKey(email))

if err != nil {
logrus.Error("key does not exist: ", err)
Expand Down Expand Up @@ -43,7 +43,7 @@
}

// set the user in the cache
if err := appsession.Cache.Set(user.Email, userData); err != nil {
if err := appsession.Cache.Set(UserKey(user.Email), userData); err != nil {
logrus.Error("failed to set user in cache", err)
return
}
Expand All @@ -55,7 +55,7 @@
}

// delete the user from the cache
if err := appsession.Cache.Delete(email); err != nil {
if err := appsession.Cache.Delete(UserKey(email)); err != nil {
logrus.Error("failed to delete user from cache", err)
return
}
Expand All @@ -68,8 +68,7 @@

// unmarshal the otp from the cache
var otpData models.OTP
otpKey := email + otp
otpDataBytes, err := appsession.Cache.Get(otpKey)
otpDataBytes, err := appsession.Cache.Get(OTPKey(email, otp))

if err != nil {
logrus.Error("key does not exist: ", err)
Expand All @@ -90,15 +89,14 @@
}

// marshal the otp
otpKey := otpData.Email + otpData.OTP
otpDataBytes, err := bson.Marshal(otpData)
if err != nil {
logrus.Error("failed to marshall", err)
return
}

// set the otp in the cache
if err := appsession.Cache.Set(otpKey, otpDataBytes); err != nil {
if err := appsession.Cache.Set(OTPKey(otpData.Email, otpData.OTP), otpDataBytes); err != nil {
logrus.Error("failed to set otp in cache", err)
return
}
Expand All @@ -110,9 +108,114 @@
}

// delete the otp from the cache
otpKey := email + otp
if err := appsession.Cache.Delete(otpKey); err != nil {
if err := appsession.Cache.Delete(OTPKey(email, otp)); err != nil {
logrus.Error("failed to delete otp from cache", err)
return
}
}

func SetBooking(appsession *models.AppSession, booking models.Booking) {
if appsession.Cache == nil {
return
}

// marshal the booking
bookingData, err := bson.Marshal(booking)
if err != nil {
logrus.Error("failed to marshall", err)
return

Check warning on line 126 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}

// set the booking in the cache
if err := appsession.Cache.Set(RoomBookingKey(booking.OccupiID), bookingData); err != nil {
logrus.Error("failed to set booking in cache", err)
return

Check warning on line 132 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L131-L132

Added lines #L131 - L132 were not covered by tests
}
}

func GetBooking(appsession *models.AppSession, bookingID string) (models.Booking, error) {
if appsession.Cache == nil {
return models.Booking{}, errors.New("cache not found")
}

// unmarshal the booking from the cache
var booking models.Booking
bookingData, err := appsession.Cache.Get(RoomBookingKey(bookingID))

if err != nil {
logrus.Error("key does not exist: ", err)
return models.Booking{}, err
}

if err := bson.Unmarshal(bookingData, &booking); err != nil {
logrus.Error("failed to unmarshall", err)
return models.Booking{}, err

Check warning on line 152 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}

return booking, nil
}

func DeleteBooking(appsession *models.AppSession, bookingID string) {
if appsession.Cache == nil {
return
}

// delete the booking from the cache
if err := appsession.Cache.Delete(RoomBookingKey(bookingID)); err != nil {
logrus.Error("failed to delete booking from cache", err)
return
}
}

func GetImage(appsession *models.AppSession, id string) (models.Image, error) {
if appsession.Cache == nil {
return models.Image{}, errors.New("cache not found")
}

// unmarshal the image from the cache
var image models.Image
imageData, err := appsession.Cache.Get(ImageKey(id))

if err != nil {
logrus.Error("key does not exist: ", err)
return models.Image{}, err
}

if err := bson.Unmarshal(imageData, &image); err != nil {
logrus.Error("Failed to unmarshall", err)
return models.Image{}, err

Check warning on line 186 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}

return image, nil
}

func SetImage(appsession *models.AppSession, id string, image models.Image) {
if appsession.Cache == nil {
return
}

// marshal the image
imageData, err := bson.Marshal(image)
if err != nil {
logrus.Error("failed to marshall", err)
return

Check warning on line 201 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L200-L201

Added lines #L200 - L201 were not covered by tests
}

// set the image in the cache
if err := appsession.Cache.Set(ImageKey(id), imageData); err != nil {
logrus.Error("failed to set user in cache", err)
return

Check warning on line 207 in occupi-backend/pkg/cache/cache.go

View check run for this annotation

Codecov / codecov/patch

occupi-backend/pkg/cache/cache.go#L206-L207

Added lines #L206 - L207 were not covered by tests
}
}

func DeleteImage(appsession *models.AppSession, id string) {
if appsession.Cache == nil {
return
}

// delete the image from the cache
if err := appsession.Cache.Delete(ImageKey(id)); err != nil {
logrus.Error("failed to delete image from cache", err)
return
}
}
17 changes: 17 additions & 0 deletions occupi-backend/pkg/cache/cache_keys_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cache

func UserKey(email string) string {
return "Users:" + email
}

func OTPKey(email, otp string) string {
return "OTPs:" + email + ":" + otp
}

func RoomBookingKey(roomID string) string {
return "RoomBookings:" + roomID
}

func ImageKey(imageID string) string {
return "Images:" + imageID
}
Loading
Loading