Skip to content

fix: 修复memory cache并发问题 - #889

Merged
houseme merged 1 commit into
silenceper:v2from
muzimu:fix-memory-cache
Jun 15, 2026
Merged

fix: 修复memory cache并发问题#889
houseme merged 1 commit into
silenceper:v2from
muzimu:fix-memory-cache

Conversation

@muzimu

@muzimu muzimu commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

memory的cache实现中Get 和 IsExist 读操作没有加锁,且存在死锁风险(deleteKey 内部加锁,但调用方可能已持锁)。

可用 sync.RWMutex 替换 sync.Mutex 修复并发读写问题,已完成相关修复并通过单元测试。

Fixes #888

@muzimu muzimu changed the title 修复memory cache并发问题 fix: 修复memory cache并发问题 Jun 15, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 718d3e387e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread cache/memory.go
Comment on lines +30 to +32
mem.mu.RLock()
ret, ok := mem.data[key]
mem.mu.RUnlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep expiry decisions under the read lock

When Get reads a soon-to-expire entry, drops the lock here, and then another goroutine Sets a fresh value for the same key before the old expiry, this goroutine can resume after the old expiry and return nil even though the current entry is valid; IsExist has the same pattern. Keep the expiry check/return decision covered by the read lock, only rechecking under the write lock when deleting an expired entry.

Useful? React with 👍 / 👎.

@muzimu
muzimu force-pushed the fix-memory-cache branch from 718d3e3 to 62f7a8f Compare June 15, 2026 10:07
@muzimu
muzimu force-pushed the fix-memory-cache branch from 62f7a8f to 34b8ae0 Compare June 15, 2026 10:09
@houseme
houseme merged commit fb3009e into silenceper:v2 Jun 15, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] memory cache有并发问题

2 participants