Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
brave-cache
Advanced tools
Brave Cache is a simple caching library manager for Nodejs.
NodeJs Ecosystem already has lots of caching libraries and this is not one of them, but they are not merge-able and consists of different apis. Brave Cache supports adding multiple cache providers/drivers while keeping same api.
npm install brave-cache
# OR
yarn add brave-cache
Out of the box, BraveCache provides supports for the following cache libraries:
lru-cache
)node-cache
)The default Provider is ObjectCacheProvider
which uses object-collection
to store the cache data in an object.
It has been registered by default.
const BraveCache = require("brave-cache");
const LRUCacheProvider = require("brave-cache/providers/lru-cache");
// Register a Provider
BraveCache.registerProvider(
LRUCacheProvider({
/* LRU Cache Options */
})
);
// Set as default Provider
BraveCache.setDefaultProvider("lru-cache");
// Create Cache, it will use LRUCache
const cache = new BraveCache();
cache.set("test", "test");
console.log(cache.get("test"));
// You can also access the provider i.e lru-cache directly
cache.provider.client.set("direct", "direct"); // => LRUCache
console.log(cache.get("direct")); // => direct
console.log(cache.provider.client.get("direct")); // => direct
These are the functions available on the cache instance. Unlike other cache packages, all instance methods come in both sync
and async
versions.
Note: Async
methods will only work if your provider supports async operations. most cache libraries do not support async operations.
Example of a cache instance.
const cache = new BraveCache();
// or specify a provider
const cache = new BraveCache("object-cache");
Get a value from the cache.
cache.set("key", "value");
cache.get("key"); // => "value"
cache.get("non existent key"); // => undefined
// cache with default value
cache.get("non existent key", "default value"); // => "default value"
// Or with default value computed from a function
cache.get("non existent key", () => "default value"); // => "default value"
Async version of get()
, Includes all the functionalities of get()
with extra capability to resolve promises or functions that return promises.
// Includes all the functionality of get()
// Or with default value as a promise
await cache.getAsync("non existent key", Promise.resolve("default value")); // => "default value"
// Or with default value computed from a function
await cache.getAsync("non existent key", () => "default value"); // => "default value"
// Or with default value computed from a function that returns a promise
await cache.getAsync("non existent key", () => Promise.resolve("default value")); // => "default value"
Get multiple values at once. keys that do not exist will be ignored.
// Set Many
cache.setMany([
["key1", "value1"],
["key2", "value2"],
["key3", "value3"]
]);
let test = cache.getMany(["key1", "key2", "key3", "key4"]);
console.log(test.key1); // => "value1";
console.log(test.key2); // => "value2";
console.log(test.key3); // => "value3";
console.log(test.key4); // => undefined;
Async version of getMany()
Set a value in the cache.
cache.set("key", "value"); // set key to value
Async version of set()
Set multiple values at once.
// using array of key value pairs
cache.setMany([
["key1", "value1"],
["key2", "value2", 500] // with TTL
]);
// using object
cache.setMany([
{ key: "key3", value: "value3" },
{ key: "key4", value: "value4", ttl: 500 } // or with TTL in seconds
]);
console.log(cache.keys()); // => ["key1", "key2", "key3", "key4"]
Async version of setMany()
Find a value in the cache. If it does not exist, set it to the default value.
cache.getOrSet("username", "joe"); // => "joe"
// cache should have username set to joe
cache.has("username"); // => true
// Trying again will return the value set in the cache
cache.getOrSet("username", "john"); // => "joe" because joe is still in the cache
// Functions can be used as default values
cache.getOrSet("email", () => "joe@example.com"); // => "joe@example.com"
Async version of getOrSet()
Check if key exists in the cache.
cache.set("pet", "cat");
cache.has("pet"); // => true
cache.has("pet2"); // => false
Async version of has()
Delete a key from the cache.
cache.set("pet", "dog");
cache.del("pet");
cache.has("pet"); // => false
Async version of del()
Alias for del()
Alias for delAsync()
Get all the keys in the cache.
cache.setMany([
["pet", "dog"],
["username", "joe"],
["email", "joe@example.com"]
]);
cache.keys(); // => ["pet", "username", "email"]
Async version of keys()
Empty the cache completely.
Async version of flush()
Get the number of items in the cache.
Async version of size()
Basically all nodejs cache
modules have similar api, so creating a custom provider is easy.
A custom provider is a function
that takes any necessary configuration and returns a BraveCacheProvider
class instance.
const BraveCacheProvider = require("brave-cache/src/BraveCacheProvider");
function SimpleCache() {
// Holds cache values
let SimpleCacheStore = {};
// Return provider instance
return new BraveCacheProvider({
name,
functions: {
get(key) {
return SimpleCacheStore[key];
},
set(key, value) {
SimpleCacheStore[key] = value;
},
has(key) {
return SimpleCacheStore.hasOwnProperty(key);
},
del(key) {
delete SimpleCacheStore[key];
},
flush() {
SimpleCacheStore = {};
},
keys() {
return Object.keys(SimpleCacheStore);
}
}
});
}
From the example above, the SimpleCache
provider is a simple implementation of how to create a cache Provider.
Notice that you have to implement all the functions that are required by the BraveCacheProvider
class.
These functions will be used when accessing the cache.
Functions to be implemented: get
, set
, has
, del
, flush
, keys
Optional functions: getMany
Note: if any optional function is not implemented, BraveCache in house function will be used instead.
A custom Provider function can include arguments that are related to the provider.
const BraveCache = require("brave-cache");
// Register Simple Cache Above
BraveCache.registerProvider(SimpleCache());
const cache = new BraveCache("simple-cache"); // `simple-cache` is the name of the provider
cache.set("test", "value");
console.log(cache.get("test")); // => value
FAQs
A flexible semantic Api for handling multiple node cache drivers.
We found that brave-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.