-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from brave/rewards-twitter-fix-excessive-api-c…
…alls Greaselion makes excessive calls to Twitter API
- Loading branch information
Showing
4 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* 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/. */ | ||
|
||
export class LruCache<T> { | ||
|
||
private values: Map<string, T> = new Map<string, T>() | ||
private maxEntries: number | ||
|
||
constructor (maxEntries: number) { | ||
this.maxEntries = maxEntries | ||
} | ||
|
||
get (key: string): T | null { | ||
if (!key) { | ||
return null | ||
} | ||
|
||
const entry = this.values.get(key) | ||
if (!entry) { | ||
return null | ||
} | ||
|
||
this.values.delete(key) | ||
this.values.set(key, entry) | ||
|
||
return entry | ||
} | ||
|
||
put (key: string, value: T) { | ||
if (this.values.size >= this.maxEntries) { | ||
const key = this.values.keys().next().value | ||
this.values.delete(key) | ||
} | ||
|
||
this.values.set(key, value) | ||
} | ||
} |
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
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
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