-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
84 deletions.
There are no files selected for viewing
90 changes: 48 additions & 42 deletions
90
components/brave_shields/browser/https_everywhere_recently_used_cache.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,63 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
/* Copyright 2016 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
#include <vector> | ||
|
||
template <class T> class RingBuffer | ||
{ | ||
private: | ||
int currentIdx = 0; | ||
int count; | ||
std::vector<T> data; | ||
public: | ||
RingBuffer(int fixedSize) : count(fixedSize), data(count) {} | ||
|
||
const T& at(int i) { | ||
return data[(currentIdx - (i % count) + count) % count]; | ||
} | ||
template <class T> class RingBuffer { | ||
public: | ||
explicit RingBuffer(int fixedSize) : count(fixedSize), data(count) {} | ||
|
||
void add(const T& newValue) { | ||
currentIdx = (currentIdx + 1) % count; | ||
data[currentIdx] = newValue; | ||
} | ||
const T& at(int i) { | ||
return data[(currentIdx - (i % count) + count) % count]; | ||
} | ||
|
||
T oldest() { | ||
return data[(currentIdx + 1) % count]; | ||
} | ||
void add(const T& newValue) { | ||
currentIdx = (currentIdx + 1) % count; | ||
data[currentIdx] = newValue; | ||
} | ||
|
||
void clear() { | ||
data = std::vector<T>(count); | ||
} | ||
T oldest() { | ||
return data[(currentIdx + 1) % count]; | ||
} | ||
|
||
void clear() { | ||
data = std::vector<T>(count); | ||
} | ||
|
||
private: | ||
int currentIdx = 0; | ||
int count; | ||
std::vector<T> data; | ||
}; | ||
|
||
template <class T> class HTTPSERecentlyUsedCache | ||
{ | ||
private: | ||
RingBuffer<T> keysByAge; | ||
public: | ||
std::unordered_map<std::string, T> data; | ||
|
||
HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} | ||
|
||
void add(const std::string& key, const T& value) { | ||
std::string old = keysByAge.oldest(); | ||
if (!old.empty()) { | ||
keysByAge.data.erase(old); | ||
} | ||
keysByAge[key] = value; | ||
} | ||
template <class T> class HTTPSERecentlyUsedCache { | ||
public: | ||
explicit HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} | ||
|
||
void clear() { | ||
data.clear(); | ||
keysByAge.clear(); | ||
void add(const std::string& key, const T& value) { | ||
std::string old = keysByAge.oldest(); | ||
if (!old.empty()) { | ||
keysByAge.data.erase(old); | ||
} | ||
keysByAge[key] = value; | ||
} | ||
|
||
void clear() { | ||
data.clear(); | ||
keysByAge.clear(); | ||
} | ||
|
||
std::unordered_map<std::string, T> data; | ||
|
||
private: | ||
RingBuffer<T> keysByAge; | ||
}; | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters