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

fix: invalidate cache #21

Merged
merged 1 commit into from
Dec 30, 2023
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
2 changes: 1 addition & 1 deletion di/wire_gen.go

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

9 changes: 9 additions & 0 deletions internal/event/event.cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Cache interface {
Get(ctx context.Context, key string) (bool, string, *apperror.AppError)
Set(ctx context.Context, key string, value string, expiration time.Duration) *apperror.AppError
Del(ctx context.Context, keys ...string) *apperror.AppError
}

type cacheImpl struct {
Expand Down Expand Up @@ -48,3 +49,11 @@ func (s *cacheImpl) Set(ctx context.Context, key string, value string, expiratio
return nil
}
}

func (s *cacheImpl) Del(ctx context.Context, keys ...string) *apperror.AppError {
if err := s.redis.Del(ctx, keys...).Err(); err != nil {
s.logger.Error("could not delete keys", zap.Strings("keys", keys), zap.Error(err))
return apperror.InternalError
}
return nil
}
2 changes: 1 addition & 1 deletion internal/event/event.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ func (h *handlerImpl) GetEventById(c *gin.Context) {

func setHeader(c *gin.Context) {
c.Header("Content-Type", "application/json; charset=utf-8")
c.Header("Cache-Control", "public, max-age=3600")
c.Header("Cache-Control", "public, max-age=30")
}
3 changes: 2 additions & 1 deletion internal/evtreg/evtreg.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ func (h *handlerImpl) RegisterEvent(c *gin.Context) {
return
}

if apperr := h.svc.RegisterEvent(email, scheduleId); apperr != nil {
if apperr := h.svc.RegisterEvent(c, email, scheduleId); apperr != nil {
utils.ReturnError(c, apperr)
return
}

c.AbortWithStatus(http.StatusNoContent)
Expand Down
4 changes: 2 additions & 2 deletions internal/evtreg/evtreg.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type repositoryImpl struct {
}

func (r *repositoryImpl) GetUserWithEventRegistrationByEmail(user *model.User, email string) error {
return r.db.Preload("RegisteredEvents").Preload("RegisteredEvents.Schedule").Where("email = ?", email).First(&user).Error
return r.db.Model(user).Preload("RegisteredEvents").Preload("RegisteredEvents.Schedule").Where("email = ?", email).First(&user).Error
}

func (r *repositoryImpl) GetScheduleById(schedule *model.Schedule, scheduleId int) error {
return r.db.Where("id = ?", scheduleId).First(schedule).Error
return r.db.Model(schedule).Where("id = ?", scheduleId).First(schedule).Error
}

func (r *repositoryImpl) RegisterEvent(userId int, scheduleId int) error {
Expand Down
16 changes: 13 additions & 3 deletions internal/evtreg/evtreg.service.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package evtreg

import (
"context"
"errors"
"fmt"

"github.com/isd-sgcu/oph66-backend/apperror"
"github.com/isd-sgcu/oph66-backend/internal/event"
"github.com/isd-sgcu/oph66-backend/internal/model"
"go.uber.org/zap"
"gorm.io/gorm"
)

type Service interface {
RegisterEvent(userEmail string, scheduleId int) *apperror.AppError
RegisterEvent(ctx context.Context, userEmail string, scheduleId int) *apperror.AppError
}

func NewService(logger *zap.Logger, repo Repository) Service {
func NewService(logger *zap.Logger, repo Repository, cache event.Cache) Service {
return &serviceImpl{
logger,
repo,
cache,
}
}

Expand All @@ -25,9 +29,10 @@ var _ Service = &serviceImpl{}
type serviceImpl struct {
logger *zap.Logger
repo Repository
cache event.Cache
}

func (h *serviceImpl) RegisterEvent(userEmail string, scheduleId int) *apperror.AppError {
func (h *serviceImpl) RegisterEvent(ctx context.Context, userEmail string, scheduleId int) *apperror.AppError {
var user model.User
if err := h.repo.GetUserWithEventRegistrationByEmail(&user, userEmail); errors.Is(err, gorm.ErrRecordNotFound) {
return apperror.UserNotFound
Expand Down Expand Up @@ -57,5 +62,10 @@ func (h *serviceImpl) RegisterEvent(userEmail string, scheduleId int) *apperror.
return apperror.InternalError
}

keys := []string{fmt.Sprintf("get_event_by_id-%v", schedule.EventId), "get_all_events"}
if apperr := h.cache.Del(ctx, keys...); apperr != nil {
return apperr
}

return nil
}
14 changes: 14 additions & 0 deletions migrations/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ CREATE TABLE event_registrations (
);

INSERT INTO feature_flags(key, enabled, cache_duration, extra_info) VALUES ('livestream', FALSE, 10, '{"url": "https://www.youtube.com/watch?v=0tOXxuLcaog"}');

CREATE OR REPLACE FUNCTION update_attandee_count()
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
sid INTEGER;
cnt INTEGER;
BEGIN
for sid, cnt in select id, count(user_id) from schedules s left join event_registrations er on s.id = er.schedule_id group by s.id loop
update schedules set current_attendee = cnt where id = sid;
end loop;
end;
$function$;