-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache SolvedStatus queries for pairs of users and problems.
- Loading branch information
Showing
10 changed files
with
106 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cached | ||
|
||
import ( | ||
"github.com/erni27/imcache" | ||
"github.com/mraron/njudge/internal/njudge" | ||
"golang.org/x/net/context" | ||
"time" | ||
) | ||
|
||
type problemAndUser struct { | ||
ProblemID, UserID int | ||
} | ||
|
||
type SolvedStatusQuery struct { | ||
solvedStatusQuery njudge.SolvedStatusQuery | ||
cache *imcache.Cache[problemAndUser, njudge.SolvedStatus] | ||
} | ||
|
||
func (ssq *SolvedStatusQuery) GetSolvedStatus(ctx context.Context, problemID, userID int) (njudge.SolvedStatus, error) { | ||
key := problemAndUser{problemID, userID} | ||
if val, ok := ssq.cache.Get(key); ok { | ||
return val, nil | ||
} | ||
|
||
val, err := ssq.solvedStatusQuery.GetSolvedStatus(ctx, problemID, userID) | ||
if err != nil { | ||
return 0, nil | ||
} | ||
|
||
ssq.cache.Set(key, val, imcache.WithDefaultExpiration()) | ||
return val, nil | ||
} | ||
|
||
func NewSolvedStatusQuery(solvedStatusQuery njudge.SolvedStatusQuery, ttl time.Duration) *SolvedStatusQuery { | ||
return &SolvedStatusQuery{ | ||
cache: imcache.New[problemAndUser, njudge.SolvedStatus]( | ||
imcache.WithDefaultExpirationOption[problemAndUser, njudge.SolvedStatus](ttl), | ||
), | ||
solvedStatusQuery: solvedStatusQuery, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters