redisize 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b7b65e687f5003dac2ba7e92d42450cea066554ce88bccb49c210ff0a413d41
4
- data.tar.gz: 95e775070c058d4f3804d03bb082969146c6e5402aec915d489a11eca262214e
3
+ metadata.gz: 71df00d9c001e09d06c90fc75ea318e50330dd6cebf2d804b0fd91ed329bb11b
4
+ data.tar.gz: caa72c28e9750012bccd1fc59caedfd27b5eb10bba75c38ba1f325e7e1527c94
5
5
  SHA512:
6
- metadata.gz: b26a2aa8e95fd8de86db627fb45207af2bbebaaaf5fc468b2b4ed8d2f9675a83f8e4852c8d1adbd1150a03bd4dd00a588d2c67c9b5a3ed30f30ddb7f05544c72
7
- data.tar.gz: b2493b59261c6c0cbe9962c6dd7e97b635de114695d77373c56608a723b78d9f2e655d59605c2788b4ca978f0e2161b0177c3db7c3c6673126af95dd5e9f1d19
6
+ metadata.gz: 17a578a95fdeb9a8c70fbea3d73c0642fa04ea37de713b8f386ad9b26e2ebd5802e2d66430f5e1698a16f227467bdf01abc6d7fd2cdf1b2479e3fadfd8acc515
7
+ data.tar.gz: fddd7a97b499514ac78a21168dfb5af6e21b10d5f28cdcc3001ec081812b565670fb8571e3a358e8759dae7bc929ab72825f16b437bae6398b8bfcb85ab3bc8f
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Redisize
2
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
3
+ The gem allows to use asynchonous way to cache or just store in redis or any other cache mechanism. For asynchronous caching the Resque or Sidekiq adapters can be used, for synchronous - inline.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,54 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Initialization
24
+ Usually adapter can'be defined automatically of presently installed gems. Curretly are supported sidekiq and resque gems to ansyncronous caching (redisizing). If no such gems are installed, the synchronous **inline** adapter is used. You can redefine manually an adapter as follows:
25
+
26
+ ```ruby
27
+ Redisize.adapter_kind = :inline
28
+ ```
29
+
30
+ Other values are ```:resque```, and ```:sidekiq```.
31
+ And then to use the gem just define in the target object:
32
+
33
+ ```ruby
34
+ include(Redisize)
35
+ ```
36
+
37
+ ### In Rails
38
+
39
+ To use the cache feature with Rails (and ActiveRecord) you have just to wrap a method accessing DB either a record or a relation to a block like this. So to redisize a record value as a json use the follwing:
40
+
41
+ ```ruby
42
+ redisize_json(attrs) do
43
+ # <JSON generation code>
44
+ # generate_json(attrs, options)
45
+ end
46
+ ```
47
+
48
+ to drop a JSON value use:
49
+
50
+ ```ruby
51
+ deredisize_json(attrs)
52
+ ```
53
+
54
+ to redisize an SQL:
55
+
56
+ ```ruby
57
+ redisize_sql do
58
+ relation.as_json(context)
59
+ end
60
+ ```
61
+
62
+ to redisize an record instance use:
63
+
64
+ ```ruby
65
+ redisize_model(slug, by_key: :slug) do
66
+ self.joins(:slug).where(slugs: {text: slug}).first
67
+ end
68
+ ```
69
+
70
+ Next calls to the block will return a cached value. Updating the record will drop cache for sql or record itself.
26
71
 
27
72
  ## Development
28
73
 
@@ -32,7 +77,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
77
 
33
78
  ## Contributing
34
79
 
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).
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/majioa/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/majioa/redisize/blob/master/CODE_OF_CONDUCT.md).
36
81
 
37
82
 
38
83
  ## License
@@ -41,4 +86,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
41
86
 
42
87
  ## Code of Conduct
43
88
 
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).
89
+ 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/majioa/redisize/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,20 @@
1
+ module Redisize
2
+ module Support
3
+ module Enumerable
4
+ def self.included kls
5
+ kls.class_eval do
6
+ def redisize
7
+ case self
8
+ when Hash
9
+ redisize_hash { map { |x| x.jsonize } }
10
+ when Array
11
+ redisize_array { map { |x| x.jsonize } }
12
+ else
13
+ x.jsonize
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Redisize
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/redisize.rb CHANGED
@@ -16,6 +16,9 @@ module Redisize
16
16
  module ORM
17
17
  autoload :ActiveRecord, File.join(__dir__, 'redisize/orm/active_record.rb')
18
18
  end
19
+ module Support
20
+ autoload :Enumerable, File.join(__dir__, 'redisize/support/enumerable.rb')
21
+ end
19
22
 
20
23
  class << self
21
24
  attr_writer :adapter
@@ -30,6 +33,10 @@ module Redisize
30
33
  ActiveRecord: 'active_record'
31
34
  }
32
35
 
36
+ SUPPORTS = {
37
+ Enumerable: 'enumerable'
38
+ }
39
+
33
40
  LOGS = {
34
41
  Rails: 'rails'
35
42
  }
@@ -42,10 +49,11 @@ module Redisize
42
49
  def included kls
43
50
  kls.extend(Redisize::ClassMethods)
44
51
 
45
- # init orm, log, and cache
52
+ # init orm, log, cache, and support classes
46
53
  init_orm_for(kls)
47
54
  init_log_for(kls)
48
55
  init_cache_for(kls)
56
+ init_support_for(kls)
49
57
  end
50
58
 
51
59
  def init_orm_for kls
@@ -57,6 +65,17 @@ module Redisize
57
65
  end
58
66
  end
59
67
 
68
+ def init_support_for kls
69
+ sup = Object.constants.reduce(nil) { |res, anc| res || SUPPORTS.keys.find {|re| /#{re}/ =~ anc.to_s } }
70
+
71
+ if sup
72
+ require("redisize/support/#{SUPPORTS[sup]}")
73
+ cls = sup.to_s.constantize
74
+ cls.include(Redisize::Support.const_get(sup))
75
+ cls.include(Redisize::ClassMethods)
76
+ end
77
+ end
78
+
60
79
  def init_log_for kls
61
80
  log = Object.constants.reduce(nil) { |res, anc| res || LOGS.keys.find {|re| /#{re}/ =~ anc.to_s } }
62
81
 
@@ -75,6 +94,7 @@ module Redisize
75
94
  require("redisize/cache/#{CACHES[cache]}")
76
95
  Redisize.include(Redisize::Cache.const_get(cache))
77
96
  Redisize.extend(Redisize::Cache.const_get(cache))
97
+ Enumerable.include(Redisize::Cache.const_get(cache))
78
98
  kls.extend(Redisize::Cache.const_get(cache))
79
99
  end
80
100
  end
@@ -175,10 +195,6 @@ module Redisize
175
195
  end
176
196
  end
177
197
 
178
- def as_json_for instance
179
- instance.attribute_names.map {|x|[x, instance.read_attribute(x)] }.to_h
180
- end
181
-
182
198
  ### internal methods for enqueued proceeds
183
199
  #
184
200
  def redisize_model_metas metakey, model_name, attrs, key
@@ -189,9 +205,9 @@ module Redisize
189
205
  assign_reverse_key(metakey, key)
190
206
  end
191
207
 
192
- # +redisize_sql_metas+ updates all the meta keys for the result value
208
+ # +redisize_array_metas+ updates all the meta keys for the result array value
193
209
  #
194
- def redisize_sql_metas key, attres
210
+ def redisize_array_metas key, attres
195
211
  model_name = key[1]
196
212
  primary_key = key[2]
197
213
 
@@ -272,7 +288,7 @@ module Redisize
272
288
 
273
289
  # self -> model instance
274
290
  def reredisize_instance
275
- attrs = Redisize.as_json_for(self)
291
+ attrs = self.as_json
276
292
  key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
277
293
 
278
294
  # binding.pry
@@ -282,7 +298,7 @@ module Redisize
282
298
 
283
299
  # self -> model instance
284
300
  def deredisize_instance
285
- attrs = Redisize.as_json_for(self)
301
+ attrs = self.as_json
286
302
  key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
287
303
 
288
304
  # binding.pry
@@ -300,15 +316,48 @@ module Redisize
300
316
  end
301
317
  end
302
318
 
319
+ def primary_key_with key_name_in = nil
320
+ key_name_in || self.respond_to?(:primary_key) && self.primary_key || case self
321
+ when ActiveRecord
322
+ self.map.first&.class&.primary_key
323
+ when ActiveRecord::Scoping, ActiveRecord::Reflection, ActiveRecord::Associations
324
+ self.values.map.first&.class&.primary_key
325
+ end
326
+ end
327
+
303
328
  # self -> model class
304
329
  def redisize_sql &block
305
330
  key = ["sql", self.name, self.primary_key, self.all.to_sql]
306
331
 
332
+ redisize_by_key(key, &block)
333
+ end
334
+
335
+ # self -> model class
336
+ def redisize_hash key_name_in = nil, &block
337
+ key_name = primary_key_with(key_name_in)
338
+
339
+ key = ["by_key", *self.values.map do |x|
340
+ key_name && x.respond_to?(key_name) ? x.send(key_name).to_s : x.to_s
341
+ end ]
342
+
343
+ redisize_by_key(key, &block)
344
+ end
345
+
346
+ # self -> model class
347
+ def redisize_array key_name_in = nil, &block
348
+ key_name = primary_key_with(key_name_in)
349
+
350
+ key = ["by_key", *self.map { |x| key_name ? x.send(key_name).to_s : x.to_s } ]
351
+
352
+ redisize_by_key(key, &block)
353
+ end
354
+
355
+ def redisize_by_key key, &block
307
356
  # binding.pry
308
357
  redisize_cache_fetch(key, expires_in: 1.day) do
309
358
  value = block.call
310
359
 
311
- Redisize.enqueue(:redisize_sql_metas, key, value)
360
+ Redisize.enqueue(:redisize_array_metas, key, value)
312
361
 
313
362
  value
314
363
  end
@@ -318,12 +367,12 @@ module Redisize
318
367
  def redisize_model value, options = {}, &block
319
368
  primary_key = options.fetch(:by_key, self.primary_key).to_s
320
369
  key = ["instance", name, primary_key, value]
321
- metakey = ["meta", self.class.polymorphic_base_name, primary_key, value]
370
+ metakey = ["meta", self.polymorphic_base_name, primary_key, value]
322
371
 
323
372
  # binding.pry
324
373
  redisize_cache_fetch(key, expires_in: 1.week) do
325
374
  if result = block.call
326
- Redisize.enqueue(:redisize_model_metas, metakey, self.name, Redisize.as_json_for(result), key)
375
+ Redisize.enqueue(:redisize_model_metas, metakey, self.name, result.as_json, key)
327
376
  end
328
377
 
329
378
  result
data/redisize.gemspec CHANGED
@@ -8,15 +8,15 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = %q{Make json record cacheable to redis}
10
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"
11
+ spec.homepage = "https://codeberg.org/majioa/redisize"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
15
15
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
16
 
17
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"
18
+ spec.metadata["source_code_uri"] = "https://codeberg.org/majioa/redisize"
19
+ spec.metadata["changelog_uri"] = "https://codeberg.org/majioa/redisize/CHANGELOG.md"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redisize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel «Malo» Skrylev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-19 00:00:00.000000000 Z
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -48,16 +48,17 @@ files:
48
48
  - lib/redisize/cache/rails.rb
49
49
  - lib/redisize/log/rails.rb
50
50
  - lib/redisize/orm/active_record.rb
51
+ - lib/redisize/support/enumerable.rb
51
52
  - lib/redisize/version.rb
52
53
  - redisize.gemspec
53
- homepage: https://github.com/majioa/redisize
54
+ homepage: https://codeberg.org/majioa/redisize
54
55
  licenses:
55
56
  - MIT
56
57
  metadata:
57
58
  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
59
+ homepage_uri: https://codeberg.org/majioa/redisize
60
+ source_code_uri: https://codeberg.org/majioa/redisize
61
+ changelog_uri: https://codeberg.org/majioa/redisize/CHANGELOG.md
61
62
  post_install_message:
62
63
  rdoc_options: []
63
64
  require_paths:
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.1.6
77
+ rubygems_version: 3.3.26
77
78
  signing_key:
78
79
  specification_version: 4
79
80
  summary: Make json record cacheable to redis