-
Notifications
You must be signed in to change notification settings - Fork 0
Add cache strategies with eviction policy #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielM08
wants to merge
6
commits into
master
Choose a base branch
from
addCacheStrategiesWithEvictionPolicy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
139fe89
chore: add baseCache class accepting capacity and evicting cached items
DanielM08 cb07981
feat: add fifo cache strategy
DanielM08 369bb4a
feat: add LRU cache strategy
DanielM08 38d0423
feat: add MRU cache strategy
DanielM08 799426e
feat: support configurable evictionPolicy to select cache strategy
DanielM08 a77c2d8
chore: update readme
DanielM08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
| import { | ||
| EvictionRememberedConfig, | ||
| RememberedConfig, | ||
| } from '../remembered-config'; | ||
| import { FifoCache, LruCache, MruCache, SimpleCache } from './implementations'; | ||
|
|
||
| export function createCache<TResponse = unknown, TKey = string>( | ||
| config: RememberedConfig<TResponse, TKey>, | ||
| ) { | ||
| switch (config.evictionPolicy) { | ||
| case 'LRU': | ||
| const lruConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new LruCache<TResponse, TKey>(lruConfig.capacity); | ||
| case 'FIFO': | ||
| const fifoConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new FifoCache<TResponse, TKey>(fifoConfig.capacity); | ||
| case 'MRU': | ||
| const mruConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new MruCache<TResponse, TKey>(mruConfig.capacity); | ||
| default: | ||
| return new SimpleCache<TResponse, TKey>(); | ||
| } | ||
| } |
This file contains hidden or 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,68 @@ | ||
| import { Cache } from './cache'; | ||
| import { Node, LinkedList } from '../utils/linked-list'; | ||
|
|
||
| export abstract class BaseCache<TResponse = unknown, TKey = string> | ||
| implements Cache<TResponse, TKey> | ||
| { | ||
| protected map: Map<TKey, Node<TResponse, TKey>>; | ||
| protected linkedList: LinkedList<TResponse, TKey>; | ||
| protected size: number; | ||
| protected readonly capacity: number; | ||
|
|
||
| constructor(capacity: number) { | ||
| if (capacity < 0) { | ||
| throw new Error('Capacity must be greater than or equal to 0'); | ||
| } | ||
|
|
||
| this.map = new Map<TKey, Node<TResponse, TKey>>(); | ||
| this.linkedList = new LinkedList<TResponse, TKey>(); | ||
|
|
||
| this.capacity = capacity; | ||
| this.size = 0; | ||
| } | ||
|
|
||
| abstract get(key: TKey): TResponse | Promise<TResponse> | undefined; | ||
|
|
||
| set(key: TKey, value: TResponse | Promise<TResponse>): void { | ||
| if (this.capacity === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const item = this.map.get(key); | ||
| if (item) { | ||
| this.handleExistingItemAccess(item, value); | ||
| return; | ||
| } | ||
|
|
||
| if (this.size === this.capacity) { | ||
| this.evictItem(); | ||
| } | ||
|
|
||
| const node = this.linkedList.addNode(key, value); | ||
| this.size++; | ||
| this.map.set(key, node); | ||
| } | ||
|
|
||
| delete(key: TKey): void { | ||
| const item = this.map.get(key); | ||
| if (item) { | ||
| this.linkedList.removeNode(item); | ||
| this.map.delete(key); | ||
| this.size--; | ||
| } | ||
| } | ||
|
|
||
| protected abstract handleExistingItemAccess( | ||
| item: Node<TResponse, TKey>, | ||
| value: TResponse | Promise<TResponse>, | ||
| ): void; | ||
| protected abstract evictItem(): void; | ||
|
|
||
| protected getHead(): Node<TResponse, TKey> { | ||
| return this.linkedList.getHead(); | ||
| } | ||
|
|
||
| protected getTail(): Node<TResponse, TKey> { | ||
| return this.linkedList.getTail(); | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| import { | ||
| EvictionRememberedConfig, | ||
| RememberedConfig, | ||
| } from '../../remembered-config'; | ||
| import { FifoCache } from '../implementations/fifo-cache'; | ||
| import { LruCache } from '../implementations/lru-cache'; | ||
| import { MruCache } from '../implementations/mru-cache'; | ||
| import { SimpleCache } from '../implementations/simple-cache'; | ||
|
|
||
| export function createCache<TResponse = unknown, TKey = string>( | ||
| config: RememberedConfig<TResponse, TKey>, | ||
| ) { | ||
| switch (config.evictionPolicy) { | ||
| case 'LRU': | ||
| const lruConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new LruCache<TResponse, TKey>(lruConfig.capacity); | ||
| case 'FIFO': | ||
| const fifoConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new FifoCache<TResponse, TKey>(fifoConfig.capacity); | ||
| case 'MRU': | ||
| const mruConfig = config as EvictionRememberedConfig<TResponse, TKey>; | ||
| return new MruCache<TResponse, TKey>(mruConfig.capacity); | ||
| default: | ||
| return new SimpleCache<TResponse, TKey>(); | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| export interface Cache<TResponse = unknown, TKey = string> { | ||
| get(key: TKey): TResponse | Promise<TResponse> | undefined; | ||
| set(key: TKey, value: TResponse | Promise<TResponse>): void; | ||
| delete(key: TKey): void; | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| import { BaseCache } from '../core/base-cache'; | ||
| import { Node } from '../utils/linked-list'; | ||
|
|
||
| export class FifoCache<TResponse = unknown, TKey = string> extends BaseCache< | ||
| TResponse, | ||
| TKey | ||
| > { | ||
| get(key: TKey): TResponse | Promise<TResponse> | undefined { | ||
| const item = this.map.get(key); | ||
| if (item) { | ||
| return item.value; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| protected handleExistingItemAccess( | ||
| item: Node<TResponse, TKey>, | ||
| value: TResponse | Promise<TResponse>, | ||
| ): void { | ||
| item.value = value; | ||
| } | ||
|
|
||
| protected evictItem(): void { | ||
| const nodeToRemove = this.getTail().prev; | ||
| if (nodeToRemove) { | ||
| this.linkedList.removeNode(nodeToRemove); | ||
| if (nodeToRemove.key) { | ||
| this.map.delete(nodeToRemove.key); | ||
| } | ||
| } | ||
| this.size--; | ||
| } | ||
| } |
This file contains hidden or 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,4 @@ | ||
| export { SimpleCache } from './simple-cache'; | ||
| export { FifoCache } from './fifo-cache'; | ||
| export { LruCache } from './lru-cache'; | ||
| export { MruCache } from './mru-cache'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to keep the idiomatic around the Map word, instead of cache, to prevent confusion with dedicated cache services.
You can call it, for example, EvictionableMap. I also recommend you to invert the class generic parameters, to make it's contract compatible with Map<TKey, TResponse | Promise>