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.2.0

Published

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

Downloads

11

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) times, where k is the number of expired items (Not applied to MAP implementation. For details, see config.)
  • in-memory or external storage (e.g. localStorage, Redis) for storing the key-value pair.

Installation

npm i ts-key-value-cache

Examples

TypeScript

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

let options: CacheOption = new CacheOption(CacheType.MAP);
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 | | :---------- | ------------------------- | ------------------------- | :---------- | | cacheType | CacheType | CacheType.MAP | The implementation of IKeyValueCache to use. See CacheType for details. | | 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. | | timeoutMode | TimeoutMode | TimeoutMode.ON_GET_ONLY | State when is the expired is removed. See TimeoutMode for details. | | queueConfig | QueueConfig[] | undefined | Only used if using cacheType.Queues.The config for each index queue. See QueueConfig for details. |

CacheType

MAP

  • Simple JS Map that provide the basic cache without index for expiredTS (timestamp).
  • Support arbitrary ttl.
  • Complicity
    • get, put, delete, clear, size: O(1)
    • clearExpiredItems: O(N)

HEAP

  • An extension of MAP which use a min-heap for faster expired item management.
  • Support arbitrary ttl.
  • Complicity
    • get, put, 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

If only a few cache item will time out, consider to use MAP instead.

QUEUES

  • An extension of MAP which use multiple FIFO queues for expired item management. Each with a fixed ttl.
  • Can have a queue with items that won't expired.
  • Complicity
    • get, put, delete, clear, size: O(1)
    • clearExpiredItems: O(k), where k is the number of expired items

If there are many possible ttl values, consider to use MAP instead.

If there is only one queue, consider to use MAP or HEAP instead.

TimeoutMode

| Mode | Description | | ------------------ | :---------- | | ON_GET_ONLY | When get(key) is called, check if the cache item found is expired. | | INDIVIDUAL_TIMEOUT | - Apart from the checking on get(), each item has its own timeout function emits so that the cache is always at its minium required size.- Should only used if there are only a few items. - Only applicable to MAP type as I believe calling clearExpiredItems() is more effective for the other type. |

QueueConfig

| Option | Type | Default | Description | | :----- | ------------------------- | ------- | :---------- | | ttl | integer > 0 or undefined | undefined | Default ttl (in seconds) of key-value pair if null ttl is supply when put(). undefined for item that won't timeout. | | size | integer > 0 or undefined | undefined | The maximum size of this queue, undefined means no limit. |

Documentation

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