Add cache invalidation for feed, article, and peopleUnverified
116dd3b parent: c1916a6 modified
internal/cluster/scoring.go +12 -0 | @@ -47,6 +47,18 @@ type ArticleRecommendation struct { | ||
| 47 | 47 | Score float64 |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | +func (e *Engine) InvalidateFeedCache(userDID string) { | |
| 51 | + e.feedCache.Remove(userDID) | |
| 52 | +} | |
| 53 | + | |
| 54 | +func (e *Engine) InvalidateArticleCache(userDID string) { | |
| 55 | + e.articleCache.Remove(userDID) | |
| 56 | +} | |
| 57 | + | |
| 58 | +func (e *Engine) InvalidatePeopleCache(userDID string) { | |
| 59 | + e.peopleCache.Remove(userDID) | |
| 60 | +} | |
| 61 | + | |
| 50 | 62 | // GetFeedRecommendations returns feed recommendations for a user. Users with |
| 51 | 63 | // fewer than 5 subscriptions get cold-start recommendations (embedding-based |
| 52 | 64 | // KNN or graph+popular fallback). Results are min-max normalized and |
| @@ -47,6 +47,18 @@ type ArticleRecommendation struct { | |||
| 47 | Score float64 | 47 | Score float64 |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | +func (e *Engine) InvalidateFeedCache(userDID string) { | ||
| 51 | + e.feedCache.Remove(userDID) | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +func (e *Engine) InvalidateArticleCache(userDID string) { | ||
| 55 | + e.articleCache.Remove(userDID) | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +func (e *Engine) InvalidatePeopleCache(userDID string) { | ||
| 59 | + e.peopleCache.Remove(userDID) | ||
| 60 | +} | ||
| 61 | + | ||
| 50 | // GetFeedRecommendations returns feed recommendations for a user. Users with | 62 | // GetFeedRecommendations returns feed recommendations for a user. Users with |
| 51 | // fewer than 5 subscriptions get cold-start recommendations (embedding-based | 63 | // fewer than 5 subscriptions get cold-start recommendations (embedding-based |
| 52 | // KNN or graph+popular fallback). Results are min-max normalized and | 64 | // KNN or graph+popular fallback). Results are min-max normalized and |
modified
internal/server/recs_handler.go +6 -4 | @@ -5,18 +5,18 @@ import ( | ||
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | func (s *Server) handleDismissFeedRecommendation(w http.ResponseWriter, r *http.Request) { |
| 8 | - s.handleDismiss(w, r, "feed_url", "feed") | |
| 8 | + s.handleDismiss(w, r, "feed_url", "feed", func(did string) { s.engine.InvalidateFeedCache(did) }) | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | func (s *Server) handleDismissArticleRecommendation(w http.ResponseWriter, r *http.Request) { |
| 12 | - s.handleDismiss(w, r, "article_url", "article") | |
| 12 | + s.handleDismiss(w, r, "article_url", "article", func(did string) { s.engine.InvalidateArticleCache(did) }) | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | func (s *Server) handleDismissPersonRecommendation(w http.ResponseWriter, r *http.Request) { |
| 16 | - s.handleDismiss(w, r, "target_did", "person") | |
| 16 | + s.handleDismiss(w, r, "target_did", "person", func(did string) { s.engine.InvalidatePeopleCache(did) }) | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | -func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, targetType string) { | |
| 19 | +func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, targetType string, invalidateCache func(string)) { | |
| 20 | 20 | user := currentUser(r) |
| 21 | 21 | targetID := r.FormValue(field) |
| 22 | 22 | if targetID == "" { |
| @@ -35,5 +35,7 @@ func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, ta | ||
| 35 | 35 | return |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | + invalidateCache(user.DID) | |
| 39 | + | |
| 38 | 40 | w.WriteHeader(http.StatusOK) |
| 39 | 41 | } |
| @@ -5,18 +5,18 @@ import ( | |||
| 5 | ) | 5 | ) |
| 6 | 6 | ||
| 7 | func (s *Server) handleDismissFeedRecommendation(w http.ResponseWriter, r *http.Request) { | 7 | func (s *Server) handleDismissFeedRecommendation(w http.ResponseWriter, r *http.Request) { |
| 8 | - s.handleDismiss(w, r, "feed_url", "feed") | 8 | + s.handleDismiss(w, r, "feed_url", "feed", func(did string) { s.engine.InvalidateFeedCache(did) }) |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | func (s *Server) handleDismissArticleRecommendation(w http.ResponseWriter, r *http.Request) { | 11 | func (s *Server) handleDismissArticleRecommendation(w http.ResponseWriter, r *http.Request) { |
| 12 | - s.handleDismiss(w, r, "article_url", "article") | 12 | + s.handleDismiss(w, r, "article_url", "article", func(did string) { s.engine.InvalidateArticleCache(did) }) |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | func (s *Server) handleDismissPersonRecommendation(w http.ResponseWriter, r *http.Request) { | 15 | func (s *Server) handleDismissPersonRecommendation(w http.ResponseWriter, r *http.Request) { |
| 16 | - s.handleDismiss(w, r, "target_did", "person") | 16 | + s.handleDismiss(w, r, "target_did", "person", func(did string) { s.engine.InvalidatePeopleCache(did) }) |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | -func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, targetType string) { | 19 | +func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, targetType string, invalidateCache func(string)) { |
| 20 | user := currentUser(r) | 20 | user := currentUser(r) |
| 21 | targetID := r.FormValue(field) | 21 | targetID := r.FormValue(field) |
| 22 | if targetID == "" { | 22 | if targetID == "" { |
| @@ -35,5 +35,7 @@ func (s *Server) handleDismiss(w http.ResponseWriter, r *http.Request, field, ta | |||
| 35 | return | 35 | return |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | + invalidateCache(user.DID) | ||
| 39 | + | ||
| 38 | w.WriteHeader(http.StatusOK) | 40 | w.WriteHeader(http.StatusOK) |
| 39 | } | 41 | } |