module Jamf::Connection::Cache

This module defines attributes and methods related to the caching of certain data from the API. TODO: Remove this when we no longer support caching from either API

Constants

EXTENDABLE_CLASSES

These classes are extendable, their Extension Attributes in the classic API are cached locally in the connection object.. NOTE: These are strings, not references to the actual classes. Otherwise, they and all the classes they reference get loaded when ruby-jss is required

Public Instance Methods

c_ext_attr_definition_cache() click to toggle source

@return [Hash{Class: Hash{String => Jamf::ExtensionAttribute}}]

This Hash caches the C-API Extension Attribute
definition objects for the three types of ext. attribs:
ComputerExtensionAttribute, MobileDeviceExtensionAttribute, and
UserExtensionAttribute, whenever they are fetched for parsing or
validating extention attribute data.

The top-level keys are the EA classes themselves:
- ComputerExtensionAttribute
- MobileDeviceExtensionAttribute
- UserExtensionAttribute

These each point to a Hash of their instances, keyed by name, e.g.
  {
   "A Computer EA" => <Jamf::ComputerExtensionAttribute...>,
   "A different Computer EA" => <Jamf::ComputerExtensionAttribute...>,
   ...
  }
   # File lib/jamf/api/connection/cache.rb
84 def c_ext_attr_definition_cache
85   @c_ext_attr_definition_cache ||= Concurrent::Map.new
86 end
c_object_list_cache() click to toggle source

@return [Concurrent::Map] This Hash-like object caches the results of

C-API queries for an APIObject
subclass's .all summary list, keyed by the subclass's RSRC_LIST_KEY.
See the APIObject.all class method.

It also caches related data items for speedier processing:

- The Hashes created by APIObject.map_all_ids_to(other_key), keyed by
  "#{RSRC_LIST_KEY}_map_#{other_key}".to_sym

- This hash also holds a cache of the rarely-used APIObject.all_objects
  hash, keyed by "#{RSRC_LIST_KEY}_objects".to_sym

When APIObject.all, and related methods are called without an argument,
and this hash has a matching value, the value is returned, rather than
requerying the API. The first time a class calls .all, or whenever refresh
is not false, the API is queried and the value in this hash is updated.
   # File lib/jamf/api/connection/cache.rb
61 def c_object_list_cache
62   @c_object_list_cache ||= Concurrent::Map.new
63 end
flushcache(key_or_klass = nil) click to toggle source

Empty cached lists from this connection then run garbage collection to clear any available memory

See the getters for

NOTE since all ruby variables are references to objects in memory, if you’ve referenced objects in these caches, those objects won’t be removed from memory by garbage collection but all cached data will be recached as needed.

e.g.

my_ref = Jamf::SomeClass.all
# my_ref now points to the same cached hash that Jamf::SomeClass.all does

my_connection.flushcache
# Jamf::SomeClass.all now points to an empty hash in the cache, but the one
# that my_ref points to still exists, full of the original data. Because
# my_ref still points to it, garbage collection doesn't remove it from
# memory

Jamf::SomeClass.all
# Jamf::SomeClass.all re-reads the data from the API and populates the
# Hash in the cache with new data, potentially different from that you're
# still seeing in my_ref

@param key_or_klass[Symbol, Class] Flush only the caches for the given

RSRC_LIST_KEY. or the EAdef cache for the given extendable class.
If nil (the default), flushes all caches

@return [void]

    # File lib/jamf/api/connection/cache.rb
122 def flushcache(key_or_klass = nil)
123   # EA defs for just one extendable class?
124   if EXTENDABLE_CLASSES.include? key_or_klass.to_s
125     @c_ext_attr_definition_cache[key_or_klass] = Concurrent::Map.new
126 
127   # one API object class?
128   elsif key_or_klass
129     map_key_pfx = "#{key_or_klass}_map_"
130     @c_object_list_cache.each_key do |cache_key|
131       next unless cache_key == key_or_klass || cache_key.to_s.start_with?(map_key_pfx)
132 
133       @c_object_list_cache.delete cache_key
134     end
135 
136   # flush everything
137   else
138     @c_object_list_cache = Concurrent::Map.new
139     @c_ext_attr_definition_cache = Concurrent::Map.new
140   end
141 
142   GC.start
143 end