Search code examples
ruby-on-railscaching

Rails low level caching with Solid Cache: getting `warning: already initialized constant`


I'm trying to cache some query data using Rails "low level caching" and Solid Cache - pretty simple stuff.

Here is my caching method:

class Location < ApplicationRecord
  def self.location_name_cache
    Rails.cache.fetch(:location_names, expires_in: 15.minutes) do
      (Location.pluck(:name) + Observation.pluck(:where)).uniq
    end
  end

  # Check if a given place name already exists, defined as a Location name string.
  def self.location_name_exists(name)
    location_name_cache.member?(name)
  end

But when I unit-test my class method (using Minitest), although the tests pass, i'm getting these warnings:

warning: already initialized constant SolidCache::Record::NULL_INSTRUMENTER
warning: previous definition of NULL_INSTRUMENTER was here

What am I doing wrong?

I've tried bundle clean --force and bundle install in case it was a stale gem thing, but that's the only issue search turned up that seemed to match my case.

Here's my caching configs:

# config/application.rb
  config.cache_store = :solid_cache_store
  config.solid_cache.connects_to = { database: { writing: :cache } }

# config/environments/test.rb - idk if these are relevant
  config.cache_classes = true
  config.action_view.cache_template_loading = true

# config/environments/development.rb - probably not relevant
  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
  if Rails.root.join("tmp/caching-dev.txt").exist?
    config.action_controller.perform_caching = true
    # debugging: log fragment reads/writes
    # (it will show [cache hit] even if set to false)
    config.action_controller.enable_fragment_cache_logging = true

    config.cache_store = :memory_store # :mem_cache_store via application.rb
    config.public_file_server.headers = {
      "Cache-Control" => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false
  end
Or is this a case where the db will cache the results anyway, and a cache is inadvisable?

Solution

  • This has been debugged by the authors. Turns out the gem's error handling was swallowing cache db configuration errors. Fix is on the way.