redisize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/TODO +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redisize/adapter/inline.rb +11 -0
- data/lib/redisize/adapter/resque.rb +21 -0
- data/lib/redisize/adapter/sidekiq.rb +20 -0
- data/lib/redisize/cache/rails.rb +21 -0
- data/lib/redisize/log/rails.rb +17 -0
- data/lib/redisize/orm/active_record.rb +13 -0
- data/lib/redisize/version.rb +3 -0
- data/lib/redisize.rb +307 -0
- data/redisize.gemspec +30 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4dd240d50c272014c6d147455c340d73dee4cba93015ce7f37a7f43af920e44b
|
4
|
+
data.tar.gz: 604bf60003f3e2ca84037a70d46769c3e791bc56a108b6e0190a5442a00edc88
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f69e2bbf3956f302194e1d3cda8afe59e9408e7823f7fbe5eee755a2c8fb3c78cf5a7edf66a7292181226e2dccde925eacf5c501475469387cf7f7d30ab709fd
|
7
|
+
data.tar.gz: 99a168322bc6fbe43a8f920f9310ec11bae7f57cd7fdf128024cfcd9be0547cf075c9f730558c8cdd0fb8b16426b937b54734a581e3d21195114f91bfe1e4aa0
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at majioa@yandex.ru. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Pavel «Malo» Skrylev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Redisize
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/redisize`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'redisize'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install redisize
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redisize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/redisize/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the Redisize project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/redisize/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/TODO
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redisize"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Redisize
|
2
|
+
module Adapter
|
3
|
+
class Resque
|
4
|
+
@queue = :caching
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def enqueue *args
|
8
|
+
::Resque.enqueue(self, *args)
|
9
|
+
end
|
10
|
+
|
11
|
+
def lock_workers _method, *_args
|
12
|
+
@queue
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform method, *args
|
16
|
+
Redisize.send(method, *args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Redisize
|
2
|
+
module Adapter
|
3
|
+
class Sidekiq
|
4
|
+
include ::Sidekiq::Worker
|
5
|
+
sidekiq_options queue: 'caching'
|
6
|
+
sidekiq_options limits: { caching: 1 }
|
7
|
+
sidekiq_options process_limits: { caching: 1 }
|
8
|
+
|
9
|
+
def perform method, *args
|
10
|
+
Redisize.send(method, *args)
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def enqueue *args
|
15
|
+
self.perform_async(*args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Redisize
|
2
|
+
module Cache
|
3
|
+
module Rails
|
4
|
+
def redisize_cache_write key, object, options = {}
|
5
|
+
::Rails.cache.write(key, object, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
def redisize_cache_read key
|
9
|
+
::Rails.cache.read(key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def redisize_cache_fetch key, options = {}, &block
|
13
|
+
::Rails.cache.fetch(key, options, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def redisize_cache_delete key
|
17
|
+
::Rails.cache.delete(key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Redisize
|
2
|
+
module Log
|
3
|
+
module Rails
|
4
|
+
def redisize_log_info *args
|
5
|
+
::Rails.logger.info(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
def redisize_log_debug *args
|
9
|
+
::Rails.logger.debug(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def redisize_log_error *args
|
13
|
+
::Rails.logger.error(*args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/redisize.rb
ADDED
@@ -0,0 +1,307 @@
|
|
1
|
+
require "redisize/version"
|
2
|
+
|
3
|
+
module Redisize
|
4
|
+
class << self
|
5
|
+
attr_writer :adapter
|
6
|
+
|
7
|
+
ADAPTERS = {
|
8
|
+
sidekiq: :Sidekiq, # req: Sidekiq::LimitFetch
|
9
|
+
resque: :Resque,
|
10
|
+
inline: :TrueClass
|
11
|
+
}
|
12
|
+
|
13
|
+
ORMS = {
|
14
|
+
ActiveRecord: 'active_record'
|
15
|
+
}
|
16
|
+
|
17
|
+
LOGS = {
|
18
|
+
Rails: 'rails'
|
19
|
+
}
|
20
|
+
|
21
|
+
CACHES = {
|
22
|
+
Rails: 'rails'
|
23
|
+
}
|
24
|
+
|
25
|
+
# system method
|
26
|
+
def included kls
|
27
|
+
kls.extend(Redisize::ClassMethods)
|
28
|
+
|
29
|
+
# init orm, log, and cache
|
30
|
+
init_orm_for(kls)
|
31
|
+
init_log_for(kls)
|
32
|
+
init_cache_for(kls)
|
33
|
+
end
|
34
|
+
|
35
|
+
def init_orm_for kls
|
36
|
+
orm = kls.ancestors.reduce(nil) { |res, anc| res || ORMS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
37
|
+
|
38
|
+
if orm
|
39
|
+
require("redisize/orm/#{ORMS[orm]}")
|
40
|
+
kls.include(Redisize::ORM.const_get(orm))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def init_log_for kls
|
45
|
+
log = Object.constants.reduce(nil) { |res, anc| res || LOGS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
46
|
+
|
47
|
+
if log
|
48
|
+
require("redisize/log/#{LOGS[log]}")
|
49
|
+
Redisize.include(Redisize::Log.const_get(log))
|
50
|
+
Redisize.extend(Redisize::Log.const_get(log))
|
51
|
+
kls.extend(Redisize::Log.const_get(log))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def init_cache_for kls
|
56
|
+
cache = Object.constants.reduce(nil) { |res, anc| res || CACHES.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
57
|
+
|
58
|
+
if cache
|
59
|
+
require("redisize/cache/#{CACHES[cache]}")
|
60
|
+
Redisize.include(Redisize::Cache.const_get(cache))
|
61
|
+
Redisize.extend(Redisize::Cache.const_get(cache))
|
62
|
+
kls.extend(Redisize::Cache.const_get(cache))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def adapter_kind= value
|
67
|
+
self.adapter = acquire_adapter(ADAPTERS[value] && value || :inline)
|
68
|
+
end
|
69
|
+
|
70
|
+
def acquire_adapter kind
|
71
|
+
require("redisize/adapter/#{kind}")
|
72
|
+
Redisize::Adapter.const_get(kind.capitalize)
|
73
|
+
end
|
74
|
+
|
75
|
+
def adapter
|
76
|
+
@adapter ||=
|
77
|
+
ADAPTERS.reduce(nil) do |res, (kind, prc)|
|
78
|
+
res || Object.constants.include?(prc) && acquire_adapter(kind) || nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def enqueue method, *args
|
83
|
+
adapter.enqueue(method, *args)
|
84
|
+
end
|
85
|
+
|
86
|
+
def filtered_for attrs, klass
|
87
|
+
filtered = attrs.select {|attr| klass.attribute_types.keys.include?(attr) }
|
88
|
+
end
|
89
|
+
|
90
|
+
def assign_reverse_key key, host_key
|
91
|
+
# binding.pry
|
92
|
+
if value = redisize_cache_read(key)
|
93
|
+
redisize_cache_write(key, value | [ host_key ], expires_in: 1.day)
|
94
|
+
redisize_log_debug("Updated key #{key.inspect} with value #{host_key.inspect}")
|
95
|
+
else
|
96
|
+
redisize_cache_write(key, [ host_key ], expires_in: 1.day)
|
97
|
+
redisize_log_debug("Created key #{key.inspect} with value #{host_key.inspect}")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def drop_key key
|
102
|
+
# binding.pry
|
103
|
+
if value = redisize_cache_read(key)
|
104
|
+
if key.first == "meta"
|
105
|
+
size = value.size
|
106
|
+
|
107
|
+
value.each { |rkey| drop_key(rkey) }
|
108
|
+
end
|
109
|
+
|
110
|
+
redisize_log_debug("Removed key #{key.inspect}#{size && " with #{size} subkeys"}")
|
111
|
+
redisize_cache_delete(key)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def rekey key, type = "meta"
|
116
|
+
[type.to_s] + key[1..-1]
|
117
|
+
end
|
118
|
+
|
119
|
+
def key_name_for model_name, attrs, type = "meta"
|
120
|
+
primary_key = model_name.constantize.primary_key
|
121
|
+
[type, model_name, primary_key, attrs[primary_key].to_s]
|
122
|
+
end
|
123
|
+
|
124
|
+
def parse_sql_key key
|
125
|
+
key[-1].split(/\s(join|from)\s/i)[1..-1].map do |part|
|
126
|
+
part.strip.split(/[\s\"]/).reject {|x| x.blank? }.first
|
127
|
+
end.uniq.map do |table|
|
128
|
+
table.singularize.camelize.constantize rescue nil
|
129
|
+
end.compact.each do |klass|
|
130
|
+
assign_reverse_key(["meta", klass.name], key)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def parse_instance_attrs model_name, attrs, key
|
135
|
+
model = model_name.constantize
|
136
|
+
children =
|
137
|
+
attrs.map do |x, value|
|
138
|
+
(value.is_a?(Array) || value.is_a?(Hash)) && x || nil
|
139
|
+
end.compact
|
140
|
+
|
141
|
+
children.each do |many|
|
142
|
+
name = /^_(?<_name>.*)/ =~ many && _name || many.to_s
|
143
|
+
|
144
|
+
instance = nil
|
145
|
+
if klass = model.reflections[name]&.klass || many.singularize.camelize.constantize rescue nil
|
146
|
+
attres_in = attrs[many]
|
147
|
+
attres = attres_in.is_a?(Hash) && [attres_in] || attres_in
|
148
|
+
attres.each do |attrs|
|
149
|
+
if attrs[klass.primary_key]
|
150
|
+
assign_reverse_key(key_name_for(klass.name, attrs), key)
|
151
|
+
else
|
152
|
+
assign_reverse_key(["meta", klass.name], key)
|
153
|
+
end
|
154
|
+
parse_instance_attrs(klass.name, attrs, key)
|
155
|
+
end
|
156
|
+
|
157
|
+
assign_reverse_key(["meta", klass.name], key)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def as_json_for instance
|
163
|
+
instance.attribute_names.map {|x|[x, instance.read_attribute(x)] }.to_h
|
164
|
+
end
|
165
|
+
|
166
|
+
### internal methods for enqueued proceeds
|
167
|
+
#
|
168
|
+
def redisize_model_metas metakey, model_name, attrs, key
|
169
|
+
# binding.pry
|
170
|
+
drop_key(metakey)
|
171
|
+
drop_key(metakey[0..1])
|
172
|
+
parse_instance_attrs(model_name, attrs, key)
|
173
|
+
assign_reverse_key(metakey, key)
|
174
|
+
end
|
175
|
+
|
176
|
+
# +redisize_sql_metas+ updates all the meta keys for the result value
|
177
|
+
#
|
178
|
+
def redisize_sql_metas key, attres
|
179
|
+
model_name = key[1]
|
180
|
+
primary_key = key[2]
|
181
|
+
|
182
|
+
# binding.pry
|
183
|
+
attres.map do |attrs|
|
184
|
+
metakey = ["meta", model_name, primary_key, attrs[primary_key]]
|
185
|
+
|
186
|
+
parse_instance_attrs(model_name, attrs, key)
|
187
|
+
assign_reverse_key(metakey, key)
|
188
|
+
end
|
189
|
+
|
190
|
+
parse_sql_key(key)
|
191
|
+
end
|
192
|
+
|
193
|
+
def deredisize_instance_metas key
|
194
|
+
metakey = rekey(key)
|
195
|
+
|
196
|
+
# binding.pry
|
197
|
+
drop_key(metakey)
|
198
|
+
redisize_cache_delete(key)
|
199
|
+
end
|
200
|
+
|
201
|
+
def reredisize_instance_metas key
|
202
|
+
metakey = rekey(key)
|
203
|
+
# binding.pry
|
204
|
+
|
205
|
+
drop_key(metakey)
|
206
|
+
assign_reverse_key(metakey, key)
|
207
|
+
end
|
208
|
+
|
209
|
+
def deredisize_model_metas model_name
|
210
|
+
# binding.pry
|
211
|
+
drop_key(["meta", model_name])
|
212
|
+
end
|
213
|
+
|
214
|
+
def deredisize_json_metas key
|
215
|
+
# binding.pry
|
216
|
+
drop_key(key)
|
217
|
+
end
|
218
|
+
|
219
|
+
def redisize_json_metas key, attrs
|
220
|
+
metakey = key_name_for(key[1], attrs)
|
221
|
+
|
222
|
+
# binding.pry
|
223
|
+
parse_instance_attrs(key[1], attrs, key)
|
224
|
+
assign_reverse_key(metakey, key)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# self -> model instance
|
229
|
+
def redisize_json scheme, &block
|
230
|
+
primary_key = self.class.primary_key
|
231
|
+
key = ["json", self.class.polymorphic_name, primary_key, self[primary_key].to_s, scheme]
|
232
|
+
|
233
|
+
# binding.pry
|
234
|
+
redisize_cache_fetch(key, expires_in: 1.week) do
|
235
|
+
value = block.call
|
236
|
+
|
237
|
+
Redisize.enqueue(:redisize_json_metas, key, value)
|
238
|
+
|
239
|
+
value
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
# self -> model instance
|
244
|
+
def deredisize_json scheme, &block
|
245
|
+
primary_key = self.class.primary_key
|
246
|
+
key = ["json", self.class.polymorphic_name, primary_key, self[primary_key], scheme]
|
247
|
+
|
248
|
+
# binding.pry
|
249
|
+
Redisize.enqueue(:deredisize_json_metas, key)
|
250
|
+
end
|
251
|
+
|
252
|
+
# self -> model instance
|
253
|
+
def deredisize_model
|
254
|
+
Redisize.enqueue(:deredisize_model_metas, self.class.polymorphic_name)
|
255
|
+
end
|
256
|
+
|
257
|
+
# self -> model instance
|
258
|
+
def reredisize_instance
|
259
|
+
attrs = Redisize.as_json_for(self)
|
260
|
+
key = Redisize.key_name_for(self.class.polymorphic_name, attrs, "instance")
|
261
|
+
|
262
|
+
# binding.pry
|
263
|
+
redisize_cache_write(key, self, expires_in: 1000.years)
|
264
|
+
Redisize.enqueue(:reredisize_instance_metas, key)
|
265
|
+
end
|
266
|
+
|
267
|
+
# self -> model instance
|
268
|
+
def deredisize_instance
|
269
|
+
attrs = Redisize.as_json_for(self)
|
270
|
+
key = Redisize.key_name_for(self.class.polymorphic_name, attrs, "instance")
|
271
|
+
|
272
|
+
# binding.pry
|
273
|
+
Redisize.enqueue(:deredisize_instance_metas, key)
|
274
|
+
end
|
275
|
+
|
276
|
+
module ClassMethods
|
277
|
+
# self -> model class
|
278
|
+
def redisize_sql &block
|
279
|
+
key = ["sql", self.name, self.primary_key, self.all.to_sql]
|
280
|
+
|
281
|
+
# binding.pry
|
282
|
+
redisize_cache_fetch(key, expires_in: 1.day) do
|
283
|
+
value = block.call
|
284
|
+
|
285
|
+
Redisize.enqueue(:redisize_sql_metas, key, value)
|
286
|
+
|
287
|
+
value
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
# self -> model class
|
292
|
+
def redisize_model value, options = {}, &block
|
293
|
+
primary_key = options.fetch(:by_key, self.primary_key).to_s
|
294
|
+
key = ["instance", name, primary_key, value]
|
295
|
+
metakey = ["meta", self.class.polymorphic_name, primary_key, value]
|
296
|
+
|
297
|
+
# binding.pry
|
298
|
+
redisize_cache_fetch(key, expires_in: 1.week) do
|
299
|
+
if result = block.call
|
300
|
+
Redisize.enqueue(:redisize_model_metas, metakey, self.name, Redisize.as_json_for(result), key)
|
301
|
+
end
|
302
|
+
|
303
|
+
result
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
data/redisize.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'lib/redisize/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "redisize"
|
5
|
+
spec.version = Redisize::VERSION
|
6
|
+
spec.authors = ["Pavel «Malo» Skrylev"]
|
7
|
+
spec.email = ["majioa@yandex.ru"]
|
8
|
+
|
9
|
+
spec.summary = %q{Make json record cacheable to redis}
|
10
|
+
spec.description = %q{Make json record cacheable to redis via various adapters like Resque, Sidekiq, etc}
|
11
|
+
spec.homepage = "https://github.com/majioa/redisize"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/majioa/redisize"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/majioa/redisize/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
spec.add_development_dependency('pry')
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redisize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel «Malo» Skrylev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Make json record cacheable to redis via various adapters like Resque,
|
28
|
+
Sidekiq, etc
|
29
|
+
email:
|
30
|
+
- majioa@yandex.ru
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- CODE_OF_CONDUCT.md
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- TODO
|
42
|
+
- bin/console
|
43
|
+
- bin/setup
|
44
|
+
- lib/redisize.rb
|
45
|
+
- lib/redisize/adapter/inline.rb
|
46
|
+
- lib/redisize/adapter/resque.rb
|
47
|
+
- lib/redisize/adapter/sidekiq.rb
|
48
|
+
- lib/redisize/cache/rails.rb
|
49
|
+
- lib/redisize/log/rails.rb
|
50
|
+
- lib/redisize/orm/active_record.rb
|
51
|
+
- lib/redisize/version.rb
|
52
|
+
- redisize.gemspec
|
53
|
+
homepage: https://github.com/majioa/redisize
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
allowed_push_host: https://rubygems.org
|
58
|
+
homepage_uri: https://github.com/majioa/redisize
|
59
|
+
source_code_uri: https://github.com/majioa/redisize
|
60
|
+
changelog_uri: https://github.com/majioa/redisize/CHANGELOG.md
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.3.0
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.1.6
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Make json record cacheable to redis
|
80
|
+
test_files: []
|