# Simple "cache" node, does this exist?

**URL:** <https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963>\
**Category:** Developing Nodes\
**Created:** [21 March 2021 15:58 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963 "2021-03-21T15:58:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdkrieg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdkrieg/32/39091_2.png) [@mdkrieg](https://discourse.nodered.org/u/mdkrieg)\
**Post date:** [21 March 2021 15:58 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/1 "2021-03-21T15:58:03Z")

</div>

Sometimes when I'm troubleshooting a flow, I want to be able to capture a message at a certain point in the flow and then re-send it on command so I can tweak the downstream code.

What I do now is copy the message from the debug output into an inject node, but I could imagine a node with a button in the middle and pressing it re-generates the most recent message even if the node has been re-deployed since then.

So, the question is, does something like this already exist? I can't seem to find anything

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [21 March 2021 16:16 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/2 "2021-03-21T16:16:23Z")

</div>

You might be able to lash something up using [node-red-contrib-queue-gate](https://flows.nodered.org/node/node-red-contrib-queue-gate). I suggest configuring it for the queueing state, maximum queue size of 1, and Keep Newest Messages. It will still need an `inject` node attached to send a trigger to release the saved message. This may not be exactly what you want, but it seems close.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 March 2021 16:25 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/3 "2021-03-21T16:25:34Z")

</div>

This can be done with a change node and an inject node. simply save the message in change node to a context variable, then in the inject read the context variable

i.e.

```auto
[{"id":"7be26afb.b2b40c","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*10)","payloadType":"jsonata","x":110,"y":2780,"wires":[["2b1242c2.cb29c6"]]},{"id":"2b1242c2.cb29c6","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload*10","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":2780,"wires":[["9ef1060a.c56308","218b7026.69a7b"]]},{"id":"9ef1060a.c56308","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":2780,"wires":[]},{"id":"218b7026.69a7b","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"debug","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":2680,"wires":[[]]},{"id":"7aa34328.330444","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"debug","payloadType":"flow","x":510,"y":2720,"wires":[["9ef1060a.c56308"]]}]

```

---

<div class="post-metadata">

**Author:** ![mdkrieg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdkrieg/32/39091_2.png) [@mdkrieg](https://discourse.nodered.org/u/mdkrieg)\
**Post date:** [21 March 2021 17:08 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/4 "2021-03-21T17:08:15Z")

</div>

Thanks for the responses, I ended up writing a function similar to the inject/change node suggestion:

```auto
[{"id":"cd5e5637.8d0508","type":"tab","label":"simple cache","disabled":false,"info":""},{"id":"5b00ca3e.921a94","type":"inject","z":"cd5e5637.8d0508","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"recall","payload":"bar-test","payloadType":"str","x":490,"y":160,"wires":[["8b8023ea.1baaa"]]},{"id":"82620f2c.d95f9","type":"inject","z":"cd5e5637.8d0508","name":"foo-test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"foo-test","payload":"fake string data","payloadType":"str","x":270,"y":180,"wires":[["8b8023ea.1baaa"]]},{"id":"8b8023ea.1baaa","type":"function","z":"cd5e5637.8d0508","name":"cache / recall","func":"if (msg.topic == \"recall\"){\n var topic = msg.payload\n var data = flow.get(\"cache.\" + topic);\n if(!data){\n node.error(\"no recall data for: \" + topic,msg);\n return null;\n }else{\n return data;\n }\n}else{\n flow.set(\"cache.\" + msg.topic,msg);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":200,"wires":[["3ae504ff.be655c"]]},{"id":"3ae504ff.be655c","type":"debug","z":"cd5e5637.8d0508","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":200,"wires":[]},{"id":"6828e502.96caac","type":"inject","z":"cd5e5637.8d0508","name":"bar-test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bar-test","payload":"{\"foo\":\"fake string data\",\"bar\":[1,1,2,3,5,8,13,21]}","payloadType":"json","x":270,"y":220,"wires":[["8b8023ea.1baaa"]]},{"id":"4d2f0308.f7978c","type":"inject","z":"cd5e5637.8d0508","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"recall","payload":"foo-test","payloadType":"str","x":490,"y":120,"wires":[["8b8023ea.1baaa"]]}]This text will be hidden

```

---

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [21 March 2021 19:08 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/5 "2021-03-21T19:08:01Z")

</div>

Look at message tracer, it will do all that you want and more.  
Holding for inspection and breakpoints.

node-red-contrib-msg-tracer

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 May 2021 19:08 UTC](https://discourse.nodered.org/t/simple-cache-node-does-this-exist/42963/6 "2021-05-20T19:08:36Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
