Sha256: d3ed35612ac50aa5b0ed8e4fdd9996db1855b153416005b1409bf6abc6aa79c2

Contents?: true

Size: 1.36 KB

Versions: 31

Compression:

Stored size: 1.36 KB

Contents

require 'active_support/concern'

# expects underlying model to have filename, class, and id attributes
module HalApi::Representer::Caches
  extend ActiveSupport::Concern
  require 'hal_api/representer/caches/serialized_json'

  # Pass in an option for the format this is going `to_`
  # used in caching the final string format of the obj
  # rather than the intermediary `Hash`, a modest accelerant
  def to_json(options={})
    options[:to_] = :json
    super(options)
  end

  # This isn't working, comment out this optimization
  # def create_representation_with(doc, options, format)
  #   cache.fetch(cache_key(represented, options), cache_options) do
  #     response = super(doc, options, format)
  #     response = HalApi::Representer::Caches::SerializedJson.new(JSON.dump(response)) if (options[:to_] == :json)
  #     response
  #   end
  # end

  def cache_key(obj, options)
    key_components = [cache_key_class_name]
    key_components << (obj.try(:is_root_resource) ? 'r' : 'l')
    key_components << obj
    key_components << options.select{|k,v| Array(options['_keys']).include?(k.to_s)}

    ActiveSupport::Cache.expand_cache_key(key_components)
  end

  def cache
    Rails.cache
  end

  def cache_key_class_name
    self.class.name.underscore.gsub(/_representer$/, "")
  end

  def cache_options
    {compress: true, race_condition_ttl: 10, expires_in: 1.hour}
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
hal_api-rails-1.2.2 lib/hal_api/representer/caches.rb
hal_api-rails-1.2.1 lib/hal_api/representer/caches.rb
hal_api-rails-1.2.0 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.5 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.4 lib/hal_api/representer/caches.rb
hal_api-rails-0.3.7 lib/hal_api/representer/caches.rb
hal_api-rails-0.3.6 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.3 lib/hal_api/representer/caches.rb
hal_api-rails-0.3.5 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.2 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.1 lib/hal_api/representer/caches.rb
hal_api-rails-1.1.0 lib/hal_api/representer/caches.rb
hal_api-rails-1.0.0 lib/hal_api/representer/caches.rb
hal_api-rails-0.7.0 lib/hal_api/representer/caches.rb
hal_api-rails-0.6.0 lib/hal_api/representer/caches.rb
hal_api-rails-0.5.0 lib/hal_api/representer/caches.rb
hal_api-rails-0.4.1 lib/hal_api/representer/caches.rb
hal_api-rails-0.4.0 lib/hal_api/representer/caches.rb
hal_api-rails-0.3.4 lib/hal_api/representer/caches.rb
hal_api-rails-0.3.3 lib/hal_api/representer/caches.rb