File: persistent_cache_collection.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download | duplicates (4)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/persistent_cache/persistent_cache_collection.h"

#include <memory>

#include "components/persistent_cache/backend_params.h"
#include "components/persistent_cache/backend_params_manager.h"
#include "components/persistent_cache/entry.h"
#include "components/persistent_cache/persistent_cache.h"

namespace {
constexpr size_t kLruCacheCapacity = 100;

// Reducing the footprint of the collection to exactly the desired target could
// have the effect of rapidly going over the limit again. This might end up
// issuing more reductions than desirable. This defines some headroom to try and
// mitigate the issue.
constexpr int64_t kFootPrintReductionHeadroomPercent = 10;
}  // namespace

namespace persistent_cache {

PersistentCacheCollection::PersistentCacheCollection(
    std::unique_ptr<BackendParamsManager> backend_params_manager,
    int64_t target_footprint)
    : backend_params_manager_(std::move(backend_params_manager)),
      persistent_caches_(kLruCacheCapacity),
      target_footprint_(target_footprint) {
  ReduceFootPrint();
}

PersistentCacheCollection::~PersistentCacheCollection() = default;

std::unique_ptr<Entry> PersistentCacheCollection::Find(
    const std::string& cache_id,
    std::string_view key) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  return GetOrCreateCache(cache_id)->Find(key);
}

void PersistentCacheCollection::ReduceFootPrint() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Clear all managed persistent caches so they don't hold on to files or
  // prevent their deletion.
  persistent_caches_.Clear();

  int64_t adjusted_target =
      target_footprint_ * (100 - kFootPrintReductionHeadroomPercent) / 100;
  int64_t current_footprint =
      backend_params_manager_->BringDownTotalFootprintOfFiles(adjusted_target)
          .current_footprint;

  bytes_until_footprint_reduction_ = target_footprint_ - current_footprint;
}

void PersistentCacheCollection::Insert(const std::string& cache_id,
                                       std::string_view key,
                                       base::span<const uint8_t> content,
                                       EntryMetadata metadata) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Approximate the footprint of this insert to the size of the key and value
  // combined. This is optimistic in some ways since it doesn't account for any
  // overhead and pessimimistic as it assumes every single write is both new and
  // doesn't evict something else.
  bytes_until_footprint_reduction_ -= (key.size() + content.size());
  if (bytes_until_footprint_reduction_ <= 0) {
    ReduceFootPrint();
  }

  GetOrCreateCache(cache_id)->Insert(key, content, metadata);
}

void PersistentCacheCollection::ClearForTesting() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  persistent_caches_.Clear();
}

void PersistentCacheCollection::DeleteAllFiles() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Clear all managed persistent caches so they don't hold on to files or
  // prevent their deletion.
  persistent_caches_.Clear();

  backend_params_manager_->DeleteAllFiles();
}

PersistentCache* PersistentCacheCollection::GetOrCreateCache(
    const std::string& cache_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto it = persistent_caches_.Get(cache_id);

  // If the cache is already created.
  if (it != persistent_caches_.end()) {
    return it->second.get();
  }

  // Create the cache
  // TODO(crbug.com/377475540): Currently this class is deeply tied to the
  // sqlite implementation. Once the conversion to and from mojo types is
  // implemented this class should get a way to select the desired backend type.
  // TODO: Allow choosing the desired access rights.
  auto inserted_it = persistent_caches_.Put(
      cache_id,
      PersistentCache::Open(backend_params_manager_->GetOrCreateParamsSync(
          BackendType::kSqlite, cache_id,
          BackendParamsManager::AccessRights::kReadWrite)));
  return inserted_it->second.get();
}

}  // namespace persistent_cache