Skip to content

Conversation

@ericproulx
Copy link
Contributor

@ericproulx ericproulx commented Nov 11, 2025

Refactor Coercer Caching to Use Grape::Util::Cache

Summary

This PR refactors the coercer caching mechanism to use the standardized Grape::Util::Cache pattern instead of manual cache management. This improves code consistency, maintainability, and follows the existing patterns used throughout the Grape codebase.

Fix #2515

Changes

Core Refactoring

  • Replaced manual cache management with Grape::Util::Cache singleton pattern
  • Moved type mappings to dedicated cache classes:
    • Grape::DryTypes::StrictCache - handles strict type mappings
    • Grape::DryTypes::ParamsCache - handles params type mappings
  • Simplified build_coercer method by removing manual cache key generation and mutex-based synchronization
  • Created CoercerCache class that extends Grape::Util::Cache for coercer instances

Code Improvements

  • Centralized type mappings in cache classes, making them easier to maintain and extend
  • Removed manual cache variables (@__cache and @__cache_write_lock) from Types module
  • Simplified PrimitiveCoercer to use centralized cache classes instead of maintaining its own mappings
  • Updated ArrayCoercer to use cache classes directly instead of scope-based lookup

Documentation Updates

  • Updated dry-types documentation links from version 1.2 to main (current documentation)
  • Limited Dry.Types() inclusion to only what Grape uses (:params, :coercible, :strict)

Test Updates

  • Removed tests for internal cache variables (no longer needed)
  • Updated test expectations to use new cache class structure
  • Simplified cache testing to verify behavior rather than implementation details

Benefits

  1. Consistency: Uses the same caching pattern as other parts of Grape (e.g., PartsCache, PatternCache, JoinedSpaceCache)
  2. Maintainability: Centralized type mappings make it easier to add new types or modify existing ones
  3. Simplicity: Removes manual mutex synchronization and cache key generation
  4. Performance: Singleton pattern ensures efficient memory usage and thread-safe access

Files Changed

  • lib/grape/dry_types.rb - Added StrictCache and ParamsCache classes
  • lib/grape/validations/types.rb - Refactored caching to use CoercerCache
  • lib/grape/validations/types/array_coercer.rb - Updated to use cache classes
  • lib/grape/validations/types/dry_type_coercer.rb - Simplified to use cache classes
  • lib/grape/validations/types/primitive_coercer.rb - Refactored to use centralized cache
  • spec/grape/validations/types/primitive_coercer_spec.rb - Updated test expectations
  • spec/grape/validations/types_spec.rb - Simplified cache tests

Testing

All existing tests pass. The refactoring maintains backward compatibility while improving the internal implementation.

- Replace manual cache management with Grape::Util::Cache
- Move type mappings to dedicated StrictCache and ParamsCache classes
- Simplify build_coercer method by removing manual cache logic
- Update dry-types documentation links from 1.2 to main
- Refactor PrimitiveCoercer to use centralized cache classes
@dblock dblock merged commit 07fcf03 into master Nov 12, 2025
103 checks passed
@dblock dblock deleted the fix_coercer_cache branch November 12, 2025 12:28
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.

Coercer's cache may contains keys that are binded to memory location.

3 participants