-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemoize_example.rb
More file actions
54 lines (43 loc) · 1.46 KB
/
memoize_example.rb
File metadata and controls
54 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'cacheable' # this may not be necessary depending on your autoloading system
# Wrap the default adapter to log cache reads
logging_adapter = Cacheable::CacheAdapters::MemoryAdapter.new
original_fetch = logging_adapter.method(:fetch)
logging_adapter.define_singleton_method(:fetch) do |key, *args, &block|
puts " [cache] fetch #{key.inspect}"
original_fetch.call(key, *args, &block)
end
Cacheable.cache_adapter = logging_adapter
class ExpensiveService
include Cacheable
cacheable :without_memoize
cacheable :with_memoize, memoize: true
def without_memoize
puts ' [method] computing value'
42
end
def with_memoize
puts ' [method] computing value'
42
end
end
svc = ExpensiveService.new
puts '--- without memoize ---'
2.times { svc.without_memoize }
# --- without memoize ---
# [cache] fetch ["ExpensiveService", :without_memoize]
# [method] computing value
# [cache] fetch ["ExpensiveService", :without_memoize] <-- adapter hit again (deserialization cost)
puts
puts '--- with memoize: true ---'
2.times { svc.with_memoize }
# --- with memoize: true ---
# [cache] fetch ["ExpensiveService", :with_memoize]
# [method] computing value
# <-- no adapter hit, returned from instance memo
puts
puts '--- after clearing ---'
svc.clear_with_memoize_cache
svc.with_memoize
# --- after clearing ---
# [cache] fetch ["ExpensiveService", :with_memoize]
# [method] computing value