Skip to content

Commit 7b37e38

Browse files
committed
Update examples and readme
We’re so popular :)
1 parent f954001 commit 7b37e38

5 files changed

Lines changed: 41 additions & 41 deletions

File tree

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require 'net/http'
4040

4141
class GitHubApiAdapter
4242
def star_count
43-
puts "Fetching data from GitHub"
43+
puts 'Fetching data from GitHub'
4444
url = 'https://api.github.com/repos/splitwise/cacheable'
4545

4646
JSON.parse(Net::HTTP.get(URI.parse(url)))['stargazers_count']
@@ -63,7 +63,7 @@ class GitHubApiAdapter
6363
cacheable :star_count
6464

6565
def star_count
66-
puts "Fetching data from GitHub"
66+
puts 'Fetching data from GitHub'
6767
url = 'https://api.github.com/repos/splitwise/cacheable'
6868

6969

@@ -78,9 +78,9 @@ end
7878
> a = GitHubApiAdapter.new
7979
> a.star_count
8080
Fetching data from GitHub
81-
=> 2
81+
=> 19
8282
> a.star_count
83-
=> 2
83+
=> 19
8484
8585
# Notice that "Fetching data from GitHub" was not output the 2nd time the method was invoked.
8686
# The network call and result parsing would also not be performed again.
@@ -98,12 +98,12 @@ The cache can intentionally be skipped by appending `_without_cache` to the meth
9898
> a = GitHubApiAdapter.new
9999
> a.star_count
100100
Fetching data from GitHub
101-
=> 2
101+
=> 19
102102
> a.star_count_without_cache
103103
Fetching data from GitHub
104-
=> 2
104+
=> 19
105105
> a.star_count
106-
=> 2
106+
=> 19
107107
```
108108

109109
#### Remove the Value via `clear_#{method}_cache`
@@ -114,15 +114,15 @@ The cached value can be cleared at any time by calling `clear_#{your_method_name
114114
> a = GitHubApiAdapter.new
115115
> a.star_count
116116
Fetching data from GitHub
117-
=> 2
117+
=> 19
118118
> a.star_count
119-
=> 2
119+
=> 19
120120
121121
> a.clear_star_count_cache
122122
=> true
123123
> a.star_count
124124
Fetching data from GitHub
125-
=> 2
125+
=> 19
126126
```
127127

128128
## Additional Configuration
@@ -151,7 +151,7 @@ require 'net/http'
151151
class GitHubApiAdapter
152152
include Cacheable
153153

154-
cacheable :star_count, key_format: -> (target, method_name, method_args) do
154+
cacheable :star_count, key_format: ->(target, method_name, method_args) do
155155
[target.class, method_name, method_args.first, Time.now.strftime('%Y-%m-%d')].join('/')
156156
end
157157

@@ -176,14 +176,14 @@ In addition, we're including the current date in the cache key so calling this m
176176
> a = GitHubApiAdapter.new
177177
> a.star_count('cacheable')
178178
Fetching data from GitHub for cacheable
179-
=> 2
179+
=> 19
180180
> a.star_count('cacheable')
181-
=> 2
181+
=> 19
182182
> a.star_count('tokenautocomplete')
183183
Fetching data from GitHub for tokenautocomplete
184-
=> 1142
184+
=> 1164
185185
> a.star_count('tokenautocomplete')
186-
=> 1142
186+
=> 1164
187187
188188
# In this example the follow cache keys are generated:
189189
# GitHubApiAdapter/star_count/cacheable/2018-09-21
@@ -204,7 +204,7 @@ require 'net/http'
204204
class GitHubApiAdapter
205205
include Cacheable
206206

207-
cacheable :star_count, unless: :growing_fast?, key_format: -> (target, method_name, method_args) do
207+
cacheable :star_count, unless: :growing_fast?, key_format: ->(target, method_name, method_args) do
208208
[target.class, method_name, method_args.first].join('/')
209209
end
210210

@@ -227,16 +227,16 @@ Cacheable is new so we don't want to cache the number of stars it has as we expe
227227
> a = GitHubApiAdapter.new
228228
> a.star_count('tokenautocomplete')
229229
Fetching data from GitHub for tokenautocomplete
230-
=> 1142
230+
=> 1164
231231
a.star_count('tokenautocomplete')
232-
=> 1142
232+
=> 1164
233233
234234
> a.star_count('cacheable')
235235
Fetching data from GitHub for cacheable
236-
=> 2
236+
=> 19
237237
> a.star_count('cacheable')
238238
Fetching data from GitHub for cacheable
239-
=> 2
239+
=> 19
240240
```
241241

242242
### Cache Options
@@ -298,15 +298,15 @@ end
298298
```irb
299299
> GitHubApiAdapter.star_count_for_cacheable
300300
Fetching data from GitHub for cacheable
301-
=> 2
301+
=> 19
302302
> GitHubApiAdapter.star_count_for_cacheable
303-
=> 2
303+
=> 19
304304
305305
> GitHubApiAdapter.star_count_for_tokenautocomplete
306306
Fetching data from GitHub for tokenautocomplete
307-
=> 1142
307+
=> 1164
308308
> GitHubApiAdapter.star_count_for_tokenautocomplete
309-
=> 1142
309+
=> 1164
310310
```
311311

312312
### Other Notes / Frequently Asked Questions

examples/class_method_example.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def self.star_count_for_tokenautocomplete
2929

3030
GitHubApiAdapter.star_count_for_cacheable
3131
# Fetching data from GitHub for cacheable
32-
# => 2
32+
# => 19
3333
GitHubApiAdapter.star_count_for_cacheable
34-
# => 2
34+
# => 19
3535

3636
GitHubApiAdapter.star_count_for_tokenautocomplete
3737
# Fetching data from GitHub for tokenautocomplete
38-
# => 1142
38+
# => 1164
3939
GitHubApiAdapter.star_count_for_tokenautocomplete
40-
# => 1142
40+
# => 1164

examples/conditional_example.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def growing_fast?(_method_name, method_args)
2424
a = GitHubApiAdapter.new
2525
a.star_count('tokenautocomplete')
2626
# Fetching data from GitHub for tokenautocomplete
27-
# => 1142
27+
# => 1164
2828
a.star_count('tokenautocomplete')
29-
# => 1142
29+
# => 1164
3030

3131
a.star_count('cacheable')
3232
# Fetching data from GitHub for cacheable
33-
# => 2
33+
# => 19
3434
a.star_count('cacheable')
3535
# Fetching data from GitHub for cacheable
36-
# => 2
36+
# => 19

examples/custom_key_example.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def star_count(repo)
2020
a = GitHubApiAdapter.new
2121
a.star_count('cacheable')
2222
# Fetching data from GitHub for cacheable
23-
# => 2
23+
# => 19
2424
a.star_count('cacheable')
25-
# => 2
25+
# => 19
2626
a.star_count('tokenautocomplete')
2727
# Fetching data from GitHub for tokenautocomplete
28-
# => 1142
28+
# => 1164
2929
a.star_count('tokenautocomplete')
30-
# => 1142
30+
# => 1164

examples/simple_example.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ def star_count
1818
a = GitHubApiAdapter.new
1919
a.star_count
2020
# Fetching data from GitHub
21-
# => 2
21+
# => 19
2222
a.star_count
23-
# => 2
23+
# => 19
2424

2525
a.star_count_without_cache
2626
# Fetching data from GitHub
27-
# => 2
27+
# => 19
2828
a.star_count
29-
# => 2
29+
# => 19
3030

3131
a.clear_star_count_cache
3232
# => true
3333
a.star_count
3434
# Fetching data from GitHub
35-
# => 2
35+
# => 19

0 commit comments

Comments
 (0)