@@ -40,7 +40,7 @@ require 'net/http'
4040
4141class 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
7878> a = GitHubApiAdapter.new
7979> a.star_count
8080Fetching 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
100100Fetching data from GitHub
101- => 2
101+ => 19
102102> a.star_count_without_cache
103103Fetching 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
116116Fetching 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
124124Fetching data from GitHub
125- => 2
125+ => 19
126126```
127127
128128## Additional Configuration
@@ -151,7 +151,7 @@ require 'net/http'
151151class 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')
178178Fetching data from GitHub for cacheable
179- => 2
179+ => 19
180180> a.star_count('cacheable')
181- => 2
181+ => 19
182182> a.star_count('tokenautocomplete')
183183Fetching 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'
204204class 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')
229229Fetching data from GitHub for tokenautocomplete
230- => 1142
230+ => 1164
231231a.star_count('tokenautocomplete')
232- => 1142
232+ => 1164
233233
234234> a.star_count('cacheable')
235235Fetching data from GitHub for cacheable
236- => 2
236+ => 19
237237> a.star_count('cacheable')
238238Fetching 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
300300Fetching 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
306306Fetching 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
0 commit comments