common-data-caching 1.3.0 → 1.4.0

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: '006960c18425203cd14453b7397ce61e86e36bd5ae9ad2fdaddf5134d32d57c4'
4
+ data.tar.gz: 6b5cbc02aea17efeb154c9e16fbdf8a34a02198b42267ae6d13df80a48c7cb90
5
5
  SHA512:
6
- metadata.gz: 106b64506c7d3ca88b2e5b1caae0e2ab4025475ee07f574983e5d66c0c8fc329e4e8ef1adf0e3d15f15f0bb87b232b836a2f4e4880b6ccb5d858388d0a29357f
7
- data.tar.gz: '08f15a5c15f5667d436d8f770d3ccc22561d22272ec998feed7847cc294b6ac21104300a299c9ae0263cac474c39381161169d175704fed08fb35b825695880f'
6
+ metadata.gz: 6ac2ddd35958107eed0573f8824a9a1e84947dd22f4f3877dcf9465a7880f02265736e0982dff057928e78d406847a75bfdfd0003360407be7a5e3ad33f4e108
7
+ data.tar.gz: 6d2936545f2cc3157fb5a007f0bcd9909367c29dd7c0c90323530ce6ba9e2ad6bf3cdd5dbb9a7089c02d457308cfd8604b8695abb806d550a34f4f643b2196d2
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.0'.freeze
3
+ end
@@ -9,6 +9,10 @@ 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
17
  versions.map do |entity, _version|
14
18
  {
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.0
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-05 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