Skip to content

Caching in Rails throws an exception by default #308

@bradgessler

Description

@bradgessler

While I was shooting the caching unit for the Phlex on Rails course, I was surprised about the behavior of the cache.

Here's the commit where I discovered the issue: thingybase/server@c9a416f

To recreate, spin up a Rails app, use cache in any Phlex component in Rails, then run rails dev:cache to enable the cache. You'll get an exception from this block:

	# Override this method to use a different cache store.
	private def cache_store
		raise "Cache store not implemented."
	end

This surprised me for a few reasons.

  1. I assumed from the docs Phlex caching in Rails would use the in-memory FIFO store (https://www.phlex.fun/components/caching.html)
  2. When I enabled the cache, I was surprised to see the exception message.

I think a reasonable behavior, in terms of "least surprises", is to add this into Components::Base by default, or equivalent.

class Components::Base
  def cache_store
    Rails.cache
  end
end

Additionally, I don't think this is implemented correctly in phlex-rails

	def low_level_cache(...)
		Rails.application.config.action_controller.perform_caching ? super : yield
	end

I get that this "disables caching", but I think the correct behavior would be to delegate this to Rails's cache store and let it handle what to do when a value is disabled or enabled.

I ran into this problem when I was trying to diagnose an issue with my cache not working that ended up not being a bug (I enabled cache for a different Raisl project 🤣)

What I'd propose for the "fix" is something like this:

  1. Delete the low_level_cache method in phlex-rails and let cache_store handle no-ops.
  2. Either patch cache_store in the phlex-rails gem to include Rails.cache (principle of least surprise) or include it in the generator.

I can put all of this into a PR, but wanted to check on here first to make sure I'm not overlooking some sort of default behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions