rails_cached_method 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/MIT-LICENSE +20 -0
- data/README.md +66 -0
- data/Rakefile +13 -0
- data/lib/rails_cached_method.rb +67 -0
- data/lib/rails_cached_method/railtie.rb +4 -0
- data/lib/rails_cached_method/version.rb +3 -0
- data/lib/tasks/rails_cached_method_tasks.rake +4 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: adb70332fd66d09b43164dc071a109c780f10008ea6a1aa83b97b46196452c5e
|
4
|
+
data.tar.gz: 0d2ecc3d2b86796d1e9da3a724008e8c419d81ddf30c820f5a169d01b48d05ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3396b9439c314d57957aed51aa90b267ad9fdbecce51baca3c4160814e099d68d6aa73b9ab1ea6e43a42ad61f023ff5809552f5ad27e44d2d38b4be01fc75332
|
7
|
+
data.tar.gz: df6cb825167aad571a4168b947e1515f9fa11f1adbbc8f21aecec6b0898137146237616d2ece74c97316c1c957dffde25fe290200c3061269ada6a504ffc466f
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2021 Igor Kasyanchuk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Rails Cached Method
|
2
|
+
|
3
|
+
Simple way to cache results of methods. Using `Rails.cache` inside with default expiration time of 1 minute.
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
To get a value and cache it just write a code like:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
User.cached.admins
|
12
|
+
post.cached.comments.last
|
13
|
+
User.cached.where(role: :moderator).count
|
14
|
+
Project.cached.first.some_heavey_method
|
15
|
+
```
|
16
|
+
|
17
|
+
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.
|
18
|
+
|
19
|
+
For example:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
post.cached.comments.first # A
|
23
|
+
post.cached.comments.last # B
|
24
|
+
```
|
25
|
+
|
26
|
+
Here `post.cached.comments` will return same collection.
|
27
|
+
|
28
|
+
You don't need to specify a key, because it's dynamically set based on object, arguments, etc.
|
29
|
+
|
30
|
+
Options:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
cached(key: nil, expires_in: 1.minute, recache: false)
|
34
|
+
```
|
35
|
+
|
36
|
+
key - you can set your key.
|
37
|
+
expires_in - TTL of cached object.
|
38
|
+
recache - should be key deleted and cached again.
|
39
|
+
|
40
|
+
|
41
|
+
## Installation
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'rails_cached_method'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
```bash
|
50
|
+
$ bundle
|
51
|
+
```
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
```bash
|
55
|
+
$ gem install rails_cached_method
|
56
|
+
```
|
57
|
+
|
58
|
+
## TODO
|
59
|
+
|
60
|
+
- think if rails dependency could be replaced with something more lightweight
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
Contribution directions go here.
|
64
|
+
|
65
|
+
## License
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "rails_cached_method/version"
|
2
|
+
require "rails_cached_method/railtie"
|
3
|
+
|
4
|
+
module RailsCachedMethod
|
5
|
+
class CachedProxy
|
6
|
+
Object.methods.each do |e|
|
7
|
+
delegate e, to: :@__object__
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(object, key: nil, expires_in:, parent_key: nil, recache: false)
|
11
|
+
@__object__ = object
|
12
|
+
@__key__ = key
|
13
|
+
@__expires_in__ = expires_in
|
14
|
+
@__recache__ = recache
|
15
|
+
@__parent_key__ = parent_key
|
16
|
+
@__object__
|
17
|
+
end
|
18
|
+
|
19
|
+
def __value__
|
20
|
+
@__object__
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(*args)
|
24
|
+
key = ___compose_key__(args)
|
25
|
+
if @__recache__
|
26
|
+
#puts "deleting: #{key}"
|
27
|
+
Rails.cache.delete(key)
|
28
|
+
end
|
29
|
+
#puts "key: #{key}, expires_in: #{@__expires_in__}, args: #{args}, recache: #{@__recache__}"
|
30
|
+
Rails.cache.fetch(key, expires_in: @__expires_in__) do
|
31
|
+
CachedProxy.new(
|
32
|
+
@__object__.send(*args),
|
33
|
+
expires_in: @__expires_in__,
|
34
|
+
recache: @__recache__,
|
35
|
+
parent_key: key,
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def ___compose_key__(args)
|
42
|
+
result = [@__parent_key__, @__key__]
|
43
|
+
result += if @__object__.respond_to?(:to_global_id)
|
44
|
+
[@__object__.to_global_id]
|
45
|
+
elsif @__object__.is_a?(ActiveRecord::Relation)
|
46
|
+
[@__object__.class]
|
47
|
+
else
|
48
|
+
[@__object__]
|
49
|
+
end
|
50
|
+
result += args[0..1]
|
51
|
+
result.compact.map(&:to_s).join('.').dasherize
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module CachedProxyExt
|
56
|
+
def cached(key: nil, expires_in: 1.minute, recache: false)
|
57
|
+
CachedProxy.new(
|
58
|
+
self,
|
59
|
+
key: key,
|
60
|
+
expires_in: expires_in,
|
61
|
+
recache: recache
|
62
|
+
)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Object.send :include, RailsCachedMethod::CachedProxyExt
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_cached_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Very easy way to cache results of your methods
|
42
|
+
email:
|
43
|
+
- igorkasyanchuk@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/rails_cached_method.rb
|
52
|
+
- lib/rails_cached_method/railtie.rb
|
53
|
+
- lib/rails_cached_method/version.rb
|
54
|
+
- lib/tasks/rails_cached_method_tasks.rake
|
55
|
+
homepage: http://github.com/igorkasyanchuk/rails_cached_method
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata:
|
59
|
+
homepage_uri: http://github.com/igorkasyanchuk/rails_cached_method
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.0.3
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Very easy way to cache results of your methods
|
79
|
+
test_files: []
|