Skip to content
Closed
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
40 changes: 40 additions & 0 deletions Cache/TimeCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* The TimeCache class is a JavaScript class that allows you to store key-value pairs in a cache with
an optional expiration time. */
class TimeCache {
constructor(expirationTime) {
this.cache = new Map()
this.expirationTime = expirationTime
}

set(key, value, keyExpirationTime = false) {
let expiration
if (
this.expirationTime < keyExpirationTime ||
keyExpirationTime === false
) {
expiration = Date.now() + this.expirationTime
} else {
expiration = Date.now() + keyExpirationTime
}
this.cache.set(key, { value, expiration })
}

get(key) {
const cacheItem = this.cache.get(key)
if (!cacheItem) {
return null
}
const { value, expiration } = cacheItem
if (expiration && Date.now() > expiration) {
this.delete(key)
return null
}
return value
}

delete(key) {
this.cache.delete(key)
}
}

export default TimeCache
66 changes: 66 additions & 0 deletions Cache/test/TimeCache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import TimeCache from '../TimeCache'
describe('TimeCache', () => {
let timeCache

beforeEach(() => {
timeCache = new TimeCache()
})

it('should set and get a value from the cache', () => {
timeCache.set('key', 'value')
expect(timeCache.get('key')).toBe('value')
})

it('should delete a value from the cache', () => {
timeCache.set('key', 'value')
timeCache.delete('key')
expect(timeCache.get('key')).toBeNull()
})

it('should return null for a non-existent key', () => {
expect(timeCache.get('non-existent-key')).toBeNull()
})

it('should return null for an expired key', (done) => {
timeCache.set('key', 'value', Date.now() - 500)
setTimeout(() => {
expect(timeCache.get('key')).toBeNull()
done()
}, 1000)
})

it('should return null for an expired key with a default expiration time', (done) => {
timeCache = new TimeCache(500)
timeCache.set('key', 'value')
setTimeout(() => {
expect(timeCache.get('key')).toBeNull()
done()
}, 1000)
})

it('should return null for an expired key with a custom expiration time', (done) => {
timeCache.set('key', 'value', 500)
setTimeout(() => {
expect(timeCache.get('key')).toBeNull()
done()
}, 1000)
})

it('should return null for an expired key with a custom expiration time that is less than the default expiration time', (done) => {
timeCache = new TimeCache(5000)
timeCache.set('key', 'value', 1000)
setTimeout(() => {
expect(timeCache.get('key')).toBeNull()
done()
}, 2000)
})

it('should return null for an expired key with a custom expiration time that is greater than the default expiration time', (done) => {
timeCache = new TimeCache(1000)
timeCache.set('key', 'value', 5000)
setTimeout(() => {
expect(timeCache.get('key')).toBeNull()
done()
}, 2000)
})
})