common-data-caching 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a082bb986847e69fc1e8d00e0ca871c87ba71cf91fdbaad036dbb7212f3a15e
4
- data.tar.gz: be69e9b648072e06927fa1808eb17fc6bb2a50cae15bd3fe1cee808439599d5b
3
+ metadata.gz: 56012b8561fa7985f849ba78ba4e4353a4c140df1ff237aaa7a7463af9efe14a
4
+ data.tar.gz: aeb5ecfffd8c492de47a8ea5abc141fec20c1605873132e2cb07135a24f32c37
5
5
  SHA512:
6
- metadata.gz: 106b64506c7d3ca88b2e5b1caae0e2ab4025475ee07f574983e5d66c0c8fc329e4e8ef1adf0e3d15f15f0bb87b232b836a2f4e4880b6ccb5d858388d0a29357f
7
- data.tar.gz: '08f15a5c15f5667d436d8f770d3ccc22561d22272ec998feed7847cc294b6ac21104300a299c9ae0263cac474c39381161169d175704fed08fb35b825695880f'
6
+ metadata.gz: 0b402d7456c859ce34346d9769598a76fc54a30cad1c17dfe2b119020526829b84dde4f0381ac5a3dd58b3d32555d2c162b25ec35f853881083c04a2b48603da
7
+ data.tar.gz: 0d0feaffe572b62933dacfcaa6a445a379fb2e7a4d1a62300d551d4955f8f4ad2be3bb5cf208a3f607dc22a5ea9a8f6af25893ecdd69549e5b1a0bb2a1ae79a8
data/README.md CHANGED
@@ -85,6 +85,26 @@ end
85
85
 
86
86
  По умолчанию, фильтрация не применяется, до тех пор пока не будет передан `scope`.
87
87
 
88
+ ### Коллекции
89
+
90
+ Для кеширования разных коллекций в рамках одной модели воспользуйтесь опцией `collections`, передав в нее массив объектов, содержимое каждого их которых соответствует стандартным опциям; значение ключа кеша укажите в `key`:
91
+
92
+ ```ruby
93
+ class User < ActiveRecord::Base
94
+ common_cache collections: [
95
+ {
96
+ key: 'admin_users',
97
+ attrs: %i[email name],
98
+ scope: proc { where(role: 'admin') }
99
+ },
100
+ {
101
+ key: 'manager_users',
102
+ attrs: [:name],
103
+ scope: proc { where(role: 'manager') }
104
+ }
105
+ ]
106
+ ```
107
+
88
108
  ## Массовое обновление коллекций
89
109
 
90
110
  Для массового обновления всех коллекций воспользутесь Rake таском:
@@ -5,33 +5,40 @@ module CommonDataCaching
5
5
  true
6
6
  end
7
7
 
8
- def common_data_cache_key
9
- "common-data-caching_#{to_s}"
8
+ def common_data_cache_keys
9
+ if cache_opts[:collections]
10
+ cache_opts[:collections].map { |i| "common-data-caching_#{i[:key]}" }
11
+ else
12
+ ["common-data-caching_#{to_s}"]
13
+ end
10
14
  end
11
15
 
12
16
  def common_data_cache_collection
13
- Rails.cache.fetch(common_data_cache_key) || []
17
+ Rails.cache.fetch("common-data-caching_#{to_s}") || []
14
18
  end
15
19
 
16
20
  def update_common_data_cache
17
- scope = cache_opts[:scope] || proc { where('1=1') }
18
- ordering = cache_opts[:order] || :id
19
-
20
- data = merge(scope).order(ordering).prepare_common_data_collection
21
- Rails.cache.write(common_data_cache_key, data)
21
+ if cache_opts[:collections]
22
+ handle_common_data_cache_collections
23
+ else
24
+ handle_common_data_collection(cache_opts)
25
+ end
22
26
  end
23
27
 
24
28
  def update_common_data_cache_versions
25
29
  versions = Rails.cache.read('common-data-caching-versions') || {}
26
- versions[to_s] = Time.now.to_i
27
- Rails.cache.write('common-data-caching-versions', versions)
30
+
31
+ common_data_cache_keys.each do |key|
32
+ versions[key] = Time.now.to_i
33
+ Rails.cache.write('common-data-caching-versions', versions)
34
+ end
28
35
  end
29
36
 
30
- def prepare_common_data_collection
31
- # колонка ID по умолчанию
37
+ def prepare_common_data_collection(attrs)
38
+ # колонка ID - по умолчанию
32
39
  hash = { id: :id }
33
40
 
34
- cache_opts[:attrs].flatten.map do |key|
41
+ attrs.flatten.map do |key|
35
42
  key.is_a?(Hash) ? hash.merge!(key) : hash.merge!(key => key)
36
43
  end
37
44
 
@@ -42,5 +49,22 @@ module CommonDataCaching
42
49
  end
43
50
  end
44
51
 
52
+ private
53
+
54
+ def handle_common_data_cache_collections
55
+ cache_opts[:collections].each do |object|
56
+ handle_common_data_collection(object)
57
+ end
58
+ end
59
+
60
+ def handle_common_data_collection(object)
61
+ scope = object[:scope] || proc { where('1=1') }
62
+ ordering = object[:order] || :id
63
+ data = merge(scope).order(ordering).prepare_common_data_collection(object[:attrs])
64
+
65
+ key = object[:key] ? "common-data-caching_#{object[:key]}" : "common-data-caching_#{to_s}"
66
+ Rails.cache.write(key, data)
67
+ end
68
+
45
69
  end
46
70
  end
@@ -0,0 +1,3 @@
1
+ module CommonDataCaching
2
+ VERSION = '1.4.1'.freeze
3
+ end
@@ -9,11 +9,16 @@ module CommonDataCaching
9
9
  Rails.cache.read('common-data-caching-versions') || {}
10
10
  end
11
11
 
12
+ def self.collection(key)
13
+ Rails.cache.fetch("common-data-caching_#{key}") || []
14
+ end
15
+
12
16
  def self.all_collections
13
- versions.map do |entity, _version|
17
+ versions.map do |key, _version|
18
+ entity = key.sub('common-data-caching_', '')
14
19
  {
15
20
  entity:,
16
- objects: entity.constantize.common_data_cache_collection
21
+ objects: collection(entity)
17
22
  }
18
23
  end
19
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: common-data-caching
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Павел Бабин
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-11 00:00:00.000000000 Z
11
+ date: 2025-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,7 @@ files:
38
38
  - lib/common_data_caching/capistrano/tasks/common_data_caching.rake
39
39
  - lib/common_data_caching/class_methods.rb
40
40
  - lib/common_data_caching/rake_tasks.rb
41
+ - lib/common_data_caching/version.rb
41
42
  homepage:
42
43
  licenses:
43
44
  - MIT