Skip to content

Commit

Permalink
Add Rache.from(cache) + rename class to Rache (#8)
Browse files Browse the repository at this point in the history
* Add Rache.from(cache) + rename class to Rache

* Use ternary
  • Loading branch information
HDegroote authored Jul 3, 2024
1 parent bfdf057 commit 9152a51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ Creates a new cache.

Creates a new cache which shares the global memory limit with the original cache.

#### `const aCache = Rache.from(cache?)`

Creates a new rache instance.

If an existing cache is passed in, it will create a sub-cache (equivalent to `aCache = cache.sub()`).

Otherwise (if no `cache` or a falsy cache is passed in), it will create a new cache (equivalent to `aCache = new Rache()`)


#### `cache.globalSize`

The amount of entries across all caches
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ class CacheValue {
}
}

class GlobalCache {
class Rache {
constructor ({ maxSize = 65536, parent = null } = {}) {
this.maxSize = parent?.maxSize || maxSize

this._array = parent?._array || []
this._map = new Map()
}

static from (cache) {
return cache ? new Rache({ parent: cache }) : new Rache()
}

get globalSize () {
return this._array.length
}
Expand All @@ -30,7 +34,7 @@ class GlobalCache {
}

sub () {
return new GlobalCache({ parent: this })
return new Rache({ parent: this })
}

set (key, value) { // ~constant time
Expand Down Expand Up @@ -112,4 +116,4 @@ class GlobalCache {
}
}

module.exports = GlobalCache
module.exports = Rache
33 changes: 21 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const test = require('brittle')
const GlobalCache = require('.')
const Rache = require('.')

test('basic set and get', t => {
const cache = new GlobalCache()
const cache = new Rache()
cache.set('key', 'value')
t.is(cache.get('key'), 'value', 'correct value')
})

test('set of existing value', t => {
const cache = new GlobalCache()
const cache = new Rache()
cache.set('key', 'value')
t.is(cache.get('key'), 'value', 'sanity check')

Expand All @@ -17,8 +17,17 @@ test('set of existing value', t => {
t.is(cache.size, 1, 'correct size')
})

test('creating with Rache.from', t => {
const cache = Rache.from()
t.pass('can create new cache with Rache.from')

const sub = Rache.from(cache)
sub.set('key', 'value')
t.is(cache.globalSize, 1, 'from links back to the global cache')
})

test('gc triggers when full and removes 1 entry', t => {
const cache = new GlobalCache({ maxSize: 3 })
const cache = new Rache({ maxSize: 3 })
cache.set('key', 'value')
cache.set('key2', 'value2')
cache.set('key3', 'value3')
Expand All @@ -34,7 +43,7 @@ test('gc triggers when full and removes 1 entry', t => {
})

test('subs share same cache, but no key conflicts', t => {
const cache = new GlobalCache({ maxSize: 3 })
const cache = new Rache({ maxSize: 3 })
const sub1 = cache.sub()
const sub2 = cache.sub()

Expand All @@ -54,7 +63,7 @@ test('subs share same cache, but no key conflicts', t => {
})

test('delete', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand Down Expand Up @@ -91,7 +100,7 @@ test('delete', t => {
})

test('iterator', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand All @@ -115,7 +124,7 @@ test('iterator', t => {
})

test('keys()', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand All @@ -128,7 +137,7 @@ test('keys()', t => {
})

test('values()', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand All @@ -141,7 +150,7 @@ test('values()', t => {
})

test('clear()', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand All @@ -166,7 +175,7 @@ test('clear()', t => {
})

test('destroy()', t => {
const cache = new GlobalCache()
const cache = new Rache()
const sub = cache.sub()

cache.set('key', 'value')
Expand All @@ -188,7 +197,7 @@ test('destroy()', t => {
})

test('internal structure remains consistent', t => {
const cache = new GlobalCache({ maxSize: 3 })
const cache = new Rache({ maxSize: 3 })

for (let i = 0; i < 1000; i++) {
cache.set(`key${i}`, `value${i}`)
Expand Down

0 comments on commit 9152a51

Please sign in to comment.