Module: Jamf::Connection::Cache

Included in:
Jamf::Connection
Defined in:
lib/jamf/api/connection/cache.rb

Overview

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

Constant Summary collapse

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

%w[Jamf::Computer Jamf::MobileDevice Jamf::User].freeze

Instance Method Summary collapse

Instance Method Details

#c_ext_attr_definition_cacheHash{Class: Hash{String => Jamf::ExtensionAttribute}}



84
85
86
# File 'lib/jamf/api/connection/cache.rb', line 84

def c_ext_attr_definition_cache
  @c_ext_attr_definition_cache ||= Concurrent::Map.new
end

#c_object_list_cacheConcurrent::Map



61
62
63
# File 'lib/jamf/api/connection/cache.rb', line 61

def c_object_list_cache
  @c_object_list_cache ||= Concurrent::Map.new
end

#flushcache(key_or_klass = nil) ⇒ void

This method returns an undefined value.

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

See the getters for

  • c_object_list_cache

  • c_ext_attr_definition_cache

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


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jamf/api/connection/cache.rb', line 122

def flushcache(key_or_klass = nil)
  # EA defs for just one extendable class?
  if EXTENDABLE_CLASSES.include? key_or_klass.to_s
    @c_ext_attr_definition_cache[key_or_klass] = Concurrent::Map.new

  # one API object class?
  elsif key_or_klass
    map_key_pfx = "#{key_or_klass}_map_"
    @c_object_list_cache.each_key do |cache_key|
      next unless cache_key == key_or_klass || cache_key.to_s.start_with?(map_key_pfx)

      @c_object_list_cache.delete cache_key
    end

  # flush everything
  else
    @c_object_list_cache = Concurrent::Map.new
    @c_ext_attr_definition_cache = Concurrent::Map.new
  end

  GC.start
end