Einfache Tabelle als Cache

Fortgeschritten

Dies ist ein Automatisierungsworkflow mit 15 Nodes. Hauptsächlich werden If, Set, DataTable, StopAndError, ScheduleTrigger und andere Nodes verwendet. Erstellen Sie ein einfaches Datencache-System ohne externe Abhängigkeiten

Voraussetzungen
  • Keine besonderen Voraussetzungen, sofort nach Import nutzbar

Kategorie

-
Workflow-Vorschau
Visualisierung der Node-Verbindungen, mit Zoom und Pan
Workflow exportieren
Kopieren Sie die folgende JSON-Konfiguration und importieren Sie sie in n8n
{
  "name": "Simple Table as Cache",
  "nodes": [
    {
      "id": "ae9f420d-1e6b-48d7-a064-7486ddd05f5f",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "notes": "To write set the value to true.\nTTL is in milliseconds",
      "position": [
        -16,
        -96
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "trueToWrite",
              "type": "boolean"
            },
            {
              "name": "cacheKey"
            },
            {
              "name": "writeValue",
              "type": "any"
            },
            {
              "name": "writeTTLms",
              "type": "number"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "46e0b000-b24c-467a-9b1a-c50eebbf4b57",
      "name": "Upsert row(s)",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        656,
        -192
      ],
      "parameters": {
        "columns": {
          "value": {
            "key": "={{ $json.cacheKey }}",
            "ttl": "={{ $now.plus($json.writeTTLms > 0 ? $json.writeTTLms : 10000) }}",
            "value": "={{ JSON.stringify($json.writeValue) }}"
          },
          "schema": [
            {
              "id": "key",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "key",
              "defaultMatch": false
            },
            {
              "id": "value",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "value",
              "defaultMatch": false
            },
            {
              "id": "ttl",
              "type": "dateTime",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "ttl",
              "defaultMatch": false
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "filters": {
          "conditions": [
            {
              "keyName": "key",
              "keyValue": "={{ $json.cacheKey }}"
            }
          ]
        },
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "RO0ChvBMPizGf4bb",
          "cachedResultUrl": "/projects/IMwUHp4OzaUhAJNR/datatables/RO0ChvBMPizGf4bb",
          "cachedResultName": "cache"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "be98ade2-7c04-4ac8-863a-7b9fbc01f57a",
      "name": "Return Value",
      "type": "n8n-nodes-base.set",
      "position": [
        880,
        -192
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={{ $('When Executed by Another Workflow').item.json.writeValue }}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "9d06984a-4645-4306-83b6-9d998d60f50c",
      "name": "Action Write Value",
      "type": "n8n-nodes-base.noOp",
      "position": [
        432,
        -192
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3341e665-6c30-4ccc-b598-5eaa2443a9e4",
      "name": "Get row(s)",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        656,
        0
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "key",
              "keyValue": "={{ $json.cacheKey }}"
            }
          ]
        },
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "RO0ChvBMPizGf4bb",
          "cachedResultUrl": "/projects/IMwUHp4OzaUhAJNR/datatables/RO0ChvBMPizGf4bb",
          "cachedResultName": "cache"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "96ae4530-e574-467a-95a9-095882c0161a",
      "name": "If Expired Cache",
      "type": "n8n-nodes-base.if",
      "position": [
        1104,
        80
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "03943791-3760-46e3-ac12-028fdbd5b440",
              "operator": {
                "type": "dateTime",
                "operation": "after"
              },
              "leftValue": "={{ $now }}",
              "rightValue": "={{ DateTime.fromISO($json.ttl) }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "9254b2a6-d265-4c3e-931f-9487ed2dccf5",
      "name": "Read Action",
      "type": "n8n-nodes-base.noOp",
      "position": [
        432,
        0
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "0a656812-609f-4b51-be4c-d364b42273a6",
      "name": "Check Action Type",
      "type": "n8n-nodes-base.if",
      "position": [
        208,
        -96
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cfe96997-a2c7-44d1-a270-798fa5fdbeee",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ !!$json.trueToWrite }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f20a262f-1880-4b20-ab84-5849a2846a5d",
      "name": "If not in Cache",
      "type": "n8n-nodes-base.if",
      "position": [
        880,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a3e38df9-cfdc-4a30-943f-34b42c37eb63",
              "operator": {
                "type": "object",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1ca6a00f-3363-4252-aa72-1779dbc9e1e1",
      "name": "Haftnotiz",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        -96
      ],
      "parameters": {
        "width": 592,
        "height": 320,
        "content": "## IMPORTANT READ THIS!\nThis flow ***Requires*** the Beta version of N8N Tables to function.\nYou will need a table called \"cache\" <- All lowercase.\nThat table will need the following columns, again all lowercase:\n1. key: string\n2. ttl: datetime\n3. value: string\n\n\nPlease create this table and update the two datatable nodes to point to this table."
      },
      "typeVersion": 1
    },
    {
      "id": "ca4d32ac-31f9-4656-ba7f-b77fb0410cb1",
      "name": "No cache found, use error detection to detect this.",
      "type": "n8n-nodes-base.stopAndError",
      "position": [
        1328,
        -96
      ],
      "parameters": {
        "errorMessage": "No Entry or Expired Cache Item."
      },
      "typeVersion": 1
    },
    {
      "id": "fa7762d4-0cce-4b73-bc58-f8569e8a9b28",
      "name": "Return Value from Cache",
      "type": "n8n-nodes-base.set",
      "position": [
        1328,
        128
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={{ JSON.parse($('Get row(s)').item.json.value) }}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "d1cbe73a-2810-4410-9e21-eb51e85a8b45",
      "name": "Haftnotiz1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        -752
      ],
      "parameters": {
        "color": 5,
        "width": 960,
        "height": 544,
        "content": "## How To Use\nCall this node via the Execute Sub-Flow node with the specified inputs.\n(Optional) You can also \"activate\" this flow to enable hourly cleaning of the cache table to help keep data sizes down.\n\n## Output\nThis subflow will return the JSON.parse() representation of the string currently stored at the key in the cache table. This will be the same value written by the cache write input to this node if it has not expired.\n\nIf no value is found in the cache table for the input key, then a error is thrown. Listen for this error by setting your error response mode to be <On Error: \"Continue (Using Error Output)\"> in the node settings.\n\n## Inputs\n#### Action Read Required\n\"cacheKey\": <any string>\n\n#### Action Write Required\n\"cacheKey\": <any string>\n\"trueToWrite\": true,\n\"writeValue\": <any value including null. You are limited to data size of the table string field so don't stuff 20MB of JSON here.>,\n\"writeTTLms\": <optional, any number above 0 as milliseconds. Defaults to 10000>"
      },
      "typeVersion": 1
    },
    {
      "id": "fedbcf7d-d7e2-4920-b57c-8b2e026c9dbe",
      "name": "1 Hour Clean for Cache Table",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -16,
        112
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8e086bcf-d753-4187-b93c-ed2406d600cb",
      "name": "Drop all rows with expired cache entries",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        208,
        112
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "ttl",
              "keyValue": "={{ $now }}",
              "condition": "lt"
            }
          ]
        },
        "options": {
          "dryRun": true
        },
        "operation": "deleteRows",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "RO0ChvBMPizGf4bb",
          "cachedResultUrl": "/projects/IMwUHp4OzaUhAJNR/datatables/RO0ChvBMPizGf4bb",
          "cachedResultName": "cache"
        }
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "3341e665-6c30-4ccc-b598-5eaa2443a9e4": {
      "main": [
        [
          {
            "node": "f20a262f-1880-4b20-ab84-5849a2846a5d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "9254b2a6-d265-4c3e-931f-9487ed2dccf5": {
      "main": [
        [
          {
            "node": "3341e665-6c30-4ccc-b598-5eaa2443a9e4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "46e0b000-b24c-467a-9b1a-c50eebbf4b57": {
      "main": [
        [
          {
            "node": "be98ade2-7c04-4ac8-863a-7b9fbc01f57a",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f20a262f-1880-4b20-ab84-5849a2846a5d": {
      "main": [
        [
          {
            "node": "ca4d32ac-31f9-4656-ba7f-b77fb0410cb1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "96ae4530-e574-467a-95a9-095882c0161a",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "96ae4530-e574-467a-95a9-095882c0161a": {
      "main": [
        [
          {
            "node": "ca4d32ac-31f9-4656-ba7f-b77fb0410cb1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "fa7762d4-0cce-4b73-bc58-f8569e8a9b28",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "0a656812-609f-4b51-be4c-d364b42273a6": {
      "main": [
        [
          {
            "node": "9d06984a-4645-4306-83b6-9d998d60f50c",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "9254b2a6-d265-4c3e-931f-9487ed2dccf5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "9d06984a-4645-4306-83b6-9d998d60f50c": {
      "main": [
        [
          {
            "node": "46e0b000-b24c-467a-9b1a-c50eebbf4b57",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "fedbcf7d-d7e2-4920-b57c-8b2e026c9dbe": {
      "main": [
        [
          {
            "node": "8e086bcf-d753-4187-b93c-ed2406d600cb",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ae9f420d-1e6b-48d7-a064-7486ddd05f5f": {
      "main": [
        [
          {
            "node": "0a656812-609f-4b51-be4c-d364b42273a6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Häufig gestellte Fragen

Wie verwende ich diesen Workflow?

Kopieren Sie den obigen JSON-Code, erstellen Sie einen neuen Workflow in Ihrer n8n-Instanz und wählen Sie "Aus JSON importieren". Fügen Sie die Konfiguration ein und passen Sie die Anmeldedaten nach Bedarf an.

Für welche Szenarien ist dieser Workflow geeignet?

Fortgeschritten

Ist es kostenpflichtig?

Dieser Workflow ist völlig kostenlos. Beachten Sie jedoch, dass Drittanbieterdienste (wie OpenAI API), die im Workflow verwendet werden, möglicherweise kostenpflichtig sind.

Workflow-Informationen
Schwierigkeitsgrad
Fortgeschritten
Anzahl der Nodes15
Kategorie-
Node-Typen8
Schwierigkeitsbeschreibung

Für erfahrene Benutzer, mittelkomplexe Workflows mit 6-15 Nodes

Externe Links
Auf n8n.io ansehen

Diesen Workflow teilen

Kategorien

Kategorien: 34