Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear memery cache for specific method #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ a.memoized?(:call) # => true
a.memoized?(:execute) # => false
```

Clear memery cache for specific method:

```ruby
class A
include Memery

memoize def call
puts "calculating"
42
end
end

a = A.new

a.call # outputs "calculating" and returns 42
a.clear_memery_cache(:call)
a.call # outputs "calculating" and returns 42
```

## Difference with other gems
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner (for example you are able to inspect the original method body using `method(:x).super_method.source`) and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:

Expand Down
15 changes: 15 additions & 0 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,20 @@ module InstanceMethods
def clear_memery_cache!
@_memery_memoized_values = {}
end

def clear_memery_cache(method_name)
store = @_memery_memoized_values
return unless store&.any?

memoization_klass = Memery::ClassMethods.const_get(:MemoizationModule)

self.class.ancestors.each do |klass|
next unless klass.is_a?(memoization_klass)
method_key = "#{method_name}_#{klass.object_id}"
store.each_key do |key|
store.delete(key) if key == method_key || (key.is_a?(Array) && key.first == method_key)
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ class H
end
end

context "flushing specific cache key" do
let(:h) { H.new }

specify do
m_values = [ h.m, h.m ]
n_values = [ h.n, h.n ]
h.clear_memery_cache(:m)
m_values << h.m
n_values << h.n
expect(m_values).to eq([:m, :m, :m])
expect(n_values).to eq([:n, :n, :n])
expect(CALLS).to eq([:m, :n, :m])
end
end

context "method with args" do
specify do
values = [ a.m_args(1, 1), a.m_args(1, 1), a.m_args(1, 2) ]
Expand Down Expand Up @@ -261,6 +276,17 @@ class H
expect(CALLS).to eq([[1, 2]])
expect(B_CALLS).to eq([[1, 1], [1, 2]])
end

context "clear_memery_cache clears cache for ancestors" do
specify do
values = [ b.m_args(1, 5), b.m_args(1, 5) ]
b.clear_memery_cache(:m_args)
values << b.m_args(1, 5)
expect(values).to eq([100, 100, 100])
expect(CALLS).to eq([[1, 2], [1, 2]])
expect(B_CALLS).to eq([[1, 5], [1, 5]])
end
end
end

context "module" do
Expand Down