class PIM::Services::DataModelLoaders::CacheServiceLoader

Loader to use a “external” cache service.

In order to make this work, a simple “CacheService” implementation needs to be provided in the external environment which defines the following two methods:

get base_keys, key put base_keys, key, value

- base_keys is an Map(!) which includes entries for service_name, data_model_hash, request_name and an optional parent_name
- key is the sub-key to the base_keys which CAN be nil
- value is the object to be cached as String using "Marshal.dump" method which CAN be nil

Basically, the CacheService should be able to create the actual “cache key” however it is best implemented, e.g. simply join each key-part with a ‘:’ character, or have clearable “sub” caches etc.

Public Class Methods

new() click to toggle source
# File services.rb, line 1577
def initialize

  # Get external cache service, if possible
  @cache_service = PIM::Services.__external_service(:cacheService)

  # TODO:
  # We have to provide a Map with service-name to datamodel-hash values to be used as the "base" key.
  # This should be set when initializing the runtime!
  @data_model_services = PIM.data_model_services || {}

end

Public Instance Methods

enabled?() click to toggle source
# File services.rb, line 1589
def enabled?
  return !@cache_service.nil?
end
get(data_module, request) click to toggle source
# File services.rb, line 1593
def get data_module, request

  cache_base_keys = get_cache_base_keys(data_module, request)
  return nil if get_cache_base_keys.nil?

  result = {}
  request.keys.each do |key|
    value = @cache_service.get(cache_base_keys, key)
    result[key] = PIM.is_empty?(value) ? nil : Marshal.restore(value)
  end

end
put(data_module, request, result) click to toggle source
# File services.rb, line 1606
def put data_module, request, result

  cache_base_keys = get_cache_base_keys(data_module, request)
  return if get_cache_base_keys.nil?

  result.each_pair do |key, value|
    value = PIM.is_empty?(value) ? nil : Marshal.dump(value)
    @cache_service.put(cache_base_keys, key, value)
  end

end