Skip to content

Commit 5c7f173

Browse files
amabluea-maurice
authored andcommitted
Added a CachePolicy class, which can be used to configure how aggressively the
cache should be pruned. This will be used in an upcoming CL to help remove inactive cached data. PiperOrigin-RevId: 280536975
1 parent 07ab358 commit 5c7f173

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "database/src/desktop/core/cache_policy.h"
16+
17+
#include <cstdint>
18+
#include <limits>
19+
20+
namespace firebase {
21+
namespace database {
22+
namespace internal {
23+
24+
const uint64_t kServerUpdatesBetweenCacheSizeChecks = 1000;
25+
26+
const uint64_t kMaxNumberOfPrunableQueriesToKeep = 1000;
27+
28+
// 20% at a time until we're below our max.
29+
const double kPercentOfQueriesToPruneAtOnce = 0.2;
30+
31+
CachePolicy::~CachePolicy() {}
32+
33+
LRUCachePolicy::LRUCachePolicy(uint64_t max_size_bytes)
34+
: max_size_bytes_(max_size_bytes) {}
35+
36+
LRUCachePolicy::~LRUCachePolicy() {}
37+
38+
bool LRUCachePolicy::ShouldPrune(uint64_t current_size_bytes,
39+
uint64_t count_of_prunable_queries) const {
40+
return current_size_bytes > max_size_bytes_ ||
41+
count_of_prunable_queries > kMaxNumberOfPrunableQueriesToKeep;
42+
}
43+
44+
bool LRUCachePolicy::ShouldCheckCacheSize(
45+
uint64_t server_updates_since_last_check) const {
46+
return server_updates_since_last_check > kServerUpdatesBetweenCacheSizeChecks;
47+
}
48+
49+
double LRUCachePolicy::GetPercentOfQueriesToPruneAtOnce() const {
50+
return kPercentOfQueriesToPruneAtOnce;
51+
}
52+
53+
uint64_t LRUCachePolicy::GetMaxNumberOfQueriesToKeep() const {
54+
return kMaxNumberOfPrunableQueriesToKeep;
55+
}
56+
57+
} // namespace internal
58+
} // namespace database
59+
} // namespace firebase
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_CORE_CACHE_POLICY_H_
16+
#define FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_CORE_CACHE_POLICY_H_
17+
18+
#include <cstdint>
19+
20+
namespace firebase {
21+
namespace database {
22+
namespace internal {
23+
24+
class CachePolicy {
25+
public:
26+
virtual ~CachePolicy();
27+
28+
virtual bool ShouldPrune(uint64_t current_size_bytes,
29+
uint64_t count_of_prunable_queries) const = 0;
30+
31+
virtual bool ShouldCheckCacheSize(
32+
uint64_t serverUpdatesSinceLastCheck) const = 0;
33+
34+
virtual double GetPercentOfQueriesToPruneAtOnce() const = 0;
35+
36+
virtual uint64_t GetMaxNumberOfQueriesToKeep() const = 0;
37+
};
38+
39+
class LRUCachePolicy : public CachePolicy {
40+
public:
41+
explicit LRUCachePolicy(uint64_t max_size_bytes);
42+
43+
~LRUCachePolicy() override;
44+
45+
bool ShouldPrune(uint64_t current_size_bytes,
46+
uint64_t count_of_prunable_queries) const override;
47+
48+
bool ShouldCheckCacheSize(
49+
uint64_t server_updates_since_last_check) const override;
50+
51+
double GetPercentOfQueriesToPruneAtOnce() const override;
52+
53+
uint64_t GetMaxNumberOfQueriesToKeep() const override;
54+
55+
private:
56+
const uint64_t max_size_bytes_;
57+
};
58+
59+
} // namespace internal
60+
} // namespace database
61+
} // namespace firebase
62+
63+
#endif // FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_CORE_CACHE_POLICY_H_

0 commit comments

Comments
 (0)