Module: CommonDataCaching

Defined in:
lib/common_data_caching.rb,
lib/common_data_caching/version.rb,
lib/common_data_caching/callbacks.rb,
lib/common_data_caching/class_methods.rb

Defined Under Namespace

Modules: Callbacks, ClassMethods

Constant Summary collapse

CACHE_KEY_PREFIX =
'common-data-caching_'.freeze
VERSION =
'1.4.2'.freeze

Class Method Summary collapse

Class Method Details

.all_collectionsObject



18
19
20
21
22
23
24
25
26
# File 'lib/common_data_caching.rb', line 18

def self.all_collections
  versions.map do |key, _version|
    entity = key.sub(CACHE_KEY_PREFIX, '')
    {
      entity:,
      objects: collection(entity)
    }
  end
end

.collection(key) ⇒ Object



14
15
16
# File 'lib/common_data_caching.rb', line 14

def self.collection(key)
  Rails.cache.fetch("#{CACHE_KEY_PREFIX}#{key}") || []
end

.update_cacheObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/common_data_caching.rb', line 28

def self.update_cache
  # получение списка моделей
  Rails.application.eager_load!
  models = ActiveRecord::Base.descendants

  # удаление старых версий кеша
  Rails.cache.delete('common-data-caching-versions')

  models.each do |model|
    # проверка - в модели предусмотрено кеширование общих данных
    next unless model.common_data_caching?

    # обновление кеша
    model.update_common_data_cache

    # обновление версий кеша
    model.update_common_data_cache_versions
  end
end

.versionsObject



10
11
12
# File 'lib/common_data_caching.rb', line 10

def self.versions
  Rails.cache.read('common-data-caching-versions').transform_keys { |k| k.sub(CACHE_KEY_PREFIX, '') } || {}
end