JuliaCollections / LRUCache.jl
Showing 1 of 2 files from the diff.
Other files ignored by Codecov
test/runtests.jl has changed.

@@ -16,20 +16,18 @@
Loading
16 16
    currentsize::Int
17 17
    maxsize::Int
18 18
    lock::SpinLock
19 -
    by::Callable
20 -
    eviction_callback::Callable
19 +
    by::Any
20 +
    finalizer::Any
21 21
22 -
    LRU{K, V}(; maxsize::Int, by::Callable = _constone,
23 -
              eviction_callback::Callable = _no_callback) where {K, V} =
24 -
        new{K, V}(Dict{K, V}(), CyclicOrderedSet{K}(), 0, maxsize, SpinLock(),
25 -
                  by, eviction_callback)
22 +
    function LRU{K, V}(; maxsize::Int, by = _constone, finalizer = nothing) where {K, V}
23 +
        dict = Dict{K, V}()
24 +
        keyset = CyclicOrderedSet{K}()
25 +
        new{K, V}(dict, keyset , 0, maxsize, SpinLock(), by, finalizer)
26 +
    end
26 27
end
27 28
28 -
function LRU(; maxsize::Int, by::Callable = _constone,
29 -
             eviction_callback::Callable = _no_callback)
30 -
    return LRU{Any,Any}(maxsize=maxsize, by=by,
31 -
                        eviction_callback=eviction_callback)
32 -
end
29 +
LRU(; maxsize::Int, by = _constone, finalizer = nothing) =
30 +
    LRU{Any,Any}(maxsize = maxsize, by = by, finalizer = finalizer)
33 31
34 32
Base.show(io::IO, lru::LRU{K, V}) where {K, V} =
35 33
    print(io, "LRU{$K, $V}(; maxsize = $(lru.maxsize))")
@@ -93,7 +91,7 @@
Loading
93 91
        _unsafe_resize!(lru, evictions)
94 92
        return v
95 93
    end
96 -
    _notify_evictions!(lru.eviction_callback, evictions)
94 +
    _finalize_evicitions!(lru.finalizer, evictions)
97 95
    return v
98 96
end
99 97
function Base.get!(default::Callable, lru::LRU{K, V}, key) where {K, V}
@@ -116,7 +114,7 @@
Loading
116 114
        _unsafe_resize!(lru, evictions)
117 115
    end
118 116
    unlock(lru.lock)
119 -
    _notify_evictions!(lru.eviction_callback, evictions)
117 +
    _finalize_evicitions!(lru.finalizer, evictions)
120 118
    return v
121 119
end
122 120
@@ -147,7 +145,7 @@
Loading
147 145
    lock(lru.lock) do
148 146
        if _unsafe_haskey(lru, key)
149 147
            old_v, n, s = lru.dict[key]
150 -
            if lru.eviction_callback !== _no_callback
148 +
            if lru.finalizer !== nothing
151 149
                push!(evictions, (key, old_v))
152 150
            end
153 151
            lru.currentsize -= s
@@ -160,7 +158,7 @@
Loading
160 158
        end
161 159
        _unsafe_resize!(lru, evictions)
162 160
    end
163 -
    _notify_evictions!(lru.eviction_callback, evictions)
161 +
    _finalize_evicitions!(lru.finalizer, evictions)
164 162
    return lru
165 163
end
166 164
@@ -170,7 +168,7 @@
Loading
170 168
    while lru.currentsize > lru.maxsize
171 169
        key = pop!(lru.keyset)
172 170
        v, n, s = pop!(lru.dict, key)
173 -
        if lru.eviction_callback !== _no_callback
171 +
        if lru.finalizer !== nothing
174 172
            push!(evictions, (key, v))
175 173
        end
176 174
        lru.currentsize -= s
@@ -183,57 +181,54 @@
Loading
183 181
    lock(lru.lock) do
184 182
        _unsafe_resize!(lru, evictions, maxsize)
185 183
    end
186 -
    _notify_evictions!(lru.eviction_callback, evictions)
184 +
    _finalize_evicitions!(lru.finalizer, evictions)
187 185
    return lru
188 186
end
189 187
190 188
function Base.delete!(lru::LRU{K, V}, key) where {K, V}
191 -
    evictions = Tuple{K, V}[]
192 -
    lock(lru.lock) do
189 +
    v = lock(lru.lock) do
193 190
        v, n, s = pop!(lru.dict, key)
194 -
        if lru.eviction_callback !== _no_callback
195 -
            push!(evictions, (key, v))
196 -
        end
197 191
        lru.currentsize -= s
198 192
        _delete!(lru.keyset, n)
193 +
        return v
194 +
    end
195 +
    if lru.finalizer !== nothing
196 +
        lru.finalizer(key, v)
199 197
    end
200 -
    _notify_evictions!(lru.eviction_callback, evictions)
201 198
    return lru
202 199
end
203 200
function Base.pop!(lru::LRU{K, V}, key) where {K, V}
204 -
    evictions = Tuple{K, V}[]
205 -
    v = lock(lru.lock) do
201 +
    (key, v) = lock(lru.lock) do
206 202
        v, n, s = pop!(lru.dict, key)
207 -
        if lru.eviction_callback !== _no_callback
208 -
            push!(evictions, (key, v))
209 -
        end
210 203
        lru.currentsize -= s
211 204
        _delete!(lru.keyset, n)
212 -
        return v
205 +
        return (key, v)
206 +
    end
207 +
    if lru.finalizer !== nothing
208 +
        lru.finalizer(key, v)
213 209
    end
214 -
    _notify_evictions!(lru.eviction_callback, evictions)
215 210
    return v
216 211
end
217 212
218 213
function Base.empty!(lru::LRU{K, V}) where {K, V}
219 214
    evictions = Tuple{K, V}[]
220 215
    lock(lru.lock) do
221 -
        if lru.eviction_callback === _no_callback
216 +
        if lru.finalizer === nothing
222 217
            lru.currentsize = 0
223 218
            empty!(lru.dict)
224 219
            empty!(lru.keyset)
225 220
        else
221 +
            sizehint!(evictions, length(lru))
226 222
            _unsafe_resize!(lru, evictions, 0)
227 223
        end
228 224
    end
229 -
    _notify_evictions!(lru.eviction_callback, evictions)
225 +
    _finalize_evicitions!(lru.finalizer, evictions)
230 226
    return lru
231 227
end
232 228
233 -
function _notify_evictions!(callback, evictions)
234 -
    while !isempty(evictions)
235 -
        key, value = pop!(evictions)
236 -
        callback(key, value)
229 +
function _finalize_evicitions!(finalizer, evictions)
230 +
    for (key, value) in evictions
231 +
        finalizer(key, value)
237 232
    end
238 233
    return
239 234
end
Files Coverage
src 76.52%
Project Totals (2 files) 76.52%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading