Skip to content

Commit

Permalink
Minor spacing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zgornel committed Sep 11, 2018
1 parent e65a287 commit a40d6ad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 81 deletions.
68 changes: 34 additions & 34 deletions src/diskcache.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Disk caching

mutable struct DiskCache{T<:Function, I<:Unsigned, O} <: AbstractCache
filename::String # file name
filename::String # file name
memcache::MemoryCache{T, I, O} # MemoryCache structure (active)
offsets::Dict{I, Tuple{Int, Int}} # existing hash - to - file positions
end


# Function that generates a name based on the name of the cached function
_generate_cache_filename(fname::String) = begin
_filename = "_" * fname * "_cache_" * string(hash(fname), base=16) * "_.bin"
return abspath(_filename)
_filename = "_" * fname * "_cache_" * string(hash(fname), base=16) * "_.bin"
return abspath(_filename)
end


Expand All @@ -30,10 +30,10 @@ end
(dc::T where T<:DiskCache)(args...; kwargs...) = begin
_hash = hash([map(hash, args)..., map(hash, collect(kwargs))...])
if _hash in keys(dc.memcache.cache)
out = dc.memcache.cache[_hash]
out = dc.memcache.cache[_hash]
else
@info "Hash miss, caching hash=$_hash..."
out = dc.memcache.func(args...; kwargs...)
@info "Hash miss, caching hash=$_hash..."
out = dc.memcache.func(args...; kwargs...)
dc.memcache.cache[_hash] = out
end
return out
Expand All @@ -54,28 +54,28 @@ end
# Macro supporting construnctions of the form:
# julia> foo(x) = x+1
# julia> fooc = @diskcache foo # now `fooc` is the cached version of `foo`
macro diskcache(symb::Symbol,
filename::String=_generate_cache_filename(string(symb)))
@assert isdefined(Main, symb)
_name = String(symb)
ex = quote
try
DiskCache($symb,
name=$_name,
filename=$filename)
catch excep
@error "Could not create DiskCache. $excep"
end
end
return esc(ex)
macro diskcache(symb::Symbol, filename::String=
_generate_cache_filename(string(symb)))
@assert isdefined(Main, symb)
_name = String(symb)
ex = quote
try
DiskCache($symb,
name=$_name,
filename=$filename)
catch excep
@error "Could not create DiskCache. $excep"
end
end
return esc(ex)
end # macro


# Macro supporting constructions of the form:
# julia> foo(x) = x+1
# julia> fooc = @diskcache foo::Int # expects output to be `Int`
macro diskcache(expr::Expr,
filename::String=_generate_cache_filename(string(expr.args[1])))
macro diskcache(expr::Expr, filename::String=
_generate_cache_filename(string(expr.args[1])))
@assert expr.head == :(::)
@assert length(expr.args) == 2 # two arguments
_symb = expr.args[1]
Expand All @@ -85,20 +85,20 @@ macro diskcache(expr::Expr,

@assert isdefined(Main, _symb) # check that symbol exists
try
@assert _type isa Type
@assert _type isa Type
catch # it may be a variable containing a type
@error "The right-hand argument of `::` is not a type."
@error "The right-hand argument of `::` is not a type."
end

ex = quote
try
DiskCache($_symb,
name=$_name,
filename=$filename,
output_type=$_type)
catch excep
@error "Could not create DiskCache. $excep"
end
end
return esc(ex)
try
DiskCache($_symb,
name=$_name,
filename=$filename,
output_type=$_type)
catch excep
@error "Could not create DiskCache. $excep"
end
end
return esc(ex)
end # macro
83 changes: 41 additions & 42 deletions src/memcache.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Memory caching (dictionary based)

mutable struct MemoryCache{T<:Function, I<:Unsigned, O} <: AbstractCache
name::String
func::T
cache::Dict{I, O}
name::String
func::T
cache::Dict{I, O}
end


Expand All @@ -17,12 +17,12 @@ MemoryCache(f::T where T<:Function;

# Call method
(mc::T where T<:MemoryCache)(args...; kwargs...) = begin
_hash = hash([map(hash, args)..., map(hash, collect(kwargs))...])
if _hash in keys(mc.cache)
out = mc.cache[_hash]
else
@info "Hash miss, caching hash=$_hash..."
out = mc.func(args...; kwargs...)
_hash = hash([map(hash, args)..., map(hash, collect(kwargs))...])
if _hash in keys(mc.cache)
out = mc.cache[_hash]
else
@info "Hash miss, caching hash=$_hash..."
out = mc.func(args...; kwargs...)
mc.cache[_hash] = out
end
return out
Expand All @@ -31,8 +31,8 @@ end

# Show method
show(io::IO, c::MemoryCache) = begin
println(io, "Memory cache for \"$(c.name)\" " *
"with $(length(c.cache)) entries.")
println(io, "Memory cache for \"$(c.name)\" " *
"with $(length(c.cache)) entries.")
end


Expand All @@ -42,43 +42,42 @@ end
# julia> foo(x) = x+1
# julia> fooc = @memcache foo # now `fooc` is the cached version of `foo`
macro memcache(symb::Symbol)
@assert isdefined(Main, symb)
_name = String(symb)
ex = quote
try
MemoryCache($symb, name=$_name)
catch excep
@error "Could not create MemoryCache. $excep"
end
end
return esc(ex)
@assert isdefined(Main, symb)
_name = String(symb)
ex = quote
try
MemoryCache($symb, name=$_name)
catch excep
@error "Could not create MemoryCache. $excep"
end
end
return esc(ex)
end # macro


# Macro supporting constructions of the form:
# julia> foo(x) = x+1
# julia> fooc = @memcache foo::Int # expects output to be `Int`
macro memcache(expr::Expr)
@assert expr.head == :(::)
@assert length(expr.args) == 2 # two arguments
_symb = expr.args[1]
_typesymbol = expr.args[2]
_type = eval(_typesymbol)
_name = String(_symb)

@assert isdefined(Main, _symb) # check that symbol exists
try
@assert _type isa Type
catch # it may be a variable containing a type
@error "The right-hand argument of `::` is not a type."
end

@assert expr.head == :(::)
@assert length(expr.args) == 2 # two arguments
_symb = expr.args[1]
_typesymbol = expr.args[2]
_type = eval(_typesymbol)
_name = String(_symb)

@assert isdefined(Main, _symb) # check that symbol exists
try
@assert _type isa Type
catch # it may be a variable containing a type
@error "The right-hand argument of `::` is not a type."
end
ex = quote
try
MemoryCache($_symb, name=$_name, output_type=$_type)
catch excep
@error "Could not create MemoryCache. $excep"
end
end
return esc(ex)
try
MemoryCache($_symb, name=$_name, output_type=$_type)
catch excep
@error "Could not create MemoryCache. $excep"
end
end
return esc(ex)
end # macro
10 changes: 5 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function cachesync!(dc::T) where T<:DiskCache
# dump
# else (if file/path does exists)
#
# create directory
# create file,
# create directory
# create file,
end


Expand All @@ -31,14 +31,14 @@ function persist!(mc::T; filename::String=
_generate_cache_filename(mc.name)) where T<:MemoryCache
# Initialize structures
_data = mc.cache
I, O = typeof(_data).parameters # works for `Dict`
I, O = typeof(_data).parameters # works for `Dict`
offsets = Dict{I, Tuple{Int,Int}}()
_dir = join(split(filename, "/")[1:end-1], "/")
!isempty(_dir) && !isdir(_dir) && mkdir(_dir)
# Write header
fid = open(filename, "w") # overwrite/create file
serialize(fid, I)
serialize(fid, O)
serialize(fid, I)
serialize(fid, O)
# Write data pairs
for (hash, datum) in _data
prevpos = position(fid)
Expand Down

0 comments on commit a40d6ad

Please sign in to comment.