Skip to content

User/Admin saves clear entire Rails cache, causing template cache misses #1270

Description

@philayres

Problem

On production, browsers refetch /pages/<token>/template more often than expected within the same session, even though app config hasn't changed.

Root cause (confirmed via logs): User and Admin both include AdminHandler, whose after_save :invalidate_cache calls Rails.cache.clear. Since User/Admin are saved on every sign-in (Devise trackable), failed login (lockable), and app-type switch, this wipes the entire shared memcached on nearly every request, destroying Application.server_cache_version and forcing a new template_version token for all users.

Additionally, partial_cache_key (in ApplicationHelper) includes u&.updated_at, so any user-record save changes the cache key even when nothing relevant (roles/app type/access) actually changed — e.g. switching app type A→B→A produces a different key than the original A session.

Plan

  1. In AdminHandler#invalidate_cache, keep full Rails.cache.clear for all admin models, but for User/Admin only clear when disabled actually changed (saved_change_to_disabled?); always reset latest_update memo regardless.
  2. Remove the u&.updated_at segment from partial_cache_key in ApplicationHelper (role/app-type/access changes are already covered by other segments).
  3. Add specs covering: user/admin save without disabled change does not clear cache; disabled change does; other AdminHandler models still clear on every save; partial_cache_key stability across non-relevant user saves.
  4. Review existing specs referencing the old clear-on-every-save behavior (admin/app_types_controller_spec.rb, pages_controller_spec.rb, reports_table_helper_spec.rb, memcached_clear_template_recovery_spec.rb).

Longer-term replacement of whole-cache clears with a namespaced/versioned cache-key scheme is out of scope for this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions