Skip to content

Commit e7cc31c

Browse files
committed
Rename CacheElement; LRUCache Logic Fixes
1 parent 0b1f86b commit e7cc31c

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

packages/optimizely-sdk/lib/core/odp/lru_cache/LRUCacheElement.tests.ts renamed to packages/optimizely-sdk/lib/core/odp/lru_cache/CacheElement.tests.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616

1717
import { assert } from 'chai'
18-
import { LRUCacheElement } from './LRUCacheElement';
18+
import { CacheElement } from './CacheElement';
1919

2020
const sleep = async (ms: number) => {
2121
return await new Promise(r => setTimeout(r, ms))
2222
}
2323

24-
describe('/odp/lru_cache/LRUCacheElement', () => {
25-
let element: LRUCacheElement<string>
24+
describe('/odp/lru_cache/CacheElement', () => {
25+
let element: CacheElement<string>
2626

2727
beforeEach(() => {
28-
element = new LRUCacheElement('foo')
28+
element = new CacheElement('foo')
2929
})
3030

31-
it('should initialize a valid LRUCacheElement', () => {
31+
it('should initialize a valid CacheElement', () => {
3232
assert.exists(element)
3333
assert.equal(element.value, 'foo')
3434
assert.isNotNull(element.time)

packages/optimizely-sdk/lib/core/odp/lru_cache/LRUCacheElement.ts renamed to packages/optimizely-sdk/lib/core/odp/lru_cache/CacheElement.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
/**
18-
* LRUCacheElement represents an individual generic item within the LRUCache
18+
* CacheElement represents an individual generic item within the LRUCache
1919
*/
20-
export class LRUCacheElement<V> {
20+
export class CacheElement<V> {
2121
private _value: V | null
2222
private _time: number
2323

@@ -35,4 +35,4 @@ export class LRUCacheElement<V> {
3535
}
3636
}
3737

38-
export default LRUCacheElement
38+
export default CacheElement

packages/optimizely-sdk/lib/core/odp/lru_cache/LRUCache.tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ describe('/lib/core/odp/lru_cache (Default)', () => {
182182

183183
await sleep(150)
184184

185-
assert.isNull(cache.peek('a'))
186-
assert.isNull(cache.peek('b'))
187-
assert.isNull(cache.peek('c'))
185+
assert.isNull(cache.lookup('a'))
186+
assert.isNull(cache.lookup('b'))
187+
assert.isNull(cache.lookup('c'))
188188

189189
cache.save({ key: 'd', value: 400 }) // { d: 400 }
190190
cache.save({ key: 'a', value: 101 }) // { d: 400, a: 101 }

packages/optimizely-sdk/lib/core/odp/lru_cache/LRUCache.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import LRUCacheElement from "./LRUCacheElement"
17+
import CacheElement from "./CacheElement"
1818

1919
/**
2020
* Least-Recently Used Cache (LRU Cache) Implementation with Generic Key-Value Pairs
@@ -23,17 +23,17 @@ import LRUCacheElement from "./LRUCacheElement"
2323
* - Removes stale elements (entries older than their timeout) from the cache.
2424
*/
2525
export class LRUCache<K, V> {
26-
private _map: Map<K, LRUCacheElement<V>> = new Map()
27-
private _maxSize = 100 // Defines maximum size of _map
28-
private _timeout = 1000 * 600 // Milliseconds each entry has before it becomes stale
26+
private _map: Map<K, CacheElement<V>> = new Map()
27+
private _maxSize // Defines maximum size of _map
28+
private _timeout // Milliseconds each entry has before it becomes stale
2929

30-
get map(): Map<K, LRUCacheElement<V>> { return this._map }
30+
get map(): Map<K, CacheElement<V>> { return this._map }
3131
get maxSize(): number { return this._maxSize }
3232
get timeout(): number { return this._timeout }
3333

34-
constructor({ maxSize, timeout }: { maxSize?: number, timeout?: number }) {
35-
if (maxSize != undefined) this._maxSize = maxSize
36-
if (timeout != undefined) this._timeout = timeout
34+
constructor({ maxSize, timeout }: { maxSize: number, timeout: number }) {
35+
this._maxSize = maxSize
36+
this._timeout = timeout
3737
}
3838

3939
/**
@@ -43,19 +43,18 @@ export class LRUCache<K, V> {
4343
public lookup(key: K): V | null {
4444
if (this._maxSize <= 0) { return null }
4545

46-
const element: LRUCacheElement<V> | undefined = this._map.get(key)
46+
const element: CacheElement<V> | undefined = this._map.get(key)
4747

4848
if (!element) return null
4949

50-
this._map.delete(key)
51-
this._map.set(key, element)
52-
53-
5450
if (element.is_stale(this._timeout)) {
5551
this._map.delete(key)
5652
return null
5753
}
5854

55+
this._map.delete(key)
56+
this._map.set(key, element)
57+
5958
return element.value
6059
}
6160

@@ -66,9 +65,9 @@ export class LRUCache<K, V> {
6665
public save({ key, value }: { key: K, value: V }): void {
6766
if (this._maxSize <= 0) return
6867

69-
const element: LRUCacheElement<V> | undefined = this._map.get(key)
68+
const element: CacheElement<V> | undefined = this._map.get(key)
7069
if (element) this._map.delete(key)
71-
this._map.set(key, new LRUCacheElement(value))
70+
this._map.set(key, new CacheElement(value))
7271

7372
if (this._map.size > this._maxSize) {
7473
const firstMapEntryKey = this._map.keys().next().value
@@ -87,18 +86,12 @@ export class LRUCache<K, V> {
8786

8887
/**
8988
* Reads value from specified key without moving elements in the LRU Cache.
90-
* Removes the element if it is stale.
9189
* @param {K} key
9290
*/
9391
public peek(key: K): V | null {
9492
if (this._maxSize <= 0) return null
9593

96-
const element: LRUCacheElement<V> | undefined = this._map.get(key)
97-
98-
if (element?.is_stale(this._timeout)) {
99-
this._map.delete(key)
100-
return null
101-
}
94+
const element: CacheElement<V> | undefined = this._map.get(key)
10295

10396
return element?.value ?? null
10497
}

0 commit comments

Comments
 (0)