Saturday, 5 November 2022

How to make memory cache for node-red with wide external (python, LabVIEW, telnet, etc.) support?

I know how to use memcached with python, telnet, JavaScript and via server CGI scripts.

I don't know how to use memcached with node-red.

In pseudo code (~python) I want to do the following with node red:

Import memcached
client = memcached.Client("127.0.0.1", 65554)

msg_payload = client.get("foo")
client.set("bar", 1)

How do I do the above with node-red?

I am a beginner with node-red but experienced programmer in general.

Also, if callbacks are possible, I am interested on those (alternatively, external python script can handle the insertions).

Best practices with node-red is appreciated (especially if memcached is a total no-no).

EDIT:

I got this far (picture below) telnet server. Also, I found node-cache module but I don't know how to connect it directly with telnet: default(?) port 8000(?) does not seem to respond or accept external requests.

enter image description here

EDIT2

Now I have a working setter with https://www.npmjs.com/package/memcache-client

var err;
var r;
var data;

const server = "localhost:11211";

const client = new memcacheClient.MemcacheClient({ server });

client.set("key", "test", (err, r) => {
    msg.payload = r;
});
//client.get("key", (err, data) => {
//    msg.payload = data;
//});



return msg;

The client.get does not work.

The entire flow:

[
{
    "id": "2f80c86f72380963",
    "type": "tab",
    "label": "Flow 2",
    "disabled": false,
    "info": "",
    "env": []
},
{
    "id": "53c023ec831264af",
    "type": "function",
    "z": "2f80c86f72380963",
    "name": "function 1",
    "func": "\n// create a normal client\n\nvar err;\nvar r;\nvar data;\n\nconst server = \"localhost:11211\";\n\nconst client = new memcacheClient.MemcacheClient({ server });\n\nclient.set(\"key\", \"test\", (err, r) => {\n    msg.payload = r;\n});\n//client.get(\"key\", (err, data) => {\n//    msg.payload = data;\n//});\n\n\n\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [
        {
            "var": "memcacheClient",
            "module": "memcache-client"
        },
        {
            "var": "assert",
            "module": "assert"
        }
    ],
    "x": 520,
    "y": 320,
    "wires": [
        [
            "e927f5bbf7b706f4",
            "fa2ee6e396c486e9"
        ]
    ]
},
{
    "id": "e927f5bbf7b706f4",
    "type": "data-review",
    "z": "2f80c86f72380963",
    "name": "Review data",
    "property": "payload",
    "fieldType": "msg",
    "active": true,
    "passthru": true,
    "hide": true,
    "outputs": 1,
    "x": 730,
    "y": 320,
    "wires": [
        []
    ]
},
{
    "id": "4634ad75b9dcd491",
    "type": "inject",
    "z": "2f80c86f72380963",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "3",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 290,
    "y": 320,
    "wires": [
        [
            "53c023ec831264af"
        ]
    ]
},
{
    "id": "fa2ee6e396c486e9",
    "type": "debug",
    "z": "2f80c86f72380963",
    "name": "debug 1",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 740,
    "y": 420,
    "wires": []
}

]



from How to make memory cache for node-red with wide external (python, LabVIEW, telnet, etc.) support?

No comments:

Post a Comment