Module: DomainSwitcher::Cache

Defined in:
lib/domain_switcher/cache.rb

Class Method Summary collapse

Class Method Details

.initObject



4
5
6
# File 'lib/domain_switcher/cache.rb', line 4

def self.init
  $domain_switcher_cache_systems = {}
end

.switch(website) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/domain_switcher/cache.rb', line 8

def self.switch(website)
  namespace = website.cache_prefix || website.symbol

  if (c = RAILS_CACHE)
    if !$domain_switcher_cache_systems.key?(namespace)
      Rails.logger.info "DomainSwitcher: Create new cache system for namespace : #{c.class} | #{namespace}" 

      # MemcachedStore => Tested and working with Memcached gem only
      $domain_switcher_cache_systems[namespace] = if ActiveSupport::Cache::MemCacheStore === c
        d = RAILS_CACHE.instance_variable_get('@data')
        if d.prefix_key == namespace
          Rails.logger.info "DomainSwitcher: Use existing cache => namespace is the same: #{namespace}"
          RAILS_CACHE
        else
          c.class.new(d.class.new(d.instance_variable_get('@servers'), d.instance_variable_get('@options').merge(:namespace => namespace, :prefix_key => namespace)))
        end
      elsif c.is_a? ActiveSupport::Cache::FileStore
        c.class.new("tmp/cache/#{namespace}")
      else
        c
      end
    end
    ActionController::Caching.send(:class_variable_set, :@@cache_store, (Thread.current[:domain_switcher_cache] = $domain_switcher_cache_systems[namespace]))
  end

end