npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-key-value-cache

v1.3.0

Published

A typescript key-value cache that support time-to-live management.

Downloads

15

Readme

Typescript Local Key Value Cache

A key-value cache with string as key and value of any type which support

  • time-to-live management
  • size control which remove exceed key-value pair in a FIFO manner
  • housekeep function to clear the expired item in cache with O(k log N) times, where k is the number of expired items.
  • in-memory or external storage (e.g. localStorage, Redis) for storing the key-value pair.

A cache with HEAP index

  • Use a min-heap for faster expired item management.
  • Support arbitrary ttl.
  • Complicity
    • get, delete, clear, size: O(1)
    • put: O(log N) for index insertion
    • clearExpiredItems: O(k log N), where k is the number of expired items

Installation

npm i ts-key-value-cache

Examples

TypeScript

import { CacheFactory, CacheOption, TimeoutMode, IKeyValueCache, CachedValue, IMapStorage } from "ts-key-value-cache";

let options: CacheOption = new CacheOption();
options.defaultTTL = 60 * 30; // 30 min
options.maxSize = 100; // this cache keep 100 key-value pair at most

let cache: IKeyValueCache<string> = CacheFactory.make<string>(options);
// One cache can only accept one type of value
// For IKeyValueCache<V>, V is the type of the value it accept
// E.g. the value must be string for this cache

cache.put("abc", "first", 60); // with 60s ttl
cache.put("abc", "testing", null); // replace the last one & using default ttl (30 min)
cache.put("Z", "123"); // will never expired unless it get push out due to cache size limit

cache.get("not exists"); // return undefined
cache.get("abc"); // return "testing"

// use external storage
class SomeMapStorageImplementation implements IMapStorage<string> {...}
cacheInstance = CacheFactory.make<string>(options, new SomeMapStorageImplementation());

Operations on IKeyValueCache

Refer to the index.d.ts, comments in code, or the generated documentation for details.

get(key: string): V | undefined Return value if found in cache and it is not yet expired. Otherwise, return undefined.

put(key: string, value: V, ttl?: number): void Put the item into cache. If there is already a cache with the same key, this will replace the old one and the expired timestamp will be renew ny the new ttl.

delete(key: string): boolean Delete the item from cache with the given key if exists.

clear(): void Clear the cache (& the index for some implementation).

size(): Integer Return the total number of item in this cache.

clearExpiredItems(): void Delete all expired items in the cache.

May take O(N) time (where N is cache current size) for some implementation without index. See CacheType for details.

Config

The cache created is config by the CacheOption passed to the factory. Set up the CacheOption by the setter of its attributes (listed below).

| Option | Type | Default | Description | | :---------- | ------------------------- | ------------------------- | :---------- | | defaultTTL | integer > 0 | undefined | The ttl (in seconds) for a item pit with a null ttl. undefined (or 0) means never timeout. | | maxSize | integer > 0 or undefined | undefined | The maximum number of key-value pairs the cache can hold. The exceed items are push out in a FIFO manner, regardless of its ttl and expired timestamp. If undefined, means there is no size limit. If the cache is of cacheType.Queues, keep this attribute undefined (meaningless) and set the one in QueueConfig instead. |

Documentation

Download the module from git and run npm run doc to get a full version of doc.