Skip to content

feat: Add ODP LRU Cache #777

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

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/optimizely-sdk/lib/core/odp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright 2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { assert } from 'chai'
import { CacheElement } from './CacheElement';

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

describe('/odp/lru_cache/CacheElement', () => {
let element: CacheElement<string>

beforeEach(() => {
element = new CacheElement('foo')
})

it('should initialize a valid CacheElement', () => {
assert.exists(element)
assert.equal(element.value, 'foo')
assert.isNotNull(element.time)
assert.doesNotThrow(() => element.is_stale(0))
})

it('should return false if not stale based on timeout', () => {
const timeoutLong = 1000
assert.equal(element.is_stale(timeoutLong), false)
})

it('should return false if not stale because timeout is less than or equal to 0', () => {
const timeoutNone = 0
assert.equal(element.is_stale(timeoutNone), false)
})

it('should return true if stale based on timeout', async () => {
await sleep(100)
const timeoutShort = 1
assert.equal(element.is_stale(timeoutShort), true)
})
})
38 changes: 38 additions & 0 deletions packages/optimizely-sdk/lib/core/odp/lru_cache/CacheElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* CacheElement represents an individual generic item within the LRUCache
*/
export class CacheElement<V> {
private _value: V | null
private _time: number

get value(): V | null { return this._value }
get time(): number { return this._time }

constructor(value: V | null = null) {
this._value = value
this._time = Date.now()
}

public is_stale(timeout: number): boolean {
if (timeout <= 0) return false
return Date.now() - this._time >= timeout
}
}

export default CacheElement
Loading