cachethod 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/LICENSE +8 -0
- data/README.md +117 -0
- data/lib/cachethod.rb +43 -1
- data/lib/cachethod/version.rb +1 -1
- metadata +4 -4
- data/lib/cachethod/active_record_base_ext.rb +0 -45
- data/lib/tasks/cachethod_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjE2N2IyNDUyMzQ1MjEzMGQ2Mjk5YzdhMzM2NTdkMjJlMGRmYjMyMA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NGQxMmIxNGNmYzk0Yjc5YzkyYjRhZDYwN2YwNmM3YmFlMDgwOTkzNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YmFlMGMxNzJlNzc4NGNjOWI2ZGU0NGUxNzNmNjE1NTg1MjNiMWJiZGUzOGJh
|
10
|
+
ZTQ2MDgyNzhiMWU0NjZhNzM3ZjI3MWY5MjI5NzI4NjBhMDNjMTFhNjczNzQ4
|
11
|
+
NjA0OTlkZWQ1ODQxYTY2OTYzNDVkNzk0ZDI5NGY1MDFmODIzZjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2FiYjFlZWE3NTNiZTA0ZTczYTBhY2ZmMzg0YjNhN2VjZmY2NTU3Y2RhMGRk
|
14
|
+
M2UyNjMwYzRlNDE5YzE4Y2ZmZjY4NTMyYjQwNzY1Y2RlNWY3MDI5NWQ0MzI0
|
15
|
+
OWIwNGQzNDYyM2VkZTYwOTRjZDgzYTYwNjQ4MTdhMjIyOTFhMWM=
|
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
/*
|
2
|
+
* ----------------------------------------------------------------------------
|
3
|
+
* "THE BEER-WARE LICENSE" (Revision 42):
|
4
|
+
* As long as you retain this notice you can do whatever you want with this
|
5
|
+
* stuff. If we meet some day, and you think this stuff is worth it, you can
|
6
|
+
* buy me a beer in return. Rene Klacan
|
7
|
+
* ----------------------------------------------------------------------------
|
8
|
+
*/
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Cachethod
|
2
|
+
|
3
|
+
With this Rails plugin you can **cache** your **model methods** and speed up
|
4
|
+
obtaining results. This can be very useful when you are doing some
|
5
|
+
expensive operations (eg. reading from file or doing some http request)
|
6
|
+
in your model methods and you don't need fresh results every time you
|
7
|
+
invoke the method or results just don't change so frequently.
|
8
|
+
|
9
|
+
**Works out of box with Rails 3 and Rails 4.**
|
10
|
+
|
11
|
+
## About
|
12
|
+
|
13
|
+
It wraps Rails.cache.fetch method so it is completely without
|
14
|
+
configuration and if you change cache configuration of your project it
|
15
|
+
will be reflected also on Cachethod methods.
|
16
|
+
|
17
|
+
## Install
|
18
|
+
|
19
|
+
In Gemfile
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'cachethod'
|
23
|
+
```
|
24
|
+
|
25
|
+
Then run
|
26
|
+
|
27
|
+
```
|
28
|
+
bundle install
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
First step is including **Cachethod** module to your class.
|
34
|
+
|
35
|
+
Next you can choose one of following methods:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
cache_method :method_name, expires_in: 1.minutes
|
39
|
+
cachethod :method_name, expires_in: 2.days
|
40
|
+
cache_methods [:method1_name, :method2_name], expires_in: 3.weeks
|
41
|
+
cachethods [:method2_name, :method2_name], expires_in: 4.months
|
42
|
+
```
|
43
|
+
|
44
|
+
First argument is name of the method you want to cache and other
|
45
|
+
arguments are the same that you are used to pass on Rails.cache.fetch
|
46
|
+
method.
|
47
|
+
|
48
|
+
Just put it in your controller:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class User < ActiveRecord::Base
|
52
|
+
include Cachethod
|
53
|
+
|
54
|
+
cache_method :some_io_method, expires_in: 10.minutes
|
55
|
+
|
56
|
+
def some_io_method
|
57
|
+
...
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Then invoke cached method multiple times in the usual way and you will
|
63
|
+
see the difference:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
user.some_io_method
|
67
|
+
user.some_io_method
|
68
|
+
user.some_io_method
|
69
|
+
```
|
70
|
+
|
71
|
+
If you want to access uncached version of your method then you can do:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
user.some_io_method!
|
75
|
+
```
|
76
|
+
|
77
|
+
## Result
|
78
|
+
|
79
|
+
For example it translates following code:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class Stormtrooper
|
83
|
+
include Cachethod
|
84
|
+
|
85
|
+
cache_methods [:some_io_method, :another_io_method], expires_in: 10.minutes
|
86
|
+
|
87
|
+
def some_io_method
|
88
|
+
...
|
89
|
+
end
|
90
|
+
|
91
|
+
def another_io_method
|
92
|
+
...
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
into the equivalent of:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Stormtrooper < ActiveRecord::Base
|
101
|
+
def some_io_method
|
102
|
+
Rails.cache.fetch(:some_key, expires_in: 10.minutes) do
|
103
|
+
...
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def another_io_method
|
108
|
+
Rails.cache.fetch(:another_key, expires_in: 10.minutes) do
|
109
|
+
...
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
This library is distributed under the Beerware license.
|
data/lib/cachethod.rb
CHANGED
@@ -1 +1,43 @@
|
|
1
|
-
|
1
|
+
module Cachethod
|
2
|
+
def self.included base
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def cache_method names, *args
|
8
|
+
@methods_to_cache ||= {}
|
9
|
+
|
10
|
+
[*names].each do |name|
|
11
|
+
if instance_methods.include?(name.to_sym)
|
12
|
+
cache_method_create(name, *args)
|
13
|
+
else
|
14
|
+
@methods_to_cache[name.to_sym] = args
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
alias_method :cache_methods, :cache_method
|
20
|
+
alias_method :cachethod, :cache_method
|
21
|
+
alias_method :cachethods, :cache_method
|
22
|
+
|
23
|
+
def method_added name
|
24
|
+
super
|
25
|
+
return if @methods_to_cache.nil? || @methods_to_cache[name].nil?
|
26
|
+
args = @methods_to_cache.delete(name)
|
27
|
+
cache_method_create(name, *args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def cache_method_create name, *args
|
31
|
+
define_method "#{name}_cached" do
|
32
|
+
cache_key = "cachethod.#{self.class.to_s.underscore}-#{id}.#{name}"
|
33
|
+
|
34
|
+
Rails.cache.fetch(cache_key, *args) do
|
35
|
+
send("#{name}!")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method "#{name}!", name
|
40
|
+
alias_method name, "#{name}_cached"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/cachethod/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cachethod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rene Klacan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -45,11 +45,11 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
48
50
|
- Rakefile
|
49
51
|
- lib/cachethod.rb
|
50
|
-
- lib/cachethod/active_record_base_ext.rb
|
51
52
|
- lib/cachethod/version.rb
|
52
|
-
- lib/tasks/cachethod_tasks.rake
|
53
53
|
- test/cachethod_test.rb
|
54
54
|
- test/test_helper.rb
|
55
55
|
homepage: https://github.com/reneklacan/cachethod
|
@@ -1,45 +0,0 @@
|
|
1
|
-
class ActiveRecord::Base
|
2
|
-
class << self
|
3
|
-
alias_method :method_added_original, :method_added
|
4
|
-
|
5
|
-
def cache_method name, *args
|
6
|
-
@methods_to_cache ||= {}
|
7
|
-
|
8
|
-
if name.is_a?(String) || name.is_a?(Symbol)
|
9
|
-
@methods_to_cache[name.to_sym] = args
|
10
|
-
elsif method_name.is_a?(Array)
|
11
|
-
cache_methods(name, *args)
|
12
|
-
else
|
13
|
-
raise Exception.new('Invalid first argument for cachethod method!')
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def cache_methods names, *args
|
18
|
-
@methods_to_cache ||= {}
|
19
|
-
|
20
|
-
names.each do |name|
|
21
|
-
@methods_to_cache[name.to_sym] = args
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
alias_method :cachethod, :cache_method
|
26
|
-
alias_method :cachethods, :cache_method
|
27
|
-
|
28
|
-
def method_added name
|
29
|
-
return if @methods_to_cache.nil? || @methods_to_cache[name].nil?
|
30
|
-
|
31
|
-
args = @methods_to_cache.delete(name)
|
32
|
-
|
33
|
-
define_method "#{name}_cached" do
|
34
|
-
cache_key = "cachethod.#{self.class.to_s.underscore}-#{id}.#{name}"
|
35
|
-
|
36
|
-
Rails.cache.fetch(cache_key, *args) do
|
37
|
-
send("#{name}!")
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
alias_method "#{name}!", name
|
42
|
-
alias_method name, "#{name}_cached"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|