rails_cached_method 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -4
- data/lib/rails_cached_method.rb +4 -0
- data/lib/rails_cached_method/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8309a61a67d126ce96170e4ca5e0aa048b4234150afdd5e69e7510f9503822d7
|
4
|
+
data.tar.gz: 0dac1785aeb5560f12fef7dbe1364c4a595c1f1fd09fb6917dffe0fd651e78cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
data/lib/rails_cached_method.rb
CHANGED
@@ -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
|