4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/disk_store/eviction_strategies/lru.rb', line 4
def files_to_evict
sorted_files = files
.map { |file|
data = nil
File.new(file, 'rb').tap { |fd|
data = { path: file, last_fetch: fd.atime, size: fd.size }
}.close
data
}
.sort { |a, b| a[:last_fetch] <=> b[:last_fetch] }
space_to_evict = current_cache_size - maximum_cache_size
space_evicted = 0
evictions = []
while space_evicted < space_to_evict
evicted_file = sorted_files.shift
space_evicted += evicted_file[:size]
evictions << evicted_file
end
evictions
end
|