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 7 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
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ Easy memoization for Julia.
using Memoize
@memoize function x(a)
println("Running")
a
2a
end
```

```
julia> x(1)
Running
1
2

julia> @memoize_cache(x)
IdDict{Any,Any} with 1 entry:
(1,) => 2

julia> x(1)
2

julia> empty!(@memoize_cache(x))
IdDict{Any,Any}()

julia> x(1)
Running
2

julia> x(1)
1
2
```

By default, Memoize.jl uses an [`IdDict`](https://docs.julialang.org/en/v1/base/collections/#Base.IdDict) as a cache, but it's also possible to specify the type of the cache. If you want to cache vectors based on the values they contain, you probably want this:
Expand All @@ -33,7 +47,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
20 changes: 18 additions & 2 deletions src/Memoize.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Memoize
using MacroTools: isexpr, combinedef, namify, splitarg, splitdef
export @memoize
export @memoize, @memoize_cache

cache_name(f) = Symbol("##", f, "_memoized_cache")

macro memoize(args...)
if length(args) == 1
Expand Down Expand Up @@ -49,7 +51,7 @@ macro memoize(args...)
end
end

fcachename = Symbol("##", f, "_memoized_cache")
fcachename = cache_name(f)
mod = __module__
fcache = isdefined(mod, fcachename) ?
getfield(mod, fcachename) :
Expand All @@ -76,4 +78,18 @@ macro memoize(args...)
end)

end

function memoize_cache(m::Module, f::Function)
fmodulename = which(m, Symbol(f))
getproperty(fmodulename, cache_name(f))
rikhuijzer marked this conversation as resolved.
Show resolved Hide resolved
end

macro memoize_cache(f)
m = __module__
f_symbol = :($f)
esc(quote
Memoize.memoize_cache($m, $f_symbol)
end)
end

end # module
23 changes: 21 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ end
@test simple(6) == 6
@test run == 2

empty!(@memoize_cache(simple))
@test simple(6) == 6
@test run == 3
@test simple(6) == 6
@test run == 3

run = 0
@memoize function typed(a::Int)
global run += 1
Expand Down Expand Up @@ -295,7 +301,6 @@ end
@test vararg_func((1,1), (1,2)) == (1,1)
@test run == 2


module MemoizeTest
using Test
using Memoize
Expand All @@ -318,7 +323,20 @@ end
@test custom_dict(1) == 1
@test run == 2

end
end # module

using .MemoizeTest
using .MemoizeTest: custom_dict

empty!(@memoize_cache(custom_dict))
@test custom_dict(1) == 1
@test MemoizeTest.run == 3
@test custom_dict(1) == 1
@test MemoizeTest.run == 3

empty!(@memoize_cache(MemoizeTest.custom_dict))
@test custom_dict(1) == 1
@test MemoizeTest.run == 4

run = 0
@memoize Dict{Tuple{String},Int}() function dict_call(a::String)::Int
Expand All @@ -333,3 +351,4 @@ end
@test run == 2
@test dict_call("bb") == 2
@test run == 2