Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 23, 2025

Rails cache store that persists entries as version-controllable files, ignoring expiration to enable deterministic builds and shared caching across environments.

Implementation

  • Cache store: Inherits ActiveSupport::Cache::Store, stores each entry as SHA256-hashed .key and .value file pairs
  • Expiration handling: Strips expiration from deserialized entries to prevent time-based invalidation
  • Error handling: File I/O failures return false rather than raising, with StandardError rescue scopes
  • Test coverage: 23 specs validating write/read, deletion, expiration ignore, edge cases (nil, booleans, long keys)
  • CI: GitHub Actions workflow across Ruby 3.2-3.3 with minimal permissions
  • Requirements: Ruby 3.2+ and Rails 7.1+

Usage

# config/application.rb
config.cache_store = :source_control_cache_store, 
  cache_path: Rails.root.join("tmp", "cache")

# Creates deterministic file structure
Rails.cache.write("user:123", {name: "Alice"})
# => tmp/cache/a1b2c3d4...key  (contains "user:123")
# => tmp/cache/a1b2c3d4...value (serialized hash)

# Ignores expiration
Rails.cache.write("key", "value", expires_in: 1.hour)
Rails.cache.read("key")  # Always returns "value", never expires

Design decisions

  • SHA256 hashing ensures filesystem safety for arbitrary keys (special chars, long strings)
  • Two-file structure enables key inspection without deserialization
  • Expiration stripped on read rather than rejected on write to maintain Store interface compatibility
Original prompt

This project is a gem (SourceControlCacheStore) exposing a Rails cache store (compatible with Rails 7.1 and higher) that caches in the following way:

  1. Has a directory specific in its store declaration for where files should go
  2. Does NOT honor expiration params
  3. Hashes each key and uses the hash for filenames
  4. Stores two files for each request:
  • #{hash}.key - the full key that was used
  • #{hash}.value - the full value that was stored

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 7 commits November 23, 2025 21:38
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Rails cache store with specific features Implement SourceControlCacheStore: file-based Rails cache for version control Nov 23, 2025
Copilot finished work on behalf of nhorton November 23, 2025 21:52
Copilot AI requested a review from nhorton November 23, 2025 21:52
@nhorton
Copy link
Contributor

nhorton commented Nov 24, 2025

@copilot - make this require Ruby 3.2+. Note that it means you need to reduce the CI versions that are tested.

Co-authored-by: nhorton <204146+nhorton@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 24, 2025

@copilot - make this require Ruby 3.2+. Note that it means you need to reduce the CI versions that are tested.

Updated to require Ruby 3.2+ and reduced CI matrix to test only Ruby 3.2 and 3.3 in commit 71930ca.

Copilot AI requested a review from nhorton November 24, 2025 17:31
Copilot finished work on behalf of nhorton November 24, 2025 17:31
@nhorton nhorton marked this pull request as ready for review November 24, 2025 17:34
@nhorton nhorton merged commit eb12d0a into main Nov 24, 2025
4 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.

2 participants