feat(push): implement Redis caching for push events and custom channels

- Implement cached queries in GetActivePushEventByKey and GetActivePushChannelByName.
- Set TTL for cached items to 24 hours via activePushEventCacheTTL and activePushChannelCacheTTL constants.
- Implement GORM hooks (AfterSave and AfterDelete) on PushEvent and PushChannel to auto-evict Redis caches, guaranteeing cache consistency.
- Evict Redis caches manually inside API handlers for Create/Update/Delete/Toggle event/channel endpoints.
- Update events.go to query models through the new caching methods.
This commit is contained in:
ryan
2026-06-16 10:19:06 +08:00
parent 9515f0811b
commit 9fa38dcfdc
5 changed files with 108 additions and 8 deletions
+9
View File
@@ -107,6 +107,9 @@ func CreateChannel(c *gin.Context) {
return
}
// 缓存一致性:清除渠道缓存
model.DeleteActivePushChannelCache(ctx, channel.Name)
c.JSON(http.StatusOK, response.OK(channel))
}
@@ -174,6 +177,9 @@ func UpdateChannel(c *gin.Context) {
return
}
// 缓存一致性:清除渠道缓存
model.DeleteActivePushChannelCache(ctx, channel.Name)
c.JSON(http.StatusOK, response.OK(channel))
}
@@ -210,6 +216,9 @@ func DeleteChannel(c *gin.Context) {
return
}
// 缓存一致性:清除渠道缓存
model.DeleteActivePushChannelCache(ctx, channel.Name)
c.JSON(http.StatusOK, response.OKNil())
}
+6 -7
View File
@@ -109,9 +109,8 @@ func (t *EventTrigger) Trigger(ctx context.Context, meta EventMetadata, body map
body["user"] = getSystemUser(asyncCtx)
}
// 1. Check if the event is enabled in the database
var event model.PushEvent
err := db.DB(asyncCtx).Where("event_key = ? AND enabled = ?", meta.Key, true).First(&event).Error
// 1. Check if the event is enabled (try Redis cache first)
eventPtr, err := model.GetActivePushEventByKey(asyncCtx, meta.Key)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return
@@ -119,6 +118,7 @@ func (t *EventTrigger) Trigger(ctx context.Context, meta EventMetadata, body map
logger.ErrorF(asyncCtx, "push_event_trigger: failed to get active event %s: %v", meta.Key, err)
return
}
event := *eventPtr
if len(event.Channels) == 0 {
return
@@ -222,11 +222,10 @@ func (t *EventTrigger) parseDefaultTemplate(meta EventMetadata, flatBody map[str
func (t *EventTrigger) enqueuePushTasks(ctx context.Context, meta EventMetadata, event *model.PushEvent, msg NotificationMessage, flatBody map[string]any) {
for _, channelName := range event.Channels {
// 检查是不是自定义数据库渠道
var customChannel model.PushChannel
err := db.DB(ctx).Where("name = ? AND enabled = ?", channelName, true).First(&customChannel).Error
// 检查是不是自定义数据库渠道 (使用 Redis 缓存优先)
customChannel, err := model.GetActivePushChannelByName(ctx, channelName)
if err == nil {
t.enqueueCustomPushChannelTasks(ctx, meta, event, &customChannel, msg, flatBody)
t.enqueueCustomPushChannelTasks(ctx, meta, event, customChannel, msg, flatBody)
continue
}
+12
View File
@@ -231,6 +231,9 @@ func CreateEvent(c *gin.Context) {
return
}
// 缓存一致性:清除旧事件缓存
model.DeleteActivePushEventCache(ctx, event.EventKey)
c.JSON(http.StatusOK, response.OK(event))
}
@@ -267,6 +270,9 @@ func DeleteEvent(c *gin.Context) {
return
}
// 缓存一致性:清除事件缓存
model.DeleteActivePushEventCache(ctx, event.EventKey)
c.JSON(http.StatusOK, response.OKNil())
}
@@ -320,6 +326,9 @@ func UpdateEvent(c *gin.Context) {
return
}
// 缓存一致性:清除事件缓存
model.DeleteActivePushEventCache(c.Request.Context(), event.EventKey)
c.JSON(http.StatusOK, response.OKNil())
}
@@ -360,6 +369,9 @@ func ToggleEvent(c *gin.Context) {
return
}
// 缓存一致性:清除事件缓存
model.DeleteActivePushEventCache(c.Request.Context(), event.EventKey)
c.JSON(http.StatusOK, response.OK(event.Enabled))
}