Class: JSONAPI::CachedResourceFragment

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/cached_resource_fragment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, id, type, context, fetchable_fields, relationships, links_json, attributes_json, meta_json) ⇒ CachedResourceFragment

Returns a new instance of CachedResourceFragment.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jsonapi/cached_resource_fragment.rb', line 36

def initialize(resource_klass, id, type, context, fetchable_fields, relationships,
               links_json, attributes_json, meta_json)
  @resource_klass = resource_klass
  @id = id
  @type = type
  @context = context
  @fetchable_fields = Set.new(fetchable_fields)

  # Relationships left uncompiled because we'll often want to insert included ids on retrieval
  @relationships = relationships

  @links_json = CompiledJson.of(links_json)
  @attributes_json = CompiledJson.of(attributes_json)
  @meta_json = CompiledJson.of(meta_json)

  # A hash of hashes
  @preloaded_fragments ||= Hash.new
end

Instance Attribute Details

#attributes_jsonObject (readonly)

Returns the value of attribute attributes_json.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def attributes_json
  @attributes_json
end

#contextObject (readonly)

Returns the value of attribute context.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def context
  @context
end

#fetchable_fieldsObject (readonly)

Returns the value of attribute fetchable_fields.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def fetchable_fields
  @fetchable_fields
end

#idObject (readonly)

Returns the value of attribute id.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def id
  @id
end

Returns the value of attribute links_json.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def links_json
  @links_json
end

#meta_jsonObject (readonly)

Returns the value of attribute meta_json.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def meta_json
  @meta_json
end

#preloaded_fragmentsObject (readonly)

Returns the value of attribute preloaded_fragments.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def preloaded_fragments
  @preloaded_fragments
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def relationships
  @relationships
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def resource_klass
  @resource_klass
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32

def type
  @type
end

Class Method Details

.fetch_fragments(resource_klass, serializer, context, cache_ids) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jsonapi/cached_resource_fragment.rb', line 3

def self.fetch_fragments(resource_klass, serializer, context, cache_ids)
  serializer_config_key = serializer.config_key(resource_klass).gsub("/", "_")
  context_json = resource_klass.attribute_caching_context(context).to_json
  context_b64 = JSONAPI.configuration.resource_cache_digest_function.call(context_json)
  context_key = "ATTR-CTX-#{context_b64.gsub("/", "_")}"

  results = self.lookup(resource_klass, serializer_config_key, context, context_key, cache_ids)

  miss_ids = results.select{|k,v| v.nil? }.keys
  unless miss_ids.empty?
    find_filters = {resource_klass._primary_key => miss_ids.uniq}
    find_options = {context: context}
    resource_klass.find(find_filters, find_options).each do |resource|
      (id, cr) = write(resource_klass, resource, serializer, serializer_config_key, context, context_key)
      results[id] = cr
    end
  end

  if JSONAPI.configuration.resource_cache_usage_report_function
    JSONAPI.configuration.resource_cache_usage_report_function.call(
      resource_klass.name,
      cache_ids.size - miss_ids.size,
      miss_ids.size
    )
  end

  return results
end

.from_cache_value(resource_klass, context, h) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jsonapi/cached_resource_fragment.rb', line 92

def self.from_cache_value(resource_klass, context, h)
  new(
    resource_klass,
    h.fetch(:id),
    h.fetch(:type),
    context,
    h.fetch(:fetchable),
    h.fetch(:rels, nil),
    h.fetch(:links, nil),
    h.fetch(:attrs, nil),
    h.fetch(:meta, nil)
  )
end

.lookup(resource_klass, serializer_config_key, context, context_key, cache_ids) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsonapi/cached_resource_fragment.rb', line 74

def self.lookup(resource_klass, serializer_config_key, context, context_key, cache_ids)
  type = resource_klass._type

  keys = cache_ids.map do |(id, cache_key)|
    [type, id, cache_key, serializer_config_key, context_key]
  end

  hits = JSONAPI.configuration.resource_cache.read_multi(*keys).reject{|_,v| v.nil? }
  return keys.each_with_object({}) do |key, hash|
    (_, id, _, _) = key
    if hits.has_key?(key)
      hash[id] = self.from_cache_value(resource_klass, context, hits[key])
    else
      hash[id] = nil
    end
  end
end

.write(resource_klass, resource, serializer, serializer_config_key, context, context_key) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jsonapi/cached_resource_fragment.rb', line 106

def self.write(resource_klass, resource, serializer, serializer_config_key, context, context_key)
  (id, cache_key) = resource.cache_id
  json = serializer.object_hash(resource) # No inclusions passed to object_hash
  cr = self.new(
    resource_klass,
    json['id'],
    json['type'],
    context,
    resource.fetchable_fields,
    json['relationships'],
    json['links'],
    json['attributes'],
    json['meta']
  )

  key = [resource_klass._type, id, cache_key, serializer_config_key, context_key]
  JSONAPI.configuration.resource_cache.write(key, cr.to_cache_value)
  return [id, cr]
end

Instance Method Details

#to_cache_valueObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jsonapi/cached_resource_fragment.rb', line 55

def to_cache_value
  {
    id: id,
    type: type,
    fetchable: fetchable_fields,
    rels: relationships,
    links: links_json.try(:to_s),
    attrs: attributes_json.try(:to_s),
    meta: meta_json.try(:to_s)
  }
end

#to_real_resourceObject



67
68
69
70
# File 'lib/jsonapi/cached_resource_fragment.rb', line 67

def to_real_resource
  rs = Resource.resource_for(self.type).find_by_keys([self.id], {context: self.context})
  return rs.try(:first)
end