From 6d52d8a712922011b2d3385af2a6a3a2207b85e5 Mon Sep 17 00:00:00 2001 From: Joscha Henningsen Date: Wed, 30 Oct 2024 19:28:12 +0100 Subject: [PATCH 1/5] implement simple cache for news --- server/backend/news.go | 46 +++++++++++++-------- server/backend/rpcserver.go | 3 ++ server/utils/cache.go | 80 +++++++++++++++++++++++++++++++++++++ server/utils/cache_test.go | 34 ++++++++++++++++ 4 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 server/utils/cache.go create mode 100644 server/utils/cache_test.go diff --git a/server/backend/news.go b/server/backend/news.go index 158a14a3..b27b295b 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -4,6 +4,8 @@ import ( "context" "errors" "fmt" + "github.com/TUM-Dev/Campus-Backend/server/utils" + "time" "gorm.io/gorm" @@ -22,11 +24,15 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource } var sources []model.NewsSource - if err := s.db.WithContext(ctx). + if s.cache.Exists(utils.CacheKeyAllNewsSources, "") { + sources = s.cache.Get(utils.CacheKeyAllNewsSources, "").([]model.NewsSource) + } else if err := s.db.WithContext(ctx). Joins("File"). Find(&sources).Error; err != nil { log.WithError(err).Error("could not find news_sources") return nil, status.Error(codes.Internal, "could not ListNewsSources") + } else { + s.cache.Set(utils.CacheKeyAllNewsSources, "", sources, 30*time.Minute) } var resp []*pb.NewsSource @@ -46,22 +52,28 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (* } var newsEntries []model.News - tx := s.db.WithContext(ctx). - Joins("File"). - Joins("NewsSource"). - Joins("NewsSource.File") - if req.NewsSource != 0 { - tx = tx.Where("src = ?", req.NewsSource) - } - if req.OldestDateAt.GetSeconds() != 0 || req.OldestDateAt.GetNanos() != 0 { - tx = tx.Where("date > ?", req.OldestDateAt.AsTime()) - } - if req.LastNewsId != 0 { - tx = tx.Where("news > ?", req.LastNewsId) - } - if err := tx.Find(&newsEntries).Error; err != nil { - log.WithError(err).Error("could not find news item") - return nil, status.Error(codes.Internal, "could not ListNews") + paramsForCache := fmt.Sprintf("%d_%d_%d", req.NewsSource, req.OldestDateAt.GetSeconds(), req.LastNewsId) + if s.cache.Exists(utils.CacheKeyNews, paramsForCache) { + newsEntries = s.cache.Get(utils.CacheKeyNews, paramsForCache).([]model.News) + } else { + tx := s.db.WithContext(ctx). + Joins("File"). + Joins("NewsSource"). + Joins("NewsSource.File") + if req.NewsSource != 0 { + tx = tx.Where("src = ?", req.NewsSource) + } + if req.OldestDateAt.GetSeconds() != 0 || req.OldestDateAt.GetNanos() != 0 { + tx = tx.Where("date > ?", req.OldestDateAt.AsTime()) + } + if req.LastNewsId != 0 { + tx = tx.Where("news > ?", req.LastNewsId) + } + if err := tx.Find(&newsEntries).Error; err != nil { + log.WithError(err).Error("could not find news item") + return nil, status.Error(codes.Internal, "could not ListNews") + } + s.cache.Set(utils.CacheKeyNews, paramsForCache, newsEntries, 10*time.Minute) } var resp []*pb.News diff --git a/server/backend/rpcserver.go b/server/backend/rpcserver.go index e1e0b609..41c6b380 100644 --- a/server/backend/rpcserver.go +++ b/server/backend/rpcserver.go @@ -1,6 +1,7 @@ package backend import ( + "github.com/TUM-Dev/Campus-Backend/server/utils" "sync" pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" @@ -13,6 +14,7 @@ type CampusServer struct { feedbackEmailLastReuestAt *sync.Map db *gorm.DB deviceBuf *deviceBuffer // deviceBuf stores all devices from recent request and flushes them to db + cache *utils.Cache } // Verify that CampusServer implements the pb.CampusServer interface @@ -24,5 +26,6 @@ func New(db *gorm.DB) *CampusServer { db: db, deviceBuf: newDeviceBuffer(), feedbackEmailLastReuestAt: &sync.Map{}, + cache: utils.NewCache(), } } diff --git a/server/utils/cache.go b/server/utils/cache.go new file mode 100644 index 00000000..b97bc017 --- /dev/null +++ b/server/utils/cache.go @@ -0,0 +1,80 @@ +package utils + +import ( + "sync" + "time" +) + +type CacheKey string + +const ( + // CacheKeyAllNewsSources is the key for all news sources from the database + CacheKeyAllNewsSources CacheKey = "all_news_sources" + // CacheKeyNews is the key for news + CacheKeyNews CacheKey = "news" +) + +// Cache is a thread-safe cache +type Cache struct { + cache map[string]any + deleteJobs map[string]time.Time + lock sync.RWMutex +} + +// NewCache creates a new cache +func NewCache() *Cache { + cache := &Cache{ + cache: make(map[string]any), + deleteJobs: make(map[string]time.Time), + lock: sync.RWMutex{}, + } + go cache.deleteLoop() + return cache +} + +// Set adds an entry to the cache +func (c *Cache) Set(key CacheKey, params string, value any, expire time.Duration) { + c.lock.Lock() + defer c.lock.Unlock() + c.cache[c.combine(key, params)] = value + c.deleteJobs[c.combine(key, params)] = time.Now().Add(expire) +} + +// Get returns an entry from the cache +func (c *Cache) Get(key CacheKey, params string) any { + c.lock.RLock() + defer c.lock.RUnlock() + return c.cache[c.combine(key, params)] +} + +// Exists checks if an entry exists in the cache +func (c *Cache) Exists(key CacheKey, params string) bool { + c.lock.RLock() + defer c.lock.RUnlock() + _, ok := c.cache[c.combine(key, params)] + return ok +} + +// Delete removes an entry from the cache +func (c *Cache) delete(key string) { + c.lock.Lock() + defer c.lock.Unlock() + delete(c.cache, key) + delete(c.deleteJobs, key) +} + +// deleteLoop deletes all entries that have expired from the cache +func (c *Cache) deleteLoop() { + for { + for s, t := range c.deleteJobs { + if time.Now().After(t) { + c.delete(s) + } + } + time.Sleep(10 * time.Second) + } +} + +func (c *Cache) combine(key CacheKey, params string) string { + return string(key) + params +} diff --git a/server/utils/cache_test.go b/server/utils/cache_test.go new file mode 100644 index 00000000..1b7533b0 --- /dev/null +++ b/server/utils/cache_test.go @@ -0,0 +1,34 @@ +package utils + +import ( + "github.com/TUM-Dev/Campus-Backend/server/model" + "testing" + "time" +) + +// test Cache +func TestCache(t *testing.T) { + cache := NewCache() + cache.Set(CacheKeyAllNewsSources, "", []model.NewsSource{}, 1*time.Hour) + cache.Set(CacheKeyNews, "1", []model.News{}, 1*time.Hour) + + if !cache.Exists(CacheKeyAllNewsSources, "") { + t.Errorf("CacheKeyAllNewsSources should exist") + } + if !cache.Exists(CacheKeyNews, "1") { + t.Errorf("CacheKeyNews should exist") + } + + if cache.Get(CacheKeyAllNewsSources, "").([]model.NewsSource) == nil { + t.Errorf("CacheKeyAllNewsSources should not be nil") + } + if cache.Get(CacheKeyNews, "1").([]model.News) == nil { + t.Errorf("CacheKeyNews should not be nil") + } + + cache.delete(string(CacheKeyAllNewsSources)) + + if cache.Exists(CacheKeyAllNewsSources, "") { + t.Errorf("CacheKeyAllNewsSources should not exist") + } +} From 079154fb4f7e95a2202f9492c945378128d0ee4c Mon Sep 17 00:00:00 2001 From: Joscha Henningsen Date: Wed, 30 Oct 2024 19:29:59 +0100 Subject: [PATCH 2/5] improve cache key method name --- server/utils/cache.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/utils/cache.go b/server/utils/cache.go index b97bc017..9895a44f 100644 --- a/server/utils/cache.go +++ b/server/utils/cache.go @@ -36,22 +36,22 @@ func NewCache() *Cache { func (c *Cache) Set(key CacheKey, params string, value any, expire time.Duration) { c.lock.Lock() defer c.lock.Unlock() - c.cache[c.combine(key, params)] = value - c.deleteJobs[c.combine(key, params)] = time.Now().Add(expire) + c.cache[c.makeFullCacheKey(key, params)] = value + c.deleteJobs[c.makeFullCacheKey(key, params)] = time.Now().Add(expire) } // Get returns an entry from the cache func (c *Cache) Get(key CacheKey, params string) any { c.lock.RLock() defer c.lock.RUnlock() - return c.cache[c.combine(key, params)] + return c.cache[c.makeFullCacheKey(key, params)] } // Exists checks if an entry exists in the cache func (c *Cache) Exists(key CacheKey, params string) bool { c.lock.RLock() defer c.lock.RUnlock() - _, ok := c.cache[c.combine(key, params)] + _, ok := c.cache[c.makeFullCacheKey(key, params)] return ok } @@ -75,6 +75,6 @@ func (c *Cache) deleteLoop() { } } -func (c *Cache) combine(key CacheKey, params string) string { +func (c *Cache) makeFullCacheKey(key CacheKey, params string) string { return string(key) + params } From 6055e34760187c9049a954f0ed4448122e7093e5 Mon Sep 17 00:00:00 2001 From: Joscha Henningsen Date: Wed, 30 Oct 2024 22:37:41 +0100 Subject: [PATCH 3/5] use lru for cache --- server/backend/news.go | 92 +++++++++++++++++++++++--------------- server/go.mod | 1 + server/go.sum | 2 + server/utils/cache.go | 80 --------------------------------- server/utils/cache_test.go | 34 -------------- 5 files changed, 60 insertions(+), 149 deletions(-) delete mode 100644 server/utils/cache.go delete mode 100644 server/utils/cache_test.go diff --git a/server/backend/news.go b/server/backend/news.go index b27b295b..9f07cc59 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -4,35 +4,37 @@ import ( "context" "errors" "fmt" - "github.com/TUM-Dev/Campus-Backend/server/utils" "time" "gorm.io/gorm" - "google.golang.org/protobuf/types/known/timestamppb" - pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" "github.com/TUM-Dev/Campus-Backend/server/model" + "github.com/hashicorp/golang-lru/v2/expirable" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/timestamppb" +) + +const ( + NewsSourceCacheDuration = time.Hour * 6 + CacheKeyAllNewsSources = "all_news_sources" + NewsCacheDuration = time.Minute * 30 + CacheKeyNews = "news" ) +var newsSourceCache = expirable.NewLRU[string, []model.NewsSource](512, nil, NewsSourceCacheDuration) +var newsCache = expirable.NewLRU[string, []model.News](512, nil, NewsCacheDuration) + func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSourcesRequest) (*pb.ListNewsSourcesReply, error) { if err := s.checkDevice(ctx); err != nil { return nil, err } - var sources []model.NewsSource - if s.cache.Exists(utils.CacheKeyAllNewsSources, "") { - sources = s.cache.Get(utils.CacheKeyAllNewsSources, "").([]model.NewsSource) - } else if err := s.db.WithContext(ctx). - Joins("File"). - Find(&sources).Error; err != nil { - log.WithError(err).Error("could not find news_sources") + sources, err := s.getNewsSources(ctx) + if err != nil { return nil, status.Error(codes.Internal, "could not ListNewsSources") - } else { - s.cache.Set(utils.CacheKeyAllNewsSources, "", sources, 30*time.Minute) } var resp []*pb.NewsSource @@ -46,34 +48,26 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource return &pb.ListNewsSourcesReply{Sources: resp}, nil } +func (s *CampusServer) getNewsSources(ctx context.Context) ([]model.NewsSource, error) { + if newsSources, ok := newsSourceCache.Get(CacheKeyAllNewsSources); ok { + return newsSources, nil + } + var sources []model.NewsSource + if err := s.db.WithContext(ctx).Joins("File").Find(&sources).Error; err != nil { + return nil, err + } + newsSourceCache.Add(CacheKeyAllNewsSources, sources) + return sources, nil +} + func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*pb.ListNewsReply, error) { if err := s.checkDevice(ctx); err != nil { return nil, err } - var newsEntries []model.News - paramsForCache := fmt.Sprintf("%d_%d_%d", req.NewsSource, req.OldestDateAt.GetSeconds(), req.LastNewsId) - if s.cache.Exists(utils.CacheKeyNews, paramsForCache) { - newsEntries = s.cache.Get(utils.CacheKeyNews, paramsForCache).([]model.News) - } else { - tx := s.db.WithContext(ctx). - Joins("File"). - Joins("NewsSource"). - Joins("NewsSource.File") - if req.NewsSource != 0 { - tx = tx.Where("src = ?", req.NewsSource) - } - if req.OldestDateAt.GetSeconds() != 0 || req.OldestDateAt.GetNanos() != 0 { - tx = tx.Where("date > ?", req.OldestDateAt.AsTime()) - } - if req.LastNewsId != 0 { - tx = tx.Where("news > ?", req.LastNewsId) - } - if err := tx.Find(&newsEntries).Error; err != nil { - log.WithError(err).Error("could not find news item") - return nil, status.Error(codes.Internal, "could not ListNews") - } - s.cache.Set(utils.CacheKeyNews, paramsForCache, newsEntries, 10*time.Minute) + var newsEntries, err = s.getNews(ctx, req.NewsSource, req.LastNewsId, req.OldestDateAt.AsTime()) + if err != nil { + return nil, status.Error(codes.Internal, "could not ListNews") } var resp []*pb.News @@ -98,6 +92,34 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (* return &pb.ListNewsReply{News: resp}, nil } +func (s *CampusServer) getNews(ctx context.Context, sourceID int32, lastNewsID int32, oldestDateAt time.Time) ([]model.News, error) { + cacheKey := fmt.Sprintf("%s_%d_%d_%d", CacheKeyNews, sourceID, oldestDateAt.Second(), lastNewsID) + + if news, ok := newsCache.Get(cacheKey); ok { + return news, nil + } + + var news []model.News + tx := s.db.WithContext(ctx). + Joins("File"). + Joins("NewsSource"). + Joins("NewsSource.File") + if sourceID != 0 { + tx = tx.Where("src = ?", sourceID) + } + if oldestDateAt.Second() != 0 || oldestDateAt.Nanosecond() != 0 { + tx = tx.Where("date > ?", oldestDateAt) + } + if lastNewsID != 0 { + tx = tx.Where("news > ?", lastNewsID) + } + if err := tx.Find(&news).Error; err != nil { + return nil, err + } + newsCache.Add(CacheKeyNews, news) + return news, nil +} + func (s *CampusServer) ListNewsAlerts(ctx context.Context, req *pb.ListNewsAlertsRequest) (*pb.ListNewsAlertsReply, error) { if err := s.checkDevice(ctx); err != nil { return nil, err diff --git a/server/go.mod b/server/go.mod index ed963c3f..820152a1 100644 --- a/server/go.mod +++ b/server/go.mod @@ -58,6 +58,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/css v1.0.1 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/server/go.sum b/server/go.sum index 7b2b5859..9266c693 100644 --- a/server/go.sum +++ b/server/go.sum @@ -157,6 +157,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjw github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= github.com/guregu/null v4.0.0+incompatible h1:4zw0ckM7ECd6FNNddc3Fu4aty9nTlpkkzH7dPn4/4Gw= github.com/guregu/null v4.0.0+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/server/utils/cache.go b/server/utils/cache.go deleted file mode 100644 index 9895a44f..00000000 --- a/server/utils/cache.go +++ /dev/null @@ -1,80 +0,0 @@ -package utils - -import ( - "sync" - "time" -) - -type CacheKey string - -const ( - // CacheKeyAllNewsSources is the key for all news sources from the database - CacheKeyAllNewsSources CacheKey = "all_news_sources" - // CacheKeyNews is the key for news - CacheKeyNews CacheKey = "news" -) - -// Cache is a thread-safe cache -type Cache struct { - cache map[string]any - deleteJobs map[string]time.Time - lock sync.RWMutex -} - -// NewCache creates a new cache -func NewCache() *Cache { - cache := &Cache{ - cache: make(map[string]any), - deleteJobs: make(map[string]time.Time), - lock: sync.RWMutex{}, - } - go cache.deleteLoop() - return cache -} - -// Set adds an entry to the cache -func (c *Cache) Set(key CacheKey, params string, value any, expire time.Duration) { - c.lock.Lock() - defer c.lock.Unlock() - c.cache[c.makeFullCacheKey(key, params)] = value - c.deleteJobs[c.makeFullCacheKey(key, params)] = time.Now().Add(expire) -} - -// Get returns an entry from the cache -func (c *Cache) Get(key CacheKey, params string) any { - c.lock.RLock() - defer c.lock.RUnlock() - return c.cache[c.makeFullCacheKey(key, params)] -} - -// Exists checks if an entry exists in the cache -func (c *Cache) Exists(key CacheKey, params string) bool { - c.lock.RLock() - defer c.lock.RUnlock() - _, ok := c.cache[c.makeFullCacheKey(key, params)] - return ok -} - -// Delete removes an entry from the cache -func (c *Cache) delete(key string) { - c.lock.Lock() - defer c.lock.Unlock() - delete(c.cache, key) - delete(c.deleteJobs, key) -} - -// deleteLoop deletes all entries that have expired from the cache -func (c *Cache) deleteLoop() { - for { - for s, t := range c.deleteJobs { - if time.Now().After(t) { - c.delete(s) - } - } - time.Sleep(10 * time.Second) - } -} - -func (c *Cache) makeFullCacheKey(key CacheKey, params string) string { - return string(key) + params -} diff --git a/server/utils/cache_test.go b/server/utils/cache_test.go deleted file mode 100644 index 1b7533b0..00000000 --- a/server/utils/cache_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package utils - -import ( - "github.com/TUM-Dev/Campus-Backend/server/model" - "testing" - "time" -) - -// test Cache -func TestCache(t *testing.T) { - cache := NewCache() - cache.Set(CacheKeyAllNewsSources, "", []model.NewsSource{}, 1*time.Hour) - cache.Set(CacheKeyNews, "1", []model.News{}, 1*time.Hour) - - if !cache.Exists(CacheKeyAllNewsSources, "") { - t.Errorf("CacheKeyAllNewsSources should exist") - } - if !cache.Exists(CacheKeyNews, "1") { - t.Errorf("CacheKeyNews should exist") - } - - if cache.Get(CacheKeyAllNewsSources, "").([]model.NewsSource) == nil { - t.Errorf("CacheKeyAllNewsSources should not be nil") - } - if cache.Get(CacheKeyNews, "1").([]model.News) == nil { - t.Errorf("CacheKeyNews should not be nil") - } - - cache.delete(string(CacheKeyAllNewsSources)) - - if cache.Exists(CacheKeyAllNewsSources, "") { - t.Errorf("CacheKeyAllNewsSources should not exist") - } -} From 3211efc32506994b7600efaf95c96b8647c2c26d Mon Sep 17 00:00:00 2001 From: Joscha Henningsen Date: Wed, 30 Oct 2024 22:43:23 +0100 Subject: [PATCH 4/5] cleanup --- server/backend/news.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/server/backend/news.go b/server/backend/news.go index 9f07cc59..c83e3434 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -6,8 +6,6 @@ import ( "fmt" "time" - "gorm.io/gorm" - pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" "github.com/TUM-Dev/Campus-Backend/server/model" "github.com/hashicorp/golang-lru/v2/expirable" @@ -15,17 +13,11 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" + "gorm.io/gorm" ) -const ( - NewsSourceCacheDuration = time.Hour * 6 - CacheKeyAllNewsSources = "all_news_sources" - NewsCacheDuration = time.Minute * 30 - CacheKeyNews = "news" -) - -var newsSourceCache = expirable.NewLRU[string, []model.NewsSource](512, nil, NewsSourceCacheDuration) -var newsCache = expirable.NewLRU[string, []model.News](512, nil, NewsCacheDuration) +var newsSourceCache = expirable.NewLRU[string, []model.NewsSource](1, nil, time.Hour*6) +var newsCache = expirable.NewLRU[string, []model.News](1024, nil, time.Minute*30) func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSourcesRequest) (*pb.ListNewsSourcesReply, error) { if err := s.checkDevice(ctx); err != nil { @@ -48,6 +40,8 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource return &pb.ListNewsSourcesReply{Sources: resp}, nil } +const CacheKeyAllNewsSources = "all_news_sources" + func (s *CampusServer) getNewsSources(ctx context.Context) ([]model.NewsSource, error) { if newsSources, ok := newsSourceCache.Get(CacheKeyAllNewsSources); ok { return newsSources, nil @@ -93,7 +87,7 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (* } func (s *CampusServer) getNews(ctx context.Context, sourceID int32, lastNewsID int32, oldestDateAt time.Time) ([]model.News, error) { - cacheKey := fmt.Sprintf("%s_%d_%d_%d", CacheKeyNews, sourceID, oldestDateAt.Second(), lastNewsID) + cacheKey := fmt.Sprintf("%d_%d_%d", sourceID, oldestDateAt.Second(), lastNewsID) if news, ok := newsCache.Get(cacheKey); ok { return news, nil @@ -116,7 +110,7 @@ func (s *CampusServer) getNews(ctx context.Context, sourceID int32, lastNewsID i if err := tx.Find(&news).Error; err != nil { return nil, err } - newsCache.Add(CacheKeyNews, news) + newsCache.Add(cacheKey, news) return news, nil } From 798e930b552f9726d0a5dcd81bfb314e5efbe6a0 Mon Sep 17 00:00:00 2001 From: Joscha Henningsen Date: Wed, 30 Oct 2024 22:43:53 +0100 Subject: [PATCH 5/5] remove previous cache --- server/backend/rpcserver.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/backend/rpcserver.go b/server/backend/rpcserver.go index 41c6b380..e1e0b609 100644 --- a/server/backend/rpcserver.go +++ b/server/backend/rpcserver.go @@ -1,7 +1,6 @@ package backend import ( - "github.com/TUM-Dev/Campus-Backend/server/utils" "sync" pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" @@ -14,7 +13,6 @@ type CampusServer struct { feedbackEmailLastReuestAt *sync.Map db *gorm.DB deviceBuf *deviceBuffer // deviceBuf stores all devices from recent request and flushes them to db - cache *utils.Cache } // Verify that CampusServer implements the pb.CampusServer interface @@ -26,6 +24,5 @@ func New(db *gorm.DB) *CampusServer { db: db, deviceBuf: newDeviceBuffer(), feedbackEmailLastReuestAt: &sync.Map{}, - cache: utils.NewCache(), } }