Sha256: 4708a09562ea4bb7da9c125c37795f1305c20ea4232cdd086ee07b49d58e485a

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

module Scrivito

# This module manages storage of different kind of cache data.
module CmsDataCache
  VERSION = 'v2'.freeze

  class << self
    attr_reader :second_level_cache
    attr_writer :cache_path

    def cache
      @cache_chain ||= Cache::RamStore.new(next_store: first_level_cache, cache_prefix: VERSION)
    end

    def cache_path=(path)
      clear_cache_chain
      @cache_path = path
    end

    def first_level_cache
      @first_level_cache ||= Cache::FileStore.new(path: @cache_path, next_store: second_level_cache)
    end

    def second_level_cache=(cache_store)
      clear_cache_chain
      @second_level_cache = cache_store
    end

    SCHEMA = {
      workspace_data:     'workspace/#{workspace_id}',

      # CONTENT SERVICE
      content_state:      'content/#{content_state_id}',
      obj_data:           'content/#{content_state_id}/obj/#{index}/#{key}',
      obj_classes_data:   'content/#{content_state_id}/obj_classes',

      # REST API
      obj_data_rest:      'obj/#{cache_id}/#{index}/#{key}',
      content_state_node: 'csn/#{content_state_id}',
      workspace_csid:     'wscsid/#{workspace_id}'
    }

    SCHEMA.each do |name, schema|
      params = schema.scan(/\#{([^}]+)}/).map(&:first)
      params_code = params.map(&:to_s).join(",")

      # using eval instead of define_method for performance reasons
      class_eval(<<-END, __FILE__, __LINE__ + 1)
        def read_#{name}(#{params_code})
          cache.read("#{schema}")
        end
      END

      class_eval(<<-END, __FILE__, __LINE__ + 1)
        def write_#{name}(#{params_code}, data)
          if data == nil
            raise InternalError, "tried to write nil into #{schema}"
          end
          cache.write("#{schema}", data)
        end
      END
    end

    private

    def clear_cache_chain
      @cache_chain = nil
      @first_level_cache = nil
      @second_level_cache = nil
    end
  end
end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scrivito_sdk-0.65.2 lib/scrivito/cms_data_cache.rb
scrivito_sdk-0.65.1 lib/scrivito/cms_data_cache.rb
scrivito_sdk-0.65.0 lib/scrivito/cms_data_cache.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/cms_data_cache.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/cms_data_cache.rb