New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

brave-cache

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brave-cache - npm Package Compare versions

Comparing version 0.0.4 to 0.1.0

21

index.d.ts
import BraveCacheProvider from "./src/provider";
declare type BraveCacheOptions = {
prefix?: string | boolean;
prefixSeparator?: string;
};
declare class BraveCache<Client = any> {
provider: BraveCacheProvider<Client>;
constructor(provider?: string);
options: BraveCacheOptions;
constructor(provider?: string, options?: BraveCacheOptions);
/**
* Shorthand to initialize new cache instance
* @param provider
* @param options
*/
static useProvider<Client>(provider: string): BraveCache<Client>;
static useProvider<Client>(provider: string, options?: BraveCacheOptions): BraveCache<Client>;
/**

@@ -31,2 +37,9 @@ * Set default provider

/**
* Get key prefix if prefix is enabled
* @param key
* @private
*/
private prefix;
private get hasPrefix();
/**
* Get a value from the cache

@@ -131,7 +144,7 @@ * @param key The key to get

*/
keys(): string[];
keys(withPrefix?: boolean): string[];
/**
* Async: Get keys of items in the cache
*/
keysAsync(): Promise<string[]>;
keysAsync(withPrefix?: boolean): Promise<string[]>;
/**

@@ -138,0 +151,0 @@ * Remove all items from the cache

89

index.js

@@ -10,4 +10,6 @@ "use strict";

class BraveCache {
constructor(provider) {
constructor(provider, options) {
this.provider = BraveCache.getProvider(provider);
// Merge options
this.options = Object.assign({ prefix: false, prefixSeparator: ":" }, (options ? options : {}));
}

@@ -17,5 +19,6 @@ /**

* @param provider
* @param options
*/
static useProvider(provider) {
return new this(provider);
static useProvider(provider, options) {
return new this(provider, options);
}

@@ -66,2 +69,13 @@ /**

/**
* Get key prefix if prefix is enabled
* @param key
* @private
*/
prefix(key) {
return this.hasPrefix ? this.options.prefix + this.options.prefixSeparator + key : key;
}
get hasPrefix() {
return typeof this.options.prefix === "string" && this.options.prefix.trim().length > 0;
}
/**
* Get a value from the cache

@@ -73,3 +87,3 @@ * @param key The key to get

this.provider.hasFunctionOrThrowError("get");
let value = this.provider.functions.get(key);
let value = this.provider.functions.get(this.prefix(key));
if (value === undefined && def)

@@ -86,3 +100,3 @@ return (0, functions_1.bc_getDefaultValue)(def);

this.provider.hasFunctionOrThrowError("get");
let value = await this.provider.functions.get(key);
let value = await this.provider.functions.get(this.prefix(key));
if (value === undefined && def)

@@ -97,10 +111,19 @@ return (0, functions_1.bc_getDefaultValue)(def);

getMany(keys) {
let items = {};
if (this.provider.functions.getMany) {
return this.provider.functions.getMany(keys);
// Prefix keys
if (this.hasPrefix)
keys = keys.map((key) => this.prefix(key));
items = this.provider.functions.getMany(keys);
}
const value = {};
for (const key of keys) {
value[key] = this.get(key);
else {
for (const key of keys) {
items[this.prefix(key)] = this.get(key);
}
}
return value;
if (this.hasPrefix) {
// Remove prefix
items = (0, functions_1.bc_removePrefixFromObjectKeys)(items, this.options.prefix, this.options.prefixSeparator);
}
return items;
}

@@ -112,10 +135,19 @@ /**

async getManyAsync(keys) {
let items = {};
if (this.provider.functions.getMany) {
return this.provider.functions.getMany(keys);
// Prefix keys
if (this.hasPrefix)
keys = keys.map((key) => this.prefix(key));
items = (await this.provider.functions.getMany(keys));
}
const value = {};
for (const key of keys) {
value[key] = await this.getAsync(key);
else {
for (const key of keys) {
items[this.prefix(key)] = await this.getAsync(key);
}
}
return value;
if (this.hasPrefix) {
// Remove prefix
items = (0, functions_1.bc_removePrefixFromObjectKeys)(items, this.options.prefix, this.options.prefixSeparator);
}
return items;
}

@@ -155,3 +187,3 @@ /**

this.provider.hasFunctionOrThrowError("set");
this.provider.functions.set(key, value, ttl);
this.provider.functions.set(this.prefix(key), value, ttl);
return this;

@@ -167,3 +199,3 @@ }

this.provider.hasFunctionOrThrowError("set");
await this.provider.functions.set(key, value, ttl);
await this.provider.functions.set(this.prefix(key), value, ttl);
return this;

@@ -207,3 +239,3 @@ }

this.provider.hasFunctionOrThrowError("del");
this.provider.functions.del(key);
this.provider.functions.del(this.prefix(key));
return this;

@@ -217,3 +249,3 @@ }

this.provider.hasFunctionOrThrowError("del");
await this.provider.functions.del(key);
await this.provider.functions.del(this.prefix(key));
return this;

@@ -241,3 +273,3 @@ }

this.provider.hasFunctionOrThrowError("has");
return this.provider.functions.has(key);
return this.provider.functions.has(this.prefix(key));
}

@@ -254,5 +286,9 @@ /**

*/
keys() {
keys(withPrefix = false) {
this.provider.hasFunctionOrThrowError("keys");
return this.provider.functions.keys();
const keys = this.provider.functions.keys();
if (this.hasPrefix && !withPrefix) {
return keys.map((key) => key.replace(this.options.prefix + this.options.prefixSeparator, ""));
}
return keys;
}

@@ -262,4 +298,9 @@ /**

*/
async keysAsync() {
return this.keys();
async keysAsync(withPrefix = false) {
this.provider.hasFunctionOrThrowError("keys");
const keys = await this.provider.functions.keys();
if (this.hasPrefix && !withPrefix) {
return keys.map((key) => key.replace(this.options.prefix + this.options.prefixSeparator, ""));
}
return keys;
}

@@ -266,0 +307,0 @@ /**

{
"name": "brave-cache",
"version": "0.0.4",
"version": "0.1.0",
"description": "A flexible semantic Api for handling multiple node cache drivers.",

@@ -20,2 +20,3 @@ "main": "index.js",

"@types/node": "^17.0.6",
"@types/random-words": "^1.1.2",
"japa": "^4.0.0",

@@ -26,2 +27,3 @@ "lru-cache": "^6.0.0",

"prettier": "^2.5.1",
"random-words": "^1.1.1",
"ts-node": "^10.4.0",

@@ -28,0 +30,0 @@ "typescript": "^4.5.4"

@@ -6,2 +6,9 @@ /**

import LRUCache from "lru-cache";
/**
* LRUCacheProvider
* @example
* const cache = new BraveCache("lru-cache");
* @param options
* @constructor
*/
export default function LRUCacheProvider(options?: LRUCache.Options<string, any>): BraveCacheProvider<LRUCache<string, any>>;

@@ -11,2 +11,9 @@ "use strict";

const lru_cache_1 = __importDefault(require("lru-cache"));
/**
* LRUCacheProvider
* @example
* const cache = new BraveCache("lru-cache");
* @param options
* @constructor
*/
function LRUCacheProvider(options = {}) {

@@ -13,0 +20,0 @@ const cache = new lru_cache_1.default(Object.assign({ max: 0, maxAge: 1000 * 60 * 60 * 24 * 7 }, options));

@@ -6,2 +6,9 @@ /**

import NodeCache from "node-cache";
/**
* Node Cache Provider
* @example
* const cache = new BraveCache("node-cache");
* @param options
* @constructor
*/
export default function NodeCacheProvider(options?: NodeCache.Options): BraveCacheProvider<NodeCache>;

@@ -11,2 +11,9 @@ "use strict";

const node_cache_1 = __importDefault(require("node-cache"));
/**
* Node Cache Provider
* @example
* const cache = new BraveCache("node-cache");
* @param options
* @constructor
*/
function NodeCacheProvider(options = {}) {

@@ -13,0 +20,0 @@ // Create a new instance of the Node Cache

@@ -5,2 +5,10 @@ /**

import BraveCacheProvider from "../src/provider";
export default function ObjectCacheProvider(data?: Record<string, any>): BraveCacheProvider<import("object-collection")<Record<string, any>>>;
import ObjectCollection from "object-collection";
/**
* Memory cache using object collection.
* @example
* const cache = new BraveCache("object-cache");
* @param data
* @constructor
*/
export default function ObjectCacheProvider(data?: Record<string, any>): BraveCacheProvider<ObjectCollection<Record<string, any>>>;

@@ -10,5 +10,12 @@ "use strict";

const provider_1 = __importDefault(require("../src/provider"));
const exports_1 = require("object-collection/exports");
const object_collection_1 = __importDefault(require("object-collection"));
/**
* Memory cache using object collection.
* @example
* const cache = new BraveCache("object-cache");
* @param data
* @constructor
*/
function ObjectCacheProvider(data) {
const cache = (0, exports_1.Obj)(data || {});
const cache = new object_collection_1.default(data || {});
return new provider_1.default({

@@ -15,0 +22,0 @@ name: "object-cache",

@@ -85,3 +85,3 @@ # Brave Cache

// Create Cache, it will use LRUCache
const cache = new BraveCache("lru-cache");
const cache = new BraveCache();
cache.set("test", "test");

@@ -88,0 +88,0 @@

@@ -6,1 +6,8 @@ /**

export declare function bc_getDefaultValue<T>(value: T | (() => T)): T;
/**
* Remove prefix from object keys
* @param prefix
* @param prefixSeparator
* @param items
*/
export declare function bc_removePrefixFromObjectKeys(items: Record<string, any>, prefix: string, prefixSeparator: string): Record<string, any>;
"use strict";
// bc is a shorthand for `BraveCache`
Object.defineProperty(exports, "__esModule", { value: true });
exports.bc_getDefaultValue = void 0;
exports.bc_removePrefixFromObjectKeys = exports.bc_getDefaultValue = void 0;
/**

@@ -16,1 +16,15 @@ * Get value from function or return default value

exports.bc_getDefaultValue = bc_getDefaultValue;
/**
* Remove prefix from object keys
* @param prefix
* @param prefixSeparator
* @param items
*/
function bc_removePrefixFromObjectKeys(items, prefix, prefixSeparator) {
for (const key in items) {
items[key.replace(prefix + prefixSeparator, "")] = items[key];
delete items[key];
}
return items;
}
exports.bc_removePrefixFromObjectKeys = bc_removePrefixFromObjectKeys;
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