Skip to content

Commit

Permalink
improve caches and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinikjkkj committed Dec 19, 2024
1 parent 09734cd commit d21845d
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 374 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cachetools-js",
"version": "1.0.2",
"version": "1.0.3",
"description": "Best, faster, ligther, all-in-one, fully-typed cache library for JS, based on cachetools from python, with events, pooling and decorators.",
"author": "vinikjkkj",
"license": "MIT",
Expand All @@ -12,7 +12,7 @@
"prepare": "tsc",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"test": "jest",
"test": "jest --detectOpenHandles",
"publish": "npm run build && npm publish"
},
"devDependencies": {
Expand Down
218 changes: 111 additions & 107 deletions src/caches/cache.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,111 @@
import { CacheEmitter, SizeError } from '../utils'
import { CacheParams, Keyable } from '../types'

/**
* ### About
* Simple cache base class, based on `Proxy`, which allows getting and setting keys like a regular object.
*
* This class is useful if you want to create custom cache logic.
*
* It can also be used for personal purposes, although its specific application might not be obvious in all cases.
*
* ### Example
* ```typescript
* const cache = new Cache({maxsize: 2})
*
* //store some keys
* cache['foo'] = 'bar'
* cache['bar'] = 'foo'
*
* //get 'bar' key
* cache['bar']
* ```
*/
export class Cache extends CacheEmitter {
protected _cache: Map<Keyable, unknown>
protected _params: CacheParams

[key: string | symbol]: unknown

/**
* Creates a new Cache.
*/
constructor(params: CacheParams = {}){
super()
this._cache = new Map()
this._params = params

return new Proxy(this, {
get: (target, key) => {
if ((target as any)[key]) {
return (target as any)[key]
} else {
return target.get(key)
}
},
set: (target, key, value) => {
if (typeof key === 'string' && key.startsWith('_')) {
(target as any)[key] = value
} else {
target.set(key, value)
}

return true
}
});
}

get(key: Keyable){
this.emit('get', key)
return this._cache.get(key)
}

set(key: Keyable, value: unknown){
this.emit('set', { key, value })
if (
this._params.maxsize &&
this.length() === this._params.maxsize
){
throw new SizeError()
}
this._cache.set(key, this._params.useClones ?
structuredClone(value) :
value
)
}

take(key: Keyable){
const value = this.get(key)
this.del(key)
return value
}

del(key: Keyable){
this.emit('del', key)
this._cache.delete(key)
}

flushAll(){
this.delAll()
}

delAll(){
this._cache.clear()
}

keys(){
return Array.from(this._cache.keys())
}

values(){
return Array.from(this._cache.values())
}

length(){
return this._cache.size
}
}
import { CacheEmitter, SizeError } from '../utils'
import { CacheParams, Keyable } from '../types'

/**
* ### About
* Simple cache base class, based on `Proxy`, which allows getting and setting keys like a regular object.
*
* This class is useful if you want to create custom cache logic.
*
* It can also be used for personal purposes, although its specific application might not be obvious in all cases.
*
* ### Example
* ```typescript
* const cache = new Cache({maxsize: 2})
*
* //store some keys
* cache['foo'] = 'bar'
* cache['bar'] = 'foo'
*
* //get 'bar' key
* cache['bar']
* ```
*/
export class Cache extends CacheEmitter {
protected _cache: Map<Keyable, unknown>
protected _params: CacheParams

[key: string | symbol]: unknown

/**
* Creates a new Cache.
*/
constructor(params: CacheParams = {}){
super()
this._cache = new Map()
this._params = params

return new Proxy(this, {
get: (target, key) => {
if ((target as any)[key]) {
return (target as any)[key]
} else {
return target.get(key)
}
},
set: (target, key, value) => {
if (typeof key === 'string' && key.startsWith('_')) {
(target as any)[key] = value
} else {
target.set(key, value)
}

return true
}
});
}

get(key: Keyable){
this.emit('get', key)
return this._cache.get(key)
}

set(key: Keyable, value: unknown){
this.emit('set', { key, value })
if (
this._params.maxsize &&
this.length() === this._params.maxsize
){
throw new SizeError()
}
this._cache.set(key, this._params.useClones ?
structuredClone(value) :
value
)
}

take(key: Keyable){
const value = this.get(key)
this.del(key)
return value
}

del(key: Keyable){
this.emit('del', key)
this._cache.delete(key)
}

flushAll(){
this.delAll()
}

delAll(){
this._cache.clear()
}

keys(){
return Array.from(this._cache.keys())
}

values(){
return Array.from(this._cache.values())
}

length(){
return this._cache.size
}

destroy() {
this.delAll()
}
}
Loading

0 comments on commit d21845d

Please sign in to comment.