Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-cache-dedupe

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-cache-dedupe - npm Package Compare versions

Comparing version 1.9.0 to 1.10.0

test/transformer.test.js

9

index.d.ts

@@ -29,2 +29,7 @@ import { Redis } from "ioredis";

interface DataTransformer {
serialize: (data: any) => any;
deserialize: (data: any) => any;
}
type Events = {

@@ -67,2 +72,4 @@ onDedupe?: (key: string) => void;

ttl?: number;
transformer?: DataTransformer;
stale?: number;
} & Events

@@ -84,3 +91,5 @@ ): Cache;

storage?: StorageOptionsType;
transformer?: DataTransformer;
ttl?: number;
stale?: number;
serialize?: (...args: any[]) => any;

@@ -87,0 +96,0 @@ references?: (...args: any[]) => References | Promise<References>;

12

index.test-d.ts

@@ -26,2 +26,8 @@ // Write a tsd file for the module

const cacheWithTtlAndStale = createCache({
ttl: 1000,
stale: 1000,
});
expectType<Cache>(cacheWithTtlAndStale);
// Testing Union Types

@@ -36,2 +42,3 @@

fetchSomething: typeof fetchSomething;
fetchSomethingElse: typeof fetchSomething;
};

@@ -50,2 +57,5 @@

unionMemoryCache.define("fetchSomethingElse", { ttl: 1000, stale: 1000}, fetchSomething);
expectType<typeof fetchSomething>(unionMemoryCache.fetchSomethingElse);
const result = await unionMemoryCache.fetchSomething("test");

@@ -55,2 +65,2 @@ expectType<{ k: any }>(result);

await unionMemoryCache.invalidateAll("test:*");
await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");
await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");

2

package.json
{
"name": "async-cache-dedupe",
"version": "1.9.0",
"version": "1.10.0",
"description": "An async deduping cache",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -86,3 +86,22 @@ # async-cache-dedupe

```
* `transformer`: the transformer to used to serialize and deserialize the cache entries.
It must be an object with the following methods:
* `serialize`: a function that receives the result of the original function and returns a serializable object.
* `deserialize`: a function that receives the serialized object and returns the original result.
* Default is `undefined`, so the default transformer is used.
Example
```js
import superjson from 'superjson';
const cache = createCache({
transformer: {
serialize: (result) => superjson.serialize(result),
deserialize: (serialized) => superjson.deserialize(serialized),
}
})
```
### `cache.define(name[, opts], original(arg, cacheKey))`

@@ -105,2 +124,3 @@

* `storage`: the storage to use, same as above. It's possible to specify different storages for each defined function for fine-tuning.
* `transformer`: the transformer to used to serialize and deserialize the cache entries. It's possible to specify different transformers for each defined function for fine-tuning.
* `references`: sync or async function to generate references, it receives `(args, key, result)` from the defined function call and must return an array of strings or falsy; see [invalidation](#invalidation) to know how to use them.

@@ -107,0 +127,0 @@

'use strict'
const { kValues, kStorage, kStorages, kTTL, kOnDedupe, kOnError, kOnHit, kOnMiss, kStale } = require('./symbol')
const { kValues, kStorage, kStorages, kTransfromer, kTTL, kOnDedupe, kOnError, kOnHit, kOnMiss, kStale } = require('./symbol')
const stringify = require('safe-stable-stringify')

@@ -11,2 +11,3 @@ const createStorage = require('./storage')

* @param {!Storage} opts.storage - the storage to use
* @param {?Object} opts.transformer - the transformer to use
* @param {?number} [opts.ttl=0] - in seconds; default is 0 seconds, so it only does dedupe without cache

@@ -55,2 +56,4 @@ * @param {?function} opts.onDedupe

this[kTransfromer] = options.transformer
this[kTTL] = options.ttl || 0

@@ -69,2 +72,3 @@ this[kOnDedupe] = options.onDedupe || noop

* @param {?Object} [opts.storage] storage to use; default is the main one
* @param {?Object} opts.transformer - the transformer to use
* @param {?number} [opts.ttl] ttl for the results; default ttl is the one passed to the constructor

@@ -125,4 +129,5 @@ * @param {?function} [opts.onDedupe] function to call on dedupe; default is the one passed to the constructor

const onMiss = opts.onMiss || this[kOnMiss]
const transformer = opts.transformer || this[kTransfromer]
const wrapper = new Wrapper(func, name, serialize, references, storage, ttl, onDedupe, onError, onHit, onMiss, stale)
const wrapper = new Wrapper(func, name, serialize, references, storage, transformer, ttl, onDedupe, onError, onHit, onMiss, stale)

@@ -194,2 +199,3 @@ this[kValues][name] = wrapper

* @param {Storage} storage
* @param {Object} transformer
* @param {number} ttl

@@ -202,3 +208,3 @@ * @param {function} onDedupe

*/
constructor (func, name, serialize, references, storage, ttl, onDedupe, onError, onHit, onMiss, stale) {
constructor (func, name, serialize, references, storage, transformer, ttl, onDedupe, onError, onHit, onMiss, stale) {
this.dedupes = new Map()

@@ -211,2 +217,3 @@ this.func = func

this.storage = storage
this.transformer = transformer
this.ttl = ttl

@@ -258,4 +265,3 @@ this.onDedupe = onDedupe

if (this.ttl > 0 || typeof this.ttl === 'function') {
let data = this.storage.get(storageKey)
if (data && typeof data.then === 'function') { data = await data }
const data = await this.get(storageKey)

@@ -292,6 +298,3 @@ if (data !== undefined) {

if (!this.references) {
let p = this.storage.set(storageKey, result, ttl)
if (p && typeof p.then === 'function') {
p = await p
}
await this.set(storageKey, result, ttl)
return result

@@ -346,6 +349,13 @@ }

async get (key) {
return this.storage.get(key)
const data = await this.storage.get(key)
if (this.transformer && !!data) {
return await this.transformer.deserialize(data)
}
return data
}
async set (key, value, ttl, references) {
if (this.transformer) {
value = this.transformer.serialize(value)
}
return this.storage.set(key, value, ttl, references)

@@ -352,0 +362,0 @@ }

@@ -6,2 +6,3 @@ 'use strict'

const kStorages = Symbol('kStorages')
const kTransfromer = Symbol('kTransformer')
const kTTL = Symbol('kTTL')

@@ -14,2 +15,2 @@ const kOnDedupe = Symbol('kOnDedupe')

module.exports = { kValues, kStorage, kStorages, kTTL, kOnDedupe, kOnError, kOnHit, kOnMiss, kStale }
module.exports = { kValues, kStorage, kStorages, kTransfromer, kTTL, kOnDedupe, kOnError, kOnHit, kOnMiss, kStale }
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc