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 +4 -4
- data/README.md +20 -0
- data/lib/common_data_caching/class_methods.rb +37 -13
- data/lib/common_data_caching/version.rb +3 -0
- data/lib/common_data_caching.rb +7 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56012b8561fa7985f849ba78ba4e4353a4c140df1ff237aaa7a7463af9efe14a
|
4
|
+
data.tar.gz: aeb5ecfffd8c492de47a8ea5abc141fec20c1605873132e2cb07135a24f32c37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
9
|
-
|
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(
|
17
|
+
Rails.cache.fetch("common-data-caching_#{to_s}") || []
|
14
18
|
end
|
15
19
|
|
16
20
|
def update_common_data_cache
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
27
|
-
|
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
|
-
|
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
|
data/lib/common_data_caching.rb
CHANGED
@@ -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 |
|
17
|
+
versions.map do |key, _version|
|
18
|
+
entity = key.sub('common-data-caching_', '')
|
14
19
|
{
|
15
20
|
entity:,
|
16
|
-
objects: entity
|
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.
|
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:
|
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
|