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
48
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 bothsubscriber
andclient
.redis.client
(Optional): Redis client to duplicate for both subscriber and client. Will be prioritized overredis.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 thefallbackFetchMethod
(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.