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

Add clear-cache #60

Merged
merged 9 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ julia> x(1)
Running
1

julia> x(1)
1

julia> @clear_cache(x)
IdDict{Any,Any}()

julia> x(1)
Running
1

julia> x(1)
1
```
Expand All @@ -33,7 +43,7 @@ using Memoize
end
```

You can also specify the full function call for constructing the dictionary. For example, to use LRUCache.jl:
You can also specify the full function call for constructing the dictionary. For example, to use LRUCache.jl:

```julia
using Memoize
Expand Down
15 changes: 5 additions & 10 deletions src/Memoize.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Memoize
using MacroTools: isexpr, combinedef, namify, splitarg, splitdef
export @memoize
export @memoize, @clear_cache

macro memoize(args...)
if length(args) == 1
Expand Down Expand Up @@ -78,15 +78,10 @@ macro memoize(args...)
end

macro clear_cache(f)
fcachename = Symbol("##", f, "_memoized_cache")
mod = __module__
fcache = getfield(mod, fcachename)

function clear_cache!(x::IdDict)
foreach(k -> delete!(fcache, k), keys(fcache))
end

Core.eval(mod, :($(clear_cache!(fcache))))
esc(quote
fcachename = Symbol("##", $f, "_memoized_cache")
Base.empty!(eval(fcachename))
end)
rikhuijzer marked this conversation as resolved.
Show resolved Hide resolved
end

end # module