Skip to content

feat: add rds subscriber#18

Draft
MistEO wants to merge 4 commits intodevfrom
bak/redis
Draft

feat: add rds subscriber#18
MistEO wants to merge 4 commits intodevfrom
bak/redis

Conversation

@MistEO
Copy link
Copy Markdown
Contributor

@MistEO MistEO commented Dec 11, 2025

Summary by Sourcery

将基于 Redis 的发布/订阅(pub/sub)订阅者集成到 FastAPI 应用中,用于驱动套餐(plan)和联系信息(contact)数据的缓存失效。

New Features:

  • 引入支持发布/订阅的异步 Redis 客户端封装,并通过应用生命周期管理,在启动时开启后台订阅任务。

Enhancements:

  • 添加用于套餐和 contact_us 缓存的失效辅助工具,并将其连接到 Redis 订阅通道,以实现集中化的缓存清理。
  • 通过环境变量驱动的应用配置来设置 Redis 连接参数,并新增 Redis 依赖。
  • 规范 ICP 路由器导入的模块命名。
Original summary in English

Summary by Sourcery

Integrate a Redis-based pub/sub subscriber into the FastAPI application to drive cache invalidation for plan and contact data.

New Features:

  • Introduce an asynchronous Redis client wrapper with publish/subscribe support and app lifespan management to start a background subscriber on startup.

Enhancements:

  • Add cache invalidation helpers for plan and contact_us caches and wire them to a Redis subscription channel for centralized cache clearing.
  • Configure Redis connection settings via environment-driven application settings and add the Redis dependency.
  • Normalize module naming for the ICP router import.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Dec 11, 2025

Reviewer's Guide

引入一个基于 Redis 异步 pub/sub 的订阅者,用于在 Redis 渠道收到消息时使内存缓存失效;将其接入 FastAPI 的 lifespan 生命周期,并为 plan 和 contact_us 端点添加显式的缓存清理辅助函数,同时更新相关配置和依赖。

基于 Redis pubsub 的缓存失效时序图

sequenceDiagram
    participant Publisher
    participant RedisServer
    participant RedisClient_rds
    participant cache_clear_subscriber
    participant plan_cache
    participant contact_us_cache

    Publisher->>RedisServer: PUBLISH misc "evict"

    RedisClient_rds->>RedisServer: SUBSCRIBE misc
    RedisServer-->>RedisClient_rds: message on misc

    RedisClient_rds->>cache_clear_subscriber: deliver message
    cache_clear_subscriber->>cache_clear_subscriber: check type == message

    cache_clear_subscriber->>plan_cache: clean_plan_cache()
    plan_cache->>plan_cache: set _plan_cache = None

    cache_clear_subscriber->>contact_us_cache: clean_contact_cache()
    contact_us_cache->>contact_us_cache: set cache = None
Loading

RedisClient 与缓存辅助函数的类图

classDiagram
    class RedisClient {
        +redis
        +pubsub
        +__init__()
        +connect() bool
        +close() None
        +publish(channel str, message str) bool
        +subscribe(channel str)
    }

    class Settings {
        +redis_host str
        +redis_port int
        +redis_db int
        +redis_password str
    }

    class PlanCacheModule {
        +get_plan_cache()
        +clean_plan_cache()
    }

    class ContactUsModule {
        +CacheExpiration int
        +get_contact_cache()
        +clean_contact_cache()
    }

    class RedisSubscriberModule {
        +channel str
        +cache_clear_subscriber()
    }

    class FastAPIAppLifespan {
        +lifespan(app FastAPI)
    }

    Settings <.. RedisClient : uses
    RedisClient <.. RedisSubscriberModule : uses
    PlanCacheModule <.. RedisSubscriberModule : cleans
    ContactUsModule <.. RedisSubscriberModule : cleans
    RedisClient <.. FastAPIAppLifespan : managed_by
Loading

文件级变更

Change Details Files
将 Redis 订阅者的生命周期接入 FastAPI 应用的启动和关闭流程。
  • 导入 asynccontextmanager 和 Redis 订阅者工具。
  • 定义一个异步 lifespan 上下文管理器,在启动时连接 Redis 订阅者,在关闭时关闭 Redis 客户端。
  • 将该 lifespan 上下文管理器传入 FastAPI 应用构造函数。
  • 修正 ICP 路由模块的导入路径大小写问题。
main.py
为 contact_us 和 plan 缓存增加显式的缓存管理辅助函数,以支持外部失效操作。
  • 将 contact_us 缓存访问器从 get_cache 重命名为 get_contact_cache,以提升可读性。
  • 添加 clean_contact_cache,通过加锁来重置 contact_us 缓存。
  • 添加 clean_plan_cache,通过加锁来重置 plan 缓存。
  • 更新 contact_us 路由以使用重命名后的缓存访问器。
src/contact_us/__init__.py
src/plan/cache.py
扩展配置和依赖以支持 Redis 连接能力。
  • 在 Settings 模型中添加 Redis 连接配置(host、port、db、password),并提供默认值。
  • 在 requirements.txt 中添加 redis 依赖包。
src/config/__init__.py
requirements.txt
实现带有发布/订阅能力的异步 Redis 客户端封装,并添加一个在收到消息时清理缓存的后台订阅者。
  • 使用 redis.asyncio 创建 RedisClient 抽象,提供 connect、close、publish 和 subscribe 方法,并包含日志记录和带可选密码的连接 URL 处理。
  • 暴露一个单例 RedisClient 实例(rds)以及一个 start_subscriber 辅助函数,用于连接并调度缓存清理订阅任务。
  • 实现 cache_clear_subscriber:订阅 misc 渠道,监听消息,对每条消息清理 plan 和 contact_us 缓存,并在退出时取消订阅。
  • 提供 redis 包的 __init__ 以重新导出关键的 Redis 工具。
src/redis/client.py
src/redis/subscriber.py
src/redis/__init__.py

Tips and commands

Interacting with Sourcery

  • 触发新评审: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的评审评论。
  • 从评审评论生成 GitHub issue: 在某条评审评论下回复,请求 Sourcery 从该评论创建 issue。你也可以直接回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中任意位置写上 @sourcery-ai 即可随时生成标题。也可以在 pull request 中评论 @sourcery-ai title 以(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在对应位置生成 PR 摘要。也可以在 pull request 中评论 @sourcery-ai summary 以(重新)生成摘要。
  • 生成 Reviewer's Guide: 在 pull request 中评论 @sourcery-ai guide,即可(重新)生成 Reviewer's Guide。
  • 一次性解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理了所有评论且不想再看到它们,这会很有用。
  • 一次性忽略所有 Sourcery 评审: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 评审。特别适合在你想从头开始新的评审时使用——别忘了评论 @sourcery-ai review 以触发新的评审!

Customizing Your Experience

访问你的 dashboard 以:

  • 启用或禁用诸如 Sourcery 自动生成的 pull request 摘要、Reviewer's Guide 等评审功能。
  • 更改评审语言。
  • 添加、删除或编辑自定义评审说明。
  • 调整其他评审设置。

Getting Help

Original review guide in English

Reviewer's Guide

Introduces an async Redis-based pub/sub subscriber to invalidate in-memory caches on messages from a Redis channel, wires it into FastAPI’s lifespan, and adds explicit cache clearing helpers for plan and contact_us endpoints, plus related configuration and dependency updates.

Sequence diagram for Redis pubsub-driven cache invalidation

sequenceDiagram
    participant Publisher
    participant RedisServer
    participant RedisClient_rds
    participant cache_clear_subscriber
    participant plan_cache
    participant contact_us_cache

    Publisher->>RedisServer: PUBLISH misc "evict"

    RedisClient_rds->>RedisServer: SUBSCRIBE misc
    RedisServer-->>RedisClient_rds: message on misc

    RedisClient_rds->>cache_clear_subscriber: deliver message
    cache_clear_subscriber->>cache_clear_subscriber: check type == message

    cache_clear_subscriber->>plan_cache: clean_plan_cache()
    plan_cache->>plan_cache: set _plan_cache = None

    cache_clear_subscriber->>contact_us_cache: clean_contact_cache()
    contact_us_cache->>contact_us_cache: set cache = None
Loading

Class diagram for RedisClient and cache helpers

classDiagram
    class RedisClient {
        +redis
        +pubsub
        +__init__()
        +connect() bool
        +close() None
        +publish(channel str, message str) bool
        +subscribe(channel str)
    }

    class Settings {
        +redis_host str
        +redis_port int
        +redis_db int
        +redis_password str
    }

    class PlanCacheModule {
        +get_plan_cache()
        +clean_plan_cache()
    }

    class ContactUsModule {
        +CacheExpiration int
        +get_contact_cache()
        +clean_contact_cache()
    }

    class RedisSubscriberModule {
        +channel str
        +cache_clear_subscriber()
    }

    class FastAPIAppLifespan {
        +lifespan(app FastAPI)
    }

    Settings <.. RedisClient : uses
    RedisClient <.. RedisSubscriberModule : uses
    PlanCacheModule <.. RedisSubscriberModule : cleans
    ContactUsModule <.. RedisSubscriberModule : cleans
    RedisClient <.. FastAPIAppLifespan : managed_by
Loading

File-Level Changes

Change Details Files
Wire Redis subscriber lifecycle into FastAPI app startup and shutdown.
  • Import asynccontextmanager and Redis subscriber utilities.
  • Define an async lifespan context manager that connects the Redis subscriber on startup and closes the Redis client on shutdown.
  • Pass the lifespan context manager into the FastAPI app constructor.
  • Fix import path casing for the ICP router module.
main.py
Add explicit cache management helpers for contact_us and plan caches to support external invalidation.
  • Rename contact_us cache accessor from get_cache to get_contact_cache for clarity.
  • Add clean_contact_cache to reset the contact_us cache under a lock.
  • Add clean_plan_cache to reset the plan cache under a lock.
  • Update contact_us route to use the renamed cache accessor.
src/contact_us/__init__.py
src/plan/cache.py
Extend configuration and dependencies to support Redis connectivity.
  • Add Redis connection settings (host, port, db, password) to the Settings model with defaults.
  • Add redis package to requirements.txt.
src/config/__init__.py
requirements.txt
Implement an async Redis client wrapper with publish/subscribe and a background subscriber that clears caches on messages.
  • Create RedisClient abstraction using redis.asyncio with connect, close, publish, and subscribe methods, including logging and connection URL handling with optional password.
  • Expose a singleton RedisClient instance (rds) and a start_subscriber helper that connects and schedules the cache-clearing subscriber task.
  • Implement cache_clear_subscriber that subscribes to the 'misc' channel, listens for messages, and on each message clears plan and contact_us caches, then unsubscribes on exit.
  • Provide a redis package init to re-export key Redis utilities.
src/redis/client.py
src/redis/subscriber.py
src/redis/__init__.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

2 participants