fix(home): 实时刷新常用订阅账号 (#505)
This commit is contained in:
1 parent
2ea86cd8dc
commit
bba3a4a493
5 files changed
+10
-46
No files matched your search
@@ -17,9 +17,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
homeSubscriptionAccountLimit = 4
|
||||
homeSubscriptionAccountWindow = 24 * time.Hour
|
||||
homeSubscriptionAccountCacheTTL = time.Minute
|
||||
homeSubscriptionAccountLimit = 4
|
||||
homeSubscriptionAccountWindow = 24 * time.Hour
|
||||
)
|
||||
|
||||
type HomeSubscriptionAccountsResponse struct {
|
||||
@@ -37,11 +36,6 @@ type HomeSubscriptionAccountResponse struct {
|
||||
Credential CredentialItemResponse `json:"credential"`
|
||||
}
|
||||
|
||||
type homeSubscriptionAccountsCache struct {
|
||||
expiresAt time.Time
|
||||
response HomeSubscriptionAccountsResponse
|
||||
}
|
||||
|
||||
type homeSubscriptionActivityRow struct {
|
||||
IdentityFingerprint string `gorm:"column:identity_fingerprint"`
|
||||
ChannelID string `gorm:"column:channel_id"`
|
||||
@@ -75,25 +69,7 @@ func (s *Service) ReadHomeSubscriptionAccounts(
|
||||
if err := ctx.Err(); err != nil {
|
||||
return HomeSubscriptionAccountsResponse{}, err
|
||||
}
|
||||
s.homeSubscriptionMu.Lock()
|
||||
defer s.homeSubscriptionMu.Unlock()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return HomeSubscriptionAccountsResponse{}, err
|
||||
}
|
||||
now := s.now().UTC()
|
||||
if cached := s.homeSubscriptionCache; cached != nil && now.Before(cached.expiresAt) {
|
||||
return cloneHomeSubscriptionAccountsResponse(cached.response), nil
|
||||
}
|
||||
|
||||
response, err := s.readHomeSubscriptionAccounts(ctx, now)
|
||||
if err != nil {
|
||||
return HomeSubscriptionAccountsResponse{}, err
|
||||
}
|
||||
s.homeSubscriptionCache = &homeSubscriptionAccountsCache{
|
||||
expiresAt: now.Add(homeSubscriptionAccountCacheTTL),
|
||||
response: cloneHomeSubscriptionAccountsResponse(response),
|
||||
}
|
||||
return response, nil
|
||||
return s.readHomeSubscriptionAccounts(ctx, s.now().UTC())
|
||||
}
|
||||
|
||||
func (s *Server) handleHomeSubscriptionAccounts(c *gin.Context) {
|
||||
@@ -367,10 +343,3 @@ func homeSubscriptionRepresentativeLess(
|
||||
func homeSubscriptionIdentityKey(channelID, identityFingerprint string) string {
|
||||
return channelID + "\x00" + identityFingerprint
|
||||
}
|
||||
|
||||
func cloneHomeSubscriptionAccountsResponse(
|
||||
value HomeSubscriptionAccountsResponse,
|
||||
) HomeSubscriptionAccountsResponse {
|
||||
value.Items = append([]HomeSubscriptionAccountResponse(nil), value.Items...)
|
||||
return value
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func TestReadHomeSubscriptionAccountsUsesBoundedHourlyActivityAndDeduplicates(t
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadHomeSubscriptionAccountsCachesRankingForOneMinute(t *testing.T) {
|
||||
func TestReadHomeSubscriptionAccountsReflectsLatestRankingOnEveryRead(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newServiceFixture(t)
|
||||
now := time.Date(2026, time.August, 31, 12, 30, 0, 0, time.UTC)
|
||||
@@ -124,14 +124,9 @@ func TestReadHomeSubscriptionAccountsCachesRankingForOneMinute(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cached, err := fixture.service.ReadHomeSubscriptionAccounts(t.Context())
|
||||
if err != nil || cached.Items[0].Credential.Account.Email != "first@example.com" {
|
||||
t.Fatalf("cached read = %#v, %v", cached, err)
|
||||
}
|
||||
now = now.Add(time.Minute)
|
||||
refreshed, err := fixture.service.ReadHomeSubscriptionAccounts(t.Context())
|
||||
if err != nil || refreshed.Items[0].Credential.Account.Email != "second@example.com" {
|
||||
t.Fatalf("refreshed read = %#v, %v", refreshed, err)
|
||||
latest, err := fixture.service.ReadHomeSubscriptionAccounts(t.Context())
|
||||
if err != nil || latest.Items[0].Credential.Account.Email != "second@example.com" {
|
||||
t.Fatalf("latest read = %#v, %v", latest, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,8 +88,6 @@ type Service struct {
|
||||
observationMu sync.Mutex
|
||||
observationFlights map[observationFlightKey]*observationFlight
|
||||
observationSemaphore chan struct{}
|
||||
homeSubscriptionMu sync.Mutex
|
||||
homeSubscriptionCache *homeSubscriptionAccountsCache
|
||||
}
|
||||
|
||||
type credentialRuntimeRetirer interface {
|
||||
|
||||
@@ -663,7 +663,6 @@ export function homeSubscriptionAccountsQueryOptions(
|
||||
queryKey: controlQueryKeys.home.subscriptionAccounts(),
|
||||
queryFn: ({ signal }) => getHomeSubscriptionAccounts(client, signal),
|
||||
enabled: computed(() => toValue(enabled)),
|
||||
staleTime: 60_000,
|
||||
refetchOnMount: 'always',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ function ignoreReadonlyProxyMutation(): Promise<void> {
|
||||
.home-subscription-accounts {
|
||||
display: grid;
|
||||
gap: var(--space-3);
|
||||
margin-top: 36px;
|
||||
border-top: 1px solid var(--color-border-subtle);
|
||||
padding-top: 20px;
|
||||
}
|
||||
|
||||
.home-subscription-accounts__grid {
|
||||
|
||||
Reference in new issue
Block a user