Skip to content

Commit

Permalink
Merge pull request #107 from evanofslack/cache
Browse files Browse the repository at this point in the history
Fix posts added to incorrect cache
  • Loading branch information
evanofslack authored Aug 3, 2023
2 parents f33664f + 22fb8d4 commit 8a54d31
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
13 changes: 7 additions & 6 deletions backend/postgres/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const (
random = "random"
)

var primes = []int{11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 107, 113, 131, 137, 149, 167, 173, 179, 191, 197, 227, 233, 239, 251, 257, 263}

func (s *PostService) CreatePost(ctx context.Context, post *analogdb.CreatePost) (*analogdb.Post, error) {
tx, err := s.db.db.BeginTx(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -689,7 +691,7 @@ func filterToOrder(filter *analogdb.PostFilter) string {
if seed := filter.Seed; seed != nil {
return fmt.Sprintf(" ORDER BY MOD(p.time, %d), p.time DESC", *seed)
} else {
newSeed := seedGenerator()
newSeed := newSeed()
filter.Seed = &newSeed
return fmt.Sprintf(" ORDER BY MOD(p.time, %d), p.time DESC", newSeed)
}
Expand All @@ -708,11 +710,10 @@ func formatLimit(filter *analogdb.PostFilter) string {
return ""
}

// seedGenerator generates a random prime number
func seedGenerator() int {
prime_seeds := []int{11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
randomIndex := rand.Intn(len(prime_seeds))
return prime_seeds[randomIndex]
// newSeed generates a random prime number
func newSeed() int {
randomIndex := rand.Intn(len(primes))
return primes[randomIndex]
}

// filterToWhere converts a PostFilter to an SQL WHERE statement
Expand Down
9 changes: 5 additions & 4 deletions backend/redis/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// name of posts cache
postsInstance = "posts"
// ttl for all other post service data
postsTTL = time.Hour * 4
postsTTL = time.Hour * 1
// in memory cache size all other post service data
postsLocalSize = 100
)
Expand Down Expand Up @@ -78,13 +78,13 @@ func (s *PostService) FindPosts(ctx context.Context, filter *analogdb.PostFilter
var count int

// try to get posts from cache
err = s.postCache.get(ctx, postsHash, &posts)
postsErr := s.postsCache.get(ctx, postsHash, &posts)

// try to get posts count from cache
err = s.postsCache.get(ctx, postsCountHash, &count)
countErr := s.postsCache.get(ctx, postsCountHash, &count)

// no error means we found in cache
if err == nil {
if postsErr == nil && countErr == nil {
return posts, count, nil
}

Expand All @@ -99,6 +99,7 @@ func (s *PostService) FindPosts(ctx context.Context, filter *analogdb.PostFilter
go func() {

s.rdb.logger.Debug().Ctx(ctx).Str("instance", s.postsCache.instance).Msg("Adding posts and posts counts to cache")

// create a new context; orignal one will be canceled when request is closed
ctx, cancel := context.WithTimeout(context.Background(), cacheOpTimeout)
defer cancel()
Expand Down
5 changes: 0 additions & 5 deletions backend/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ func (cache *Cache) get(ctx context.Context, key string, item interface{}) error
cache.logger.Debug().Ctx(ctx).Str("instance", cache.instance).Msg("Cache miss")
cache.stats.incMisses()

// or error decoding an empty array? this is fine and not an error
// } else if strings.Contains(err.Error(), decodeArrayErr) {
// cache.logger.Debug().Ctx(ctx).Str("instance", cache.instance).Msg("Error decoding array on cache get, proceeding")
// cache.stats.incMisses()

// or an actual error
} else {
cache.logger.Error().Err(err).Ctx(ctx).Str("instance", cache.instance).Msg("Error getting item from cache")
Expand Down
18 changes: 18 additions & 0 deletions backend/server/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -57,6 +58,9 @@ var maxSimilarityLimit = 50
// default to sorting by time descending (latest)
var defaultSort = "time"

// seeds for random post order
var primes = []int{11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 107, 113, 131, 137, 149, 167, 173, 179, 191, 197, 227, 233, 239, 251, 257, 263}

const (
postsPath = "/posts"
postPath = "/post"
Expand Down Expand Up @@ -265,13 +269,17 @@ func (s *Server) makePostResponse(r *http.Request, filter *analogdb.PostFilter)

// setMeta computes the metadata from a query
func setMeta(filter *analogdb.PostFilter, posts []*analogdb.Post, count int) (Meta, error) {

meta := Meta{}

// totalPosts
meta.TotalPosts = count

// seed
if seed := filter.Seed; seed != nil {
meta.Seed = *seed
}

// pageSize
if limit := filter.Limit; limit != nil {
meta.PageSize = *limit
Expand All @@ -280,6 +288,7 @@ func setMeta(filter *analogdb.PostFilter, posts []*analogdb.Post, count int) (Me
return meta, nil
}
}

//pageID
if sort := filter.Sort; sort != nil {
if *sort == "time" || *sort == "random" {
Expand All @@ -290,6 +299,7 @@ func setMeta(filter *analogdb.PostFilter, posts []*analogdb.Post, count int) (Me
return Meta{}, errors.New("invalid sort parameter: " + *sort)
}
}

//pageUrl
if sort := filter.Sort; sort != nil {
path := postsPath
Expand Down Expand Up @@ -323,6 +333,7 @@ func setMeta(filter *analogdb.PostFilter, posts []*analogdb.Post, count int) (Me
}
meta.PageURL = path
}

return meta, nil
}

Expand All @@ -336,6 +347,11 @@ func paramJoiner(numParams *int) string {
}
}

func newSeed() int {
randomIndex := rand.Intn(len(primes))
return primes[randomIndex]
}

// parse URL for query parameters and convert to PostFilter needed to query db
func parseToFilter(r *http.Request) (*analogdb.PostFilter, error) {

Expand Down Expand Up @@ -366,7 +382,9 @@ func parseToFilter(r *http.Request) (*analogdb.PostFilter, error) {
filter.Sort = &score
case "random":
random := "random"
seed := newSeed()
filter.Sort = &random
filter.Seed = &seed
}
} else {
return nil, errors.New("invalid sort parameter - valid options: latest, top, random")
Expand Down

0 comments on commit 8a54d31

Please sign in to comment.