rails_cached_method 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adb70332fd66d09b43164dc071a109c780f10008ea6a1aa83b97b46196452c5e
4
- data.tar.gz: 0d2ecc3d2b86796d1e9da3a724008e8c419d81ddf30c820f5a169d01b48d05ab
3
+ metadata.gz: 8309a61a67d126ce96170e4ca5e0aa048b4234150afdd5e69e7510f9503822d7
4
+ data.tar.gz: 0dac1785aeb5560f12fef7dbe1364c4a595c1f1fd09fb6917dffe0fd651e78cf
5
5
  SHA512:
6
- metadata.gz: 3396b9439c314d57957aed51aa90b267ad9fdbecce51baca3c4160814e099d68d6aa73b9ab1ea6e43a42ad61f023ff5809552f5ad27e44d2d38b4be01fc75332
7
- data.tar.gz: df6cb825167aad571a4168b947e1515f9fa11f1adbbc8f21aecec6b0898137146237616d2ece74c97316c1c957dffde25fe290200c3061269ada6a504ffc466f
6
+ metadata.gz: 60e9b34da18925376a03d791cc935f1393fdc889e3fe34da4a52f4fc270d1476e728064db19e5ed00ef7b38fe74ee92e6931be6b05c02ec6079ccf8bae3db4f6
7
+ data.tar.gz: 2318fa0d270d2ef9611040a22055c90b4e492e78a4c65add5ec0dceb0e81b1218bf5200bda2280dc5614ce63ae794001e0d494494ed73b7be5e3b909d41c5e81
data/README.md CHANGED
@@ -2,16 +2,17 @@
2
2
 
3
3
  Simple way to cache results of methods. Using `Rails.cache` inside with default expiration time of 1 minute.
4
4
 
5
+ ![Demo](docs/rails_cached_method.png)
5
6
 
6
7
  ## Usage
7
8
 
8
9
  To get a value and cache it just write a code like:
9
10
 
10
11
  ```ruby
11
- User.cached.admins
12
- post.cached.comments.last
13
- User.cached.where(role: :moderator).count
14
- Project.cached.first.some_heavey_method
12
+ User.cached.admins # get list of admins and automatically put them in cache with 1 minute TTL
13
+ post.cached.comments.last # get last comment from the post and cache it
14
+ User.cached.where(role: :moderator).count # cache moderators count
15
+ Project.cached.first.some_heavey_method # cache result of some_heavey_method for first Project (if first is Project.first)
15
16
  ```
16
17
 
17
18
  So basically just call method `cached` on any object. It will wrap and cache **every** result from the next method call. So every result from method chain is cached.
@@ -37,6 +38,58 @@ key - you can set your key.
37
38
  expires_in - TTL of cached object.
38
39
  recache - should be key deleted and cached again.
39
40
 
41
+ ## How it works
42
+
43
+ After you call `.cached` it will generate a cache key and store value in Rails.cache. Value is class which is wrapps original value and allow you to call methods on the value using `method_missing`. Every result is cached again and again. So on every method call in methods chain every result will be stored in Rails.cache.
44
+
45
+ To get original value you can call `.__value__`.
46
+
47
+ Some pieces of code to show you how it works:
48
+
49
+ ```ruby
50
+ def cached(key: nil, expires_in: 1.minute, recache: false)
51
+ CachedProxy.new(
52
+ self,
53
+ key: key,
54
+ expires_in: expires_in,
55
+ recache: recache
56
+ )
57
+ end
58
+ # ---------------------------------------------------------------
59
+ class CachedProxy
60
+
61
+ Object.methods.each do |e|
62
+ delegate e, to: :@__object__
63
+ end
64
+
65
+ def method_missing(*args)
66
+ key = ___compose_key__(args)
67
+ if @__recache__
68
+ Rails.cache.delete(key)
69
+ end
70
+ Rails.cache.fetch(key, expires_in: @__expires_in__) do
71
+ CachedProxy.new(
72
+ @__object__.send(*args),
73
+ expires_in: @__expires_in__,
74
+ recache: @__recache__,
75
+ parent_key: key,
76
+ )
77
+ end
78
+ end
79
+ private
80
+ def ___compose_key__(args)
81
+ result = [@__parent_key__, @__key__]
82
+ result += if @__object__.respond_to?(:to_global_id)
83
+ [@__object__.to_global_id]
84
+ elsif @__object__.is_a?(ActiveRecord::Relation)
85
+ [@__object__.class]
86
+ else
87
+ [@__object__]
88
+ end
89
+ result += args[0..1]
90
+ result.compact.map(&:to_s).join('.').dasherize
91
+ end
92
+ ```
40
93
 
41
94
  ## Installation
42
95
  Add this line to your application's Gemfile:
@@ -3,9 +3,13 @@ require "rails_cached_method/railtie"
3
3
 
4
4
  module RailsCachedMethod
5
5
  class CachedProxy
6
+ # disable warning of redefining __send__ and __object_id__
7
+ old = $VERBOSE.dup
8
+ $VERBOSE = nil
6
9
  Object.methods.each do |e|
7
10
  delegate e, to: :@__object__
8
11
  end
12
+ $VERBOSE = old
9
13
 
10
14
  def initialize(object, key: nil, expires_in:, parent_key: nil, recache: false)
11
15
  @__object__ = object
@@ -1,3 +1,3 @@
1
1
  module RailsCachedMethod
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_cached_method
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk