-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata_cache.cc
100 lines (78 loc) · 2.59 KB
/
metadata_cache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "smbprovider/metadata_cache.h"
#include <base/time/tick_clock.h>
namespace smbprovider {
MetadataCache::MetadataCache(base::TickClock* tick_clock,
base::TimeDelta entry_lifetime,
Mode mode)
: tick_clock_(tick_clock), entry_lifetime_(entry_lifetime), mode_(mode) {}
MetadataCache::~MetadataCache() = default;
void MetadataCache::AddEntry(const DirectoryEntry& entry) {
// NowTicks() is non-decreasing so any value we get will always be at
// at least equal to the higest version we've seen before.
max_expiration_time_ = tick_clock_->NowTicks() + entry_lifetime_;
cache_[entry.full_path] = CacheEntry(entry, max_expiration_time_);
}
bool MetadataCache::FindEntry(const std::string& full_path,
DirectoryEntry* out_entry) {
if (!IsEnabled()) {
return false;
}
auto entry_iter = cache_.find(full_path);
if (entry_iter == cache_.end()) {
return false;
}
if (IsExpired(entry_iter->second)) {
cache_.erase(entry_iter);
return false;
}
*out_entry = entry_iter->second.entry;
return true;
}
void MetadataCache::ClearAll() {
cache_.clear();
}
bool MetadataCache::IsEmpty() const {
return cache_.empty();
}
bool MetadataCache::RemoveEntry(const std::string& entry_path) {
return cache_.erase(entry_path) > 0;
}
void MetadataCache::PurgeExpiredEntries() {
if (IsEmpty()) {
// Nothing to do if it is already empty.
return;
}
// If all entries are expired, just clear the entire map.
if (AreAllEntriesExpired()) {
ClearAll();
return;
}
// Otherwise iterate through the map removing the expired entries.
const base::TimeTicks threshold = tick_clock_->NowTicks();
auto it = cache_.cbegin();
while (it != cache_.cend()) {
if (MetadataCache::IsExpired(it->second, threshold)) {
it = cache_.erase(it);
} else {
++it;
}
}
}
bool MetadataCache::IsExpired(const CacheEntry& cache_entry,
base::TimeTicks threshold) {
return threshold > cache_entry.expiration_time;
}
bool MetadataCache::IsExpired(
const MetadataCache::CacheEntry& cache_entry) const {
return MetadataCache::IsExpired(cache_entry, tick_clock_->NowTicks());
}
bool MetadataCache::AreAllEntriesExpired() const {
return tick_clock_->NowTicks() > max_expiration_time_;
}
bool MetadataCache::IsEnabled() const {
return mode_ != Mode::kDisabled;
}
} // namespace smbprovider