Skip to content
Permalink
Browse files

feat: LRUMemoCache (ex-@memoCache in js-lib)

  • Loading branch information...
kirillgroshkov committed May 12, 2019
1 parent 70ae029 commit 6da651babd0b5804c6ca79787201e737e520fd7f
Showing with 144 additions and 28 deletions.
  1. +6 −4 package.json
  2. +2 −0 src/index.ts
  3. +24 −0 src/util/__snapshots__/lruMemoCache.test.ts.snap
  4. +35 −0 src/util/lruMemoCache.test.ts
  5. +35 −0 src/util/lruMemoCache.ts
  6. +42 −24 yarn.lock
@@ -2,20 +2,22 @@
"name": "@naturalcycles/nodejs-lib",
"version": "5.1.0",
"dependencies": {
"@naturalcycles/js-lib": "^6.0.0",
"@types/hapi__joi": "^15.0.1",
"@types/lru-cache": "^5.1.0",
"lru-cache": "^5.1.1",
"luxon": "^1.13.2"
},
"peerDependencies": {
"@hapi/joi": "^15.0.0",
"@naturalcycles/js-lib": ">=5"
"@hapi/joi": "^15.0.0"
},
"devDependencies": {
"@hapi/joi": "^15.0.2",
"@naturalcycles/dev-lib": "^7.4.0",
"@naturalcycles/js-lib": "^5.0.1",
"@naturalcycles/semantic-release": "^1.0.0",
"@types/node": "^12.0.0",
"jest": "^24.8.0"
"jest": "^24.8.0",
"weak": "^1.0.1"
},
"files": [
"dist",
@@ -1,5 +1,6 @@
import { processSharedUtil } from './infra/process.shared.util'
import { requireEnvKeys } from './util/env.util'
import { LRUMemoCache } from './util/lruMemoCache'
import { unzipBuffer, unzipToString, zipBuffer, zipString } from './util/zip.util'
import { ExtendedJoi, Joi } from './validation/joi/joi.extensions'
import {
@@ -66,4 +67,5 @@ export {
zipString,
unzipToString,
requireEnvKeys,
LRUMemoCache,
}
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`memoCache a 1`] = `
[MockFunction] {
"calls": Array [
Array [
6,
],
Array [
12,
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
}
`;
@@ -0,0 +1,35 @@
import { memo } from '@naturalcycles/js-lib'
import { LRUMemoCache } from './lruMemoCache'

class A {
func (n: number): void {
console.log(`func ${n}`)
}

@memo({ cacheFactory: () => new LRUMemoCache({ maxAge: 100, max: 100 }) })
a (a1: number, a2: number): number {
const n = a1 * a2
this.func(n)
return n
}
}

test('memoCache a', () => {
const a = new A()
a.func = jest.fn()

// first call
let r = a.a(2, 3)
expect(r).toBe(6)
r = a.a(3, 4)
expect(r).toBe(12)

// second call
r = a.a(2, 3)
expect(r).toBe(6)
r = a.a(3, 4)
expect(r).toBe(12)

// to be called once per set of arguments (2)
expect(a.func).toMatchSnapshot()
})
@@ -0,0 +1,35 @@
import { MemoCache } from '@naturalcycles/js-lib'
import * as LRUCache from 'lru-cache'

export type LRUMemoCacheOpts = LRUCache.Options<string, any>

/**
* @example
* Use it like this:
*
* @memo({ cacheFactory: () => new LRUMemoCache({...}) })
* method1 ()
*/
export class LRUMemoCache implements MemoCache {
constructor (opt: LRUMemoCacheOpts) {
this.lru = new LRUCache<string, any>(opt)
}

private lru!: LRUCache<string, any>

has (k: any): boolean {
return this.lru.has(k)
}

get (k: any): any {
return this.lru.get(k)
}

set (k: any, v: any): void {
this.lru.set(k, v)
}

clear (): void {
this.lru.reset()
}
}
@@ -517,14 +517,12 @@
"@types/luxon" "^1.12.0"
lru-cache "^5.1.1"

"@naturalcycles/js-lib@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-5.0.1.tgz#65d55333de47c49b751dc90290dc5f59748b753a"
integrity sha512-okipp78GmYY+JvmOisqwwWsuojKmzLBqgHS2T75BxSJWUJgNkCZ8jieomUus4FVKnyNW9/g6bVLytAlxw7QUdQ==
"@naturalcycles/js-lib@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/js-lib/-/js-lib-6.0.0.tgz#f2f71bceb8ca8efe6e4aa040994408f8a589c718"
integrity sha512-YtaHn+byGaQnXgqtg/Rb2SqGFjRqqSQ/dK39BVEFcwMQmNy4LEaYYuhRdNpWoDwlLfq0ROWdxyDTYdMIrEHQ3g==
dependencies:
"@types/lru-cache" "^5.1.0"
"@types/luxon" "^1.12.0"
lru-cache "^5.1.1"

"@naturalcycles/promise-lib@^2.1.2":
version "2.2.0"
@@ -555,10 +553,10 @@
universal-user-agent "^2.0.1"
url-template "^2.0.8"

"@octokit/request@3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-3.0.2.tgz#dd2424691f486d7ea332ec06e3de55b1ea13c5dc"
integrity sha512-lBH2hf2Yuh9XlmP3MSpn3jL9DyCGG+cuPXDRQiJMK42BwW6xFhwWmG1k6xWykcLM4GwZG/5fuwcqnQXYG0ZTSg==
"@octokit/request@3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-3.0.3.tgz#ace63b5ea196cc00ad27f3fbe5c13a9698681ec8"
integrity sha512-M7pUfsiaiiUMEP4/SMysTeWxyGrkoQg6FBPEtCBIFgeDnzHaPboTpUZGTh6u1GQXdrlzMfPVn/vQs98js1QtwQ==
dependencies:
"@octokit/endpoint" "^5.1.0"
deprecation "^1.0.1"
@@ -568,11 +566,11 @@
universal-user-agent "^2.0.1"

"@octokit/rest@^16.13.1":
version "16.25.3"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.25.3.tgz#ce9e7a6230d20d58574ec929f622f2778ead7eb4"
integrity sha512-/6/Isn9pNoKUQwuWUaskxMC6kFxtXTHhzsgYbyirEQ3UvcLciHvPgtRTbuV3bbVf0x4+4WEfKaI9UzxmPQ3W3A==
version "16.25.4"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.25.4.tgz#4bc47c08e46ea522e79f4798605f11fa9aa8520d"
integrity sha512-62G8sb5PmIDhnu6K4W39If0BdA+P6VLuY83nMjjHyQiXH1QbjiKKeEv9uwRHe7XTcyZbVNG9M5zt5uxOpMW8CQ==
dependencies:
"@octokit/request" "3.0.2"
"@octokit/request" "3.0.3"
atob-lite "^2.0.0"
before-after-hook "^1.4.0"
btoa-lite "^1.0.0"
@@ -1227,6 +1225,13 @@ bin-links@^1.1.2:
graceful-fs "^4.1.11"
write-file-atomic "^2.3.0"

bindings@^1.2.1:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"

block-stream@*:
version "0.0.9"
resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a"
@@ -1240,9 +1245,9 @@ bluebird@^3.5.0, bluebird@^3.5.1, bluebird@^3.5.3:
integrity sha512-FG+nFEZChJrbQ9tIccIfZJBz3J7mLrAhxakAbnrJWn8d7aKOC+LWifa0G+p4ZqKp4y13T7juYvdhq9NzKdsrjw==

bottleneck@^2.0.1:
version "2.18.0"
resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.18.0.tgz#41fa63ae185b65435d789d1700334bc48222dacf"
integrity sha512-U1xiBRaokw4yEguzikOl0VrnZp6uekjpmfrh6rKtr1D+/jFjYCL6J83ZXlGtlBDwVdTmJJ+4Lg5FpB3xmLSiyA==
version "2.18.1"
resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.18.1.tgz#cf7c494d90283ecbe896baa46a1397ae8b3c19d7"
integrity sha512-EhSYARs0MHsNRBPrp1TaeHpgmWFUpA6yl3NNBPjGNilBaQZr4iSbrJ16JbQVXuZkIaB7YVYfaiMiRq7NgyZFQg==

boxen@^1.2.1:
version "1.3.0"
@@ -1632,9 +1637,9 @@ columnify@~1.5.4:
wcwidth "^1.0.0"

combined-stream@^1.0.6, combined-stream@~1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.7.tgz#2d1d24317afb8abe95d6d2c0b07b57813539d828"
integrity sha512-brWl9y6vOB1xYPZcpZde3N9zDByXTosAeMDo4p1wzo6UMOX4vumB+TP1RZ76sfE6Md68Q0NJSrE/gbezd4Ul+w==
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

@@ -2436,6 +2441,11 @@ figures@^2.0.0:
dependencies:
escape-string-regexp "^1.0.5"

file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==

fill-range@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7"
@@ -4779,7 +4789,7 @@ mute-stream@~0.0.4:
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==

nan@^2.12.1:
nan@^2.0.5, nan@^2.12.1:
version "2.13.2"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7"
integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw==
@@ -7057,9 +7067,9 @@ typescript@^3.4.5:
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==

uglify-js@^3.1.4:
version "3.5.11"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.11.tgz#833442c0aa29b3a7d34344c7c63adaa3f3504f6a"
integrity sha512-izPJg8RsSyqxbdnqX36ExpbH3K7tDBsAU/VfNv89VkMFy3z39zFjunQGsSHOlGlyIfGLGprGeosgQno3bo2/Kg==
version "3.5.12"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.12.tgz#6b759cabc08c3e91fe82323d6387019f0c5864cd"
integrity sha512-KeQesOpPiZNgVwJj8Ge3P4JYbQHUdZzpx6Fahy6eKAYRSV4zhVmLXoC+JtOeYxcHCHTve8RG1ZGdTvpeOUM26Q==
dependencies:
commander "~2.20.0"
source-map "~0.6.1"
@@ -7253,6 +7263,14 @@ wcwidth@^1.0.0:
dependencies:
defaults "^1.0.3"

weak@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/weak/-/weak-1.0.1.tgz#ab99aab30706959aa0200cb8cf545bb9cb33b99e"
integrity sha1-q5mqswcGlZqgIAy4z1RbucszuZ4=
dependencies:
bindings "^1.2.1"
nan "^2.0.5"

webidl-conversions@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

0 comments on commit 6da651b

Please sign in to comment.
You can’t perform that action at this time.