-
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.
✨ (caching): pretty "stable" caching mechanism for users
- Loading branch information
1 parent
82f6521
commit 5a8d933
Showing
32 changed files
with
641 additions
and
486 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,31 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ArcticOJ/blizzard/v0/config" | ||
"github.com/ArcticOJ/blizzard/v0/logger" | ||
"github.com/redis/go-redis/v9" | ||
"net" | ||
"time" | ||
) | ||
|
||
type ( | ||
DB uint8 | ||
) | ||
|
||
const ( | ||
Result DB = iota + 1 | ||
User | ||
User DB = iota + 1 | ||
Bucket | ||
Submission | ||
Judge | ||
Problem | ||
) | ||
|
||
func CreateClient(db DB, name string) (c redis.UniversalClient) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
c = redis.NewClient(&redis.Options{ | ||
Addr: net.JoinHostPort(config.Config.Dragonfly.Host, fmt.Sprint(config.Config.Dragonfly.Port)), | ||
DB: int(db), | ||
}) | ||
if config.Config.Debug { | ||
c.AddHook(DebugHook{Name: name}) | ||
} | ||
logger.Panic(c.Ping(ctx).Err(), "failed to initialize redis client for %s cache", name) | ||
return | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,77 @@ | ||
package stores | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ArcticOJ/blizzard/v0/cache" | ||
"github.com/ArcticOJ/blizzard/v0/db/models/contest" | ||
"github.com/ArcticOJ/blizzard/v0/rejson" | ||
"github.com/ArcticOJ/blizzard/v0/utils" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var Submissions *submissionStore | ||
|
||
type submissionStore struct { | ||
j *rejson.ReJSON | ||
l sync.RWMutex | ||
} | ||
|
||
const ( | ||
defaultPendingSubmissionKey = "blizzard::pending_submission[%d]" | ||
// 30 minutes | ||
defaultExtraTtl = 30 * 60 | ||
) | ||
|
||
func init() { | ||
Submissions = &submissionStore{j: &rejson.ReJSON{Client: cache.CreateClient(cache.Submission, "submissions")}} | ||
} | ||
|
||
func (s *submissionStore) SetPending(ctx context.Context, id uint32, tag uint64, count, ttl uint16) error { | ||
s.l.Lock() | ||
defer s.l.Unlock() | ||
return s.j.JTxPipelined(ctx, func(r *rejson.ReJSON) error { | ||
k := fmt.Sprintf(defaultPendingSubmissionKey, id) | ||
if e := r.JSONSet(ctx, k, "$", map[string]interface{}{ | ||
"tag": tag, | ||
"cases": utils.ArrayFill[interface{}](nil, int(count)), | ||
}); e != nil { | ||
return e | ||
} | ||
return r.Expire(ctx, k, time.Duration(defaultExtraTtl+count*(ttl+2))*time.Second).Err() | ||
}) | ||
} | ||
|
||
func (s *submissionStore) UpdatePending(ctx context.Context, id uint32, result contest.CaseResult) error { | ||
return s.j.JSONSet(ctx, fmt.Sprintf(defaultPendingSubmissionKey, id), fmt.Sprintf("$..cases[%d]", result.ID-1), result) | ||
} | ||
|
||
func (s *submissionStore) IsPending(ctx context.Context, id uint32) bool { | ||
s.l.RLock() | ||
defer s.l.RUnlock() | ||
ok, e := s.j.Exists(ctx, fmt.Sprintf(defaultPendingSubmissionKey, id)).Result() | ||
return ok == 1 && e == nil | ||
} | ||
|
||
func (s *submissionStore) GetPendingResults(ctx context.Context, id uint32) []contest.CaseResult { | ||
r := s.j.JSONGet(ctx, fmt.Sprintf(defaultPendingSubmissionKey, id), "$.cases") | ||
if _r := rejson.Unmarshal[[][]contest.CaseResult](r); _r != nil && len(*_r) > 0 { | ||
return (*_r)[0] | ||
} | ||
return nil | ||
} | ||
|
||
func (s *submissionStore) GetPendingTag(ctx context.Context, id uint32) (uint64, bool) { | ||
r := s.j.JSONGet(ctx, fmt.Sprintf(defaultPendingSubmissionKey, id), "$") | ||
if t := rejson.Unmarshal[[]uint64](r); t != nil && len(*t) > 0 { | ||
return (*t)[0], true | ||
} | ||
return 0, false | ||
} | ||
|
||
func (s *submissionStore) DeletePending(ctx context.Context, id uint32) { | ||
s.l.Lock() | ||
defer s.l.Unlock() | ||
s.j.Del(ctx, fmt.Sprintf(defaultPendingSubmissionKey, id)) | ||
} |
Oops, something went wrong.