Repository navigation
perf:优化缓存相关热点 #8120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
perf:优化缓存相关热点 #8120
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRedisHashToObjDestination(t *testing.T) { | ||
| type entry struct { | ||
| ID int | ||
| } | ||
| var typedNil *entry | ||
| for _, test := range []struct { | ||
| name string | ||
| obj any | ||
| }{ | ||
| {name: "nil interface", obj: nil}, | ||
| {name: "typed nil pointer", obj: typedNil}, | ||
| {name: "non-pointer struct", obj: entry{}}, | ||
| {name: "pointer to non-struct", obj: new(int)}, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| var err error | ||
| require.NotPanics(t, func() { | ||
| err = RedisHashToObj(map[string]string{"ID": "7"}, test.obj) | ||
| }) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| t.Run("valid destination", func(t *testing.T) { | ||
| var obj entry | ||
| require.NoError(t, RedisHashToObj(map[string]string{"ID": "7"}, &obj)) | ||
| assert.Equal(t, 7, obj.ID) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -648,6 +648,7 @@ func CompleteSubscriptionOrder(tradeNo string, providerPayload string, expectedP | |
| if err != nil { | ||
| return err | ||
| } | ||
| invalidateSubscriptionDecisionCache(logUserId) | ||
| if upgradeGroup != "" && logUserId > 0 { | ||
| refreshSubscriptionUserGroupCache(logUserId, common.LogText("subscription payment completion")) | ||
| } | ||
|
|
@@ -747,6 +748,7 @@ func AdminBindSubscription(userId int, planId int, sourceNote string) (*common.M | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| invalidateSubscriptionDecisionCache(userId) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent in-flight loads from restoring invalidated decisions.
The same ordering affects the other invalidation calls. A cancellation or expiry can leave a stale active decision in place. Coordinate cache publication with invalidation so that a decision read before a mutation cannot be published after that mutation. Also prevent a read that starts after a mutation from joining a
|
||
| if groupChanged { | ||
| refreshSubscriptionUserGroupCache(userId, common.LogText("admin subscription creation")) | ||
| return common.NewMessage("User group will be upgraded to {{group}}", map[string]any{"group": plan.UpgradeGroup}), nil | ||
|
|
@@ -845,6 +847,7 @@ func PurchaseSubscriptionWithBalance(userId int, planId int) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| invalidateSubscriptionDecisionCache(userId) | ||
|
|
||
| if chargedQuota > 0 { | ||
| if err := cacheDecrUserQuota(userId, int64(chargedQuota)); err != nil { | ||
|
|
@@ -879,37 +882,24 @@ func GetAllActiveUserSubscriptions(userId int) ([]SubscriptionSummary, error) { | |
| } | ||
|
|
||
| // HasActiveUserSubscription returns whether the user has any active subscription. | ||
| // This is a lightweight existence check to avoid heavy pre-consume transactions. | ||
| // The cached decision avoids heavy pre-consume transactions and repeated SQL reads. | ||
| func HasActiveUserSubscription(userId int) (bool, error) { | ||
| if userId <= 0 { | ||
| return false, errors.New("invalid userId") | ||
| } | ||
| now := common.GetTimestamp() | ||
| var count int64 | ||
| if err := DB.Model(&UserSubscription{}). | ||
| Where("user_id = ? AND status = ? AND end_time > ?", userId, "active", now). | ||
| Count(&count).Error; err != nil { | ||
| state, err := loadSubscriptionDecisionState(userId) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return count > 0, nil | ||
| return *state.HasActive, nil | ||
| } | ||
|
|
||
| // UserActiveSubscriptionsAllowWalletOverflow returns whether wallet balance may be used | ||
| // after the user's subscription quota is exhausted. A single active subscription that | ||
| // disallows wallet overflow (allow_wallet_overflow = false) blocks the fallback. | ||
| func UserActiveSubscriptionsAllowWalletOverflow(userId int) (bool, error) { | ||
| if userId <= 0 { | ||
| return false, errors.New("invalid userId") | ||
| } | ||
| now := common.GetTimestamp() | ||
| var strictCount int64 | ||
| if err := DB.Model(&UserSubscription{}). | ||
| Where("user_id = ? AND status = ? AND end_time > ? AND allow_wallet_overflow = ?", | ||
| userId, "active", now, false). | ||
| Count(&strictCount).Error; err != nil { | ||
| state, err := loadSubscriptionDecisionState(userId) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return strictCount == 0, nil | ||
| return *state.AllowWalletOverflow, nil | ||
| } | ||
|
|
||
| // GetAllUserSubscriptions returns all subscriptions (active and expired) for a user. | ||
|
|
@@ -977,6 +967,7 @@ func AdminInvalidateUserSubscription(userSubscriptionId int) (*common.Message, e | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| invalidateSubscriptionDecisionCache(userId) | ||
| if cacheGroup != "" && userId > 0 { | ||
| refreshSubscriptionUserGroupCache(userId, common.LogText("admin subscription update")) | ||
| } | ||
|
|
@@ -1018,6 +1009,7 @@ func AdminDeleteUserSubscription(userSubscriptionId int) (*common.Message, error | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| invalidateSubscriptionDecisionCache(userId) | ||
| if cacheGroup != "" && userId > 0 { | ||
| refreshSubscriptionUserGroupCache(userId, common.LogText("admin subscription deletion")) | ||
| } | ||
|
|
@@ -1245,6 +1237,7 @@ func ExpireDueSubscriptions(limit int) (int, error) { | |
| if err != nil { | ||
| return expiredCount, err | ||
| } | ||
| invalidateSubscriptionDecisionCache(userId) | ||
| if cacheGroup != "" { | ||
| refreshSubscriptionUserGroupCache(userId, common.LogText("subscription expiration")) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.