복붙노트

[REDIS] 이니셜 라이저에서 cache_store 설정

REDIS

이니셜 라이저에서 cache_store 설정

내 레일 3 cache_store로 레디 스-저장소를 사용하도록 노력하고있어. 또한이 이니셜 라이저 / app_config.rb하는 부하 구성 설정에 대한 YAML 파일을. 내 초기화에 / 내가 가진 redis.rb :

MyApp::Application.config.cache_store = :redis_store, APP_CONFIG['redis'] 

그러나이 작업에 나타나지 않습니다. 만약 내가한다면:

Rails.cache

내 레일에서 것은 내가 명확하게 사용하고 볼 수있는 콘솔

ActiveSupport.Cache.FileStore

캐시 저장소 대신 레디 스 매장 등. 그러나, 나는 이런 내 application.rb 파일에 설정을 추가하는 경우 :

config.cache_store = :redis_store 

그것은 내가 APP_CONFIG에 액세스 할 수없는, 그래서 application.rb 후로드 초기화 응용 프로그램의 설정을 제외하고 잘 작동합니다.

사람이 경험 했습니까? 나는 초기화에서 캐시 상점을 설정할 수없는 것.

해결법

  1. ==============================

    1.몇 가지 조사 후, 가능한 설명은 레일 전에 방법을 실행 초기화 initialize_cache이 / 초기화가 있다는 것입니다. 그것은 이전에 실행 체인에 정의되어 있지 않은 경우 그럼 캐시 저장소가 설정 될 실 거예요. 당신은 application.rb 또는 환경 / production.rb처럼, 체인의 이전을 구성해야

    몇 가지 조사 후, 가능한 설명은 레일 전에 방법을 실행 초기화 initialize_cache이 / 초기화가 있다는 것입니다. 그것은 이전에 실행 체인에 정의되어 있지 않은 경우 그럼 캐시 저장소가 설정 될 실 거예요. 당신은 application.rb 또는 환경 / production.rb처럼, 체인의 이전을 구성해야

    응용 프로그램은 다음과 같이 구성되기 전에 내 솔루션은 APP_CONFIG로드를 이동했다 :

    APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]
    

    다음 동일한 파일 :

    config.cache_store = :redis_store, APP_CONFIG['redis']
    

    또 다른 옵션은 before_configuration 블록에서 cache_store을 넣어했다, 이런 식으로 뭔가 :

    config.before_configuration do
      APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]
      config.cache_store = :redis_store, APP_CONFIG['redis']
    end
    
  2. ==============================

    2.는 config는 / 초기화 후 설정 / application.rb 및 설정 / 환경, Rails.cache 초기화 후 실행되지만.

    는 config는 / 초기화 후 설정 / application.rb 및 설정 / 환경, Rails.cache 초기화 후 실행되지만.

    따라서, 하나 개의 솔루션은 캐시의 설정 / application.rb 또는 설정 / 환경 / *. RB를 구성하는 것입니다.

    캐시가 의도적으로 초기화에서 구성해야하는 경우이 수동으로 설정 한 후 Rails.cache를 설정하여 수행 할 수 있습니다 :

    # config/initializers/cache.rb
    Rails.application.config.cache_store = :redis_store, APP_CONFIG['redis']
    
    # Make sure to add this line (http://stackoverflow.com/a/38619281/2066546):
    Rails.cache = ActiveSupport::Cache.lookup_store(Rails.application.config.cache_store)
    

    확인하기 위하여는, 일이 같은 사양을 추가 :

    # spec/features/smoke_spec.rb
    require 'spec_helper'
    
    feature "Smoke test" do
      scenario "Testing the rails cache" do
        Rails.cache.write "foo", "bar"
        expect(Rails.cache.read("foo")).to eq "bar"
        expect(Rails.cache).to be_kind_of ActiveSupport::Cache::RedisStore
      end
    end
    
  3. ==============================

    3.나는 다음을 시도하고 밖으로 작동합니다.

    나는 다음을 시도하고 밖으로 작동합니다.

    MyApp::Application.config.cache_store = :redis_store
    self.class.send :remove_const, :RAILS_CACHE if self.class.const_defined? :RAILS_CACHE
    RAILS_CACHE = ActiveSupport::Cache.lookup_store(MyApp::Application.config.cache_store)
    
  4. ==============================

    4.변경하는 경우가 있었다 원래의 설정으로, 도움을 수행합니다

    변경하는 경우가 있었다 원래의 설정으로, 도움을 수행합니다

    MyApp::Application.config.cache_store = :redis_store, APP_CONFIG['redis'] 
    

    에:

    MyApp::Application.config.cache_store = :redis_store, APP_CONFIG['redis'] 
    RAILS_CACHE = MyApp::Application.config.cache_store
    

    ?

  5. ==============================

    5.MyApp를 동일한 문제 및 설정 RAILS_CACHE을했다 : Application.config.cache_store뿐만 아니라 그것을 해결했습니다.

    MyApp를 동일한 문제 및 설정 RAILS_CACHE을했다 : Application.config.cache_store뿐만 아니라 그것을 해결했습니다.

  6. ==============================

    6.이니셜 라이저에서

    이니셜 라이저에서

    레디 스 || = Rails.configuration.redis_client

    application.rb에서

    config.redis_client = Redis.new({
      :host => ENV["REDIS_HOST"], 
      :port => ENV["REDIS_PORT"],
      :db => ENV["REDIS_DB"].to_i,
    })
    
    config.cache_store = :redis_store, { client: config.redis_client }
    
  7. from https://stackoverflow.com/questions/5810289/setting-the-cache-store-in-an-initializer by cc-by-sa and MIT license