
Security News
pnpm 10.16 Adds New Setting for Delayed Dependency Updates
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
@jamesbontempo/typecache
Advanced tools
A simple TypeScript caching library with ttl support and SQL-inspired syntax
typecache
is a simple TypeScript caching library with ttl support and SQL-inspired syntax.
npm install @jamesbontempo/typecache
npm test
const { TypeCache } = require("@jamesbontempo/typecache");
const cache = new TypeCache();
cache.ttl = 15 * 60 * 1000; // 15 minutes
cache.on("delete", item => { console.log(item); });
cache.insert("key", "value");
cache.update("key", ["new value 1", "new value 2"]);
console.log(cache.keys()); // ["key"]
console.log(cache.select("key")); // ["new value 1", "new value 2"]
console.log(cache.exists("non-existent key")); // false
cache.delete("key"); // removes & emits "delete" event for the item w/a key of "key"
cache.clear(); // removes & emits delete events for any remaining items
Creates a new TypeCache
instance. By default, there is no ttl
(actually, it's set to Infinity
) & all items persist until deleted, or until the cache itself is cleared or destroyed.
count
The number of items in the cache.
ttl
The default ttl in milliseconds for items inserted into the cache. You can "get" or "set" this value. Values less than or equal to zero are ignored.
keys()
Returns an array of the cache keys.
clear()
Removes all items from the cache.
insert(key, value [, ttl, force])
Inserts an item into the cache. If no ttl
is provided the default cache-level ttl
is used. If a ttl
value less than or equal to zero is provided it's ignored. If an item already exists for the given key and force
is true
the item will effectively be overwritten (both its value
and its ttl
); otherwise, no changes will be made.
select(key)
Returns the value of the item with the given key
.
exists(key)
Returns true
if an item with the given key exists in the cache, false otherwise. This can be particularly helpful for determining if an item really exists since a call to select
for a non-existent key will return undefined
.
update(key, value)
Updates the value of the item in the cache.
remaining(key)
Returns the remaining ttl
time for the item; that is, how much longer before the item is removed from the cache. If there is no ttl
it will return Infinity
.
extend(key [, ttl])
Extends the ttl
of the item by a number of milliseconds. If no ttl
is provided the default cache-level ttl is used (allowing you to basically "bump" the item). If a ttl
of Infinity
is provided, the ttl
is effectively removed.
shorten(key, ttl)
Shortens the ttl
of the item by a number of milliseconds. Values less than or equal to zero are ignored. If the current ttl
is less than the value provided no changes are made because the item would be deleted before the shortened time anyway. If the current ttl
is Infinity
, the ttl
is set to the value provided.
delete(key)
Deletes the item from the cache.
The cache emits events when the insert
, update
, and delete
methods are called. The events have the same corresponding names: insert
, update
, and delete
. They emit objects with information about the related cache items:
{
key: key name,
value: key value,
ttl: key ttl,
added: when the key was added,
modified: when the key value was last modified,
deleted: when the key was deleted
}
insert
Emits information about the item inserted.
update
Emits information about an update to an item, including before
and after
objects allowing for comparison.
delete
Emits information about the item deleted; potentially useful for "listening" for ttl
expirations.
This allows you to do things like:
cache.on("insert", item => { verify(item); });
cache.on("update", item => { compare(item.before, item.after); });
cache.on("delete", item => { log(item); });
FAQs
A simple TypeScript caching library with ttl support and SQL-inspired syntax
We found that @jamesbontempo/typecache 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's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.