common-data-caching 1.2.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 +4 -4
- data/README.md +28 -0
- data/lib/common_data_caching/callbacks.rb +4 -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 +13 -3
- 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: '006960c18425203cd14453b7397ce61e86e36bd5ae9ad2fdaddf5134d32d57c4'
|
|
4
|
+
data.tar.gz: 6b5cbc02aea17efeb154c9e16fbdf8a34a02198b42267ae6d13df80a48c7cb90
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6ac2ddd35958107eed0573f8824a9a1e84947dd22f4f3877dcf9465a7880f02265736e0982dff057928e78d406847a75bfdfd0003360407be7a5e3ad33f4e108
|
|
7
|
+
data.tar.gz: 6d2936545f2cc3157fb5a007f0bcd9909367c29dd7c0c90323530ce6ba9e2ad6bf3cdd5dbb9a7089c02d457308cfd8604b8695abb806d550a34f4f643b2196d2
|
data/README.md
CHANGED
|
@@ -47,6 +47,14 @@ User.common_data_cache_collection
|
|
|
47
47
|
CommonDataCaching.versions
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
При необходимости, чтобы не обновлять кеш при создании или редактировании определенной записи, передайте `skip_common_data_caching: true` в качестве аргумента:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
User.create(email: "user@example.com", skip_common_data_caching: true)
|
|
54
|
+
|
|
55
|
+
User.find(1).update(name: "User", skip_common_data_caching: true)
|
|
56
|
+
```
|
|
57
|
+
|
|
50
58
|
### Сортировка
|
|
51
59
|
|
|
52
60
|
Для применения сортировки, передайте опцию `order`:
|
|
@@ -77,6 +85,26 @@ end
|
|
|
77
85
|
|
|
78
86
|
По умолчанию, фильтрация не применяется, до тех пор пока не будет передан `scope`.
|
|
79
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
|
+
|
|
80
108
|
## Массовое обновление коллекций
|
|
81
109
|
|
|
82
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,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
|
{
|
|
@@ -58,10 +62,16 @@ ActiveSupport.on_load(:active_record) do
|
|
|
58
62
|
# методы кеширования
|
|
59
63
|
extend CommonDataCaching::ClassMethods
|
|
60
64
|
|
|
61
|
-
#
|
|
65
|
+
# коллбэки
|
|
62
66
|
include CommonDataCaching::Callbacks
|
|
63
|
-
|
|
64
|
-
after_commit :
|
|
67
|
+
|
|
68
|
+
after_commit :update_common_data_cache,
|
|
69
|
+
on: %i[create update destroy],
|
|
70
|
+
if: proc { |i| i.common_data_caching? && i.skip_common_data_caching != true }
|
|
71
|
+
|
|
72
|
+
after_commit :update_common_data_cache_versions,
|
|
73
|
+
on: %i[create update destroy],
|
|
74
|
+
if: proc { |i| i.common_data_caching? && i.skip_common_data_caching != true }
|
|
65
75
|
end
|
|
66
76
|
end
|
|
67
77
|
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.0
|
|
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-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
|