Skip to content

Commit 7b53fa8

Browse files
authored
feat: Add ODP LRU Cache (#777)
1 parent da42162 commit 7b53fa8

File tree

6 files changed

+554
-0
lines changed

6 files changed

+554
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2022, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2022, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { assert } from 'chai'
18+
import { CacheElement } from './CacheElement';
19+
20+
const sleep = async (ms: number) => {
21+
return await new Promise(r => setTimeout(r, ms))
22+
}
23+
24+
describe('/odp/lru_cache/CacheElement', () => {
25+
let element: CacheElement<string>
26+
27+
beforeEach(() => {
28+
element = new CacheElement('foo')
29+
})
30+
31+
it('should initialize a valid CacheElement', () => {
32+
assert.exists(element)
33+
assert.equal(element.value, 'foo')
34+
assert.isNotNull(element.time)
35+
assert.doesNotThrow(() => element.is_stale(0))
36+
})
37+
38+
it('should return false if not stale based on timeout', () => {
39+
const timeoutLong = 1000
40+
assert.equal(element.is_stale(timeoutLong), false)
41+
})
42+
43+
it('should return false if not stale because timeout is less than or equal to 0', () => {
44+
const timeoutNone = 0
45+
assert.equal(element.is_stale(timeoutNone), false)
46+
})
47+
48+
it('should return true if stale based on timeout', async () => {
49+
await sleep(100)
50+
const timeoutShort = 1
51+
assert.equal(element.is_stale(timeoutShort), true)
52+
})
53+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2022, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* CacheElement represents an individual generic item within the LRUCache
19+
*/
20+
export class CacheElement<V> {
21+
private _value: V | null
22+
private _time: number
23+
24+
get value(): V | null { return this._value }
25+
get time(): number { return this._time }
26+
27+
constructor(value: V | null = null) {
28+
this._value = value
29+
this._time = Date.now()
30+
}
31+
32+
public is_stale(timeout: number): boolean {
33+
if (timeout <= 0) return false
34+
return Date.now() - this._time >= timeout
35+
}
36+
}
37+
38+
export default CacheElement

0 commit comments

Comments
 (0)