Class: Grape::ContentTypes::MimeTypesCache

Inherits:
Util::Cache
  • Object
show all
Defined in:
lib/grape/content_types.rb

Overview

Both tables below are derived from nothing but the content-type registry, and one content-type-aware middleware is built per API instance — so an app mounting N APIs held N copies of tables it only ever reads. Keying the cache on the registry itself collapses them: Hash keys compare by value, so every API that registers the same content types shares one table.

Both the key and the table are frozen: the caller's registry stays reachable (through middleware.options[:content_types], among others) and mutating a live key would corrupt a cache that is now shared process-wide.

Instance Attribute Summary

Attributes inherited from Util::Cache

#cache

Instance Method Summary collapse

Methods inherited from Util::Cache

#[]

Constructor Details

#initializeMimeTypesCache

Returns a new instance of MimeTypesCache.



66
67
68
69
70
71
# File 'lib/grape/content_types.rb', line 66

def initialize
  super
  @cache = Hash.new do |h, from_settings|
    h[from_settings.dup.freeze] = from_settings.invert.transform_keys! { |mime_type| Grape::ContentTypes.media_type(mime_type) }.freeze
  end
end