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

fp-tree-and-member-cache

v0.1.2

Published

Wrapper class for redis-value-cache adapted for Trees and Members.\ Provides Search for kind with name, id or shortName for trees.\ Provides Search for members by id, group, location, position or link.\ On updates over Redis pub/sub the values are dropped

Downloads

70

Readme

FP-Tree-And-Member-Cache

Wrapper class for redis-value-cache adapted for Trees and Members.
Provides Search for kind with name, id or shortName for trees.
Provides Search for members by id, group, location, position or link.
On updates over Redis pub/sub the values are dropped from the cache. Uses the etag property to decide if there were changes.
TreeAndMemberCache needs to be connected before it can be used.

Options

Options for creating a treeAndMemberCache.

  • redis: Options for connection to Redis.
    • redis.clientOpts: Options for the node-redis client (details). Used for both subscriber and client.
    • redis.client (Optional): Redis client to duplicate for both subscriber and client. Will be prioritized over redis.clientOpts if both are provided.
    • channel: name of the channel for updates.
  • fallbackFetchMethod (Optional): Function returning an array of Trees or Members and optionally the etag for this directory.
  • cacheFallbackValues (Optional): Whether to cache Trees/Members from the fallbackFetchMethod (Default: false).
  • cacheMaxSize (Optional): Maximum amount of objects to store in cache (Default 500).

Usage (Typescript)

import {TreeAndMemberCache} from "fp-tree-and-member-cache"
import type { Options } from "fp-tree-and-member-cache";

type Tree = {
	id: number;
	children: Tree[];
	data: {
		shortName?: string;
	};
	name: string;
	kind: "POSITION" | "SECURITY" | "LOCATION" | "GROUP";
}

type Member = {
	linktype: string;
	linkid: string;
	pos: number;
	grp: number;
	loc: number;
	id: string;
}

const opts: Options<Tree, Member> = {
	redis: {
		clientOpts: {
			socket: {
				port: 6379,
				host: "localhost"
			},
			name: "..."
		},
		channel: "channel_name"
	},
	fallbackFetchMethod: async (scope: "trees" | "members", dscId: number, directoryId: number) => {
		// simplified example
		const resp = await fetch("...", {headers: {"User-Agent": "..." }})

		if (!resp.ok) {
			return null;
		}

		const etag = resp.headers.get("etag");

		if (scope === "tree") {
			const treeBody = await resp.json() as {tree: Tree[]};

			return {
				values: treeBody.tree,
				etag: etag ?? undefined
			}
		} 
		const memberBody = await resp.json() as {rows: Member[]};

		return {
			values: memberBody.members,
			etag: etag ?? undefined
		}
	},
	cacheFallbackValues: true,
	cacheMaxSize: 250
}

// Either connect tmc after creation ...

const tmc1 = new TreeAndMemberCache(opts);

await tmc1.connect();

// ... or create already connected tmc

const tmc2 = await TreeAndMemberCache.new(opts);

const result = await tmc1.getTreeNodeByKindAndId(5030, 30, "SECURITY", 13379);

console.log(result?.treeNode)

await tmc1.disconnect();

await tmc2.disconnect();

Emitted Events

| Name | When | Listener arguments | |-------------------------|-----------------------------------------------------------------|----------------------------------------------------------------------------------------------| | ready | RedisValueCache is ready to use | No arguments | | connection-error | Either the client or the subscriber emitted an error event | (error: Error, client: "subscriber" \| "client") | | dropped | A value was dropped because of a message | (scope: "tree" \| "member", dscId: number, directoryId: number,etag: string \| undefined) |

!!Warning!!: You SHOULD listen to error events. If a TreeAndMemberCache doesn't have at least one error listener registered and an error event occurs, that error will be thrown and the Node.js process will exit. See the EventEmitter docs for more details.

If an error event is emitted the TreeAndMemberCache flushes the cache because it could miss messages.

Removing values from cache

If you do not use the delete function the only way values are removed from the cache are via messages in the redis channel. Additionally to receiving a message abut a directory the etag of the message needs to be different from the saved etag to remove the directory.

fallbackFetchMethod

The fallbackFetchMethod is used if a value can not be found in redis. It is important that the array of trees returned by this function only includes roots.

You do not have to return an etag in the fallbackFetchMethod. This would mean the directory will be removed from the cache on any update message for the directory.

It is recommended to set cacheFallbackValues to true if you are using the fallbackFetchMethode, because the additional effort put into indexing the trees/members would be wasted otherwise. If you do not want to cache fallback values it might be better to check if the cache returned a value and then fetch the directory yourself.