| 6 | import { getBaseClasses, getCredentialData, getCredentialParam, ICommonObject, INode, INodeData, INodeParams } from '../../../src' |
| 7 | |
| 8 | class RedisCache implements INode { |
| 9 | label: string |
| 10 | name: string |
| 11 | version: number |
| 12 | description: string |
| 13 | type: string |
| 14 | icon: string |
| 15 | category: string |
| 16 | baseClasses: string[] |
| 17 | inputs: INodeParams[] |
| 18 | credential: INodeParams |
| 19 | |
| 20 | constructor() { |
| 21 | this.label = 'Redis Cache' |
| 22 | this.name = 'redisCache' |
| 23 | this.version = 1.0 |
| 24 | this.type = 'RedisCache' |
| 25 | this.description = 'Cache LLM response in Redis, useful for sharing cache across multiple processes or servers' |
| 26 | this.icon = 'redis.svg' |
| 27 | this.category = 'Cache' |
| 28 | this.baseClasses = [this.type, ...getBaseClasses(LangchainRedisCache)] |
| 29 | this.credential = { |
| 30 | label: 'Connect Credential', |
| 31 | name: 'credential', |
| 32 | type: 'credential', |
| 33 | optional: true, |
| 34 | credentialNames: ['redisCacheApi', 'redisCacheUrlApi'] |
| 35 | } |
| 36 | this.inputs = [ |
| 37 | { |
| 38 | label: 'Time to Live (ms)', |
| 39 | name: 'ttl', |
| 40 | type: 'number', |
| 41 | step: 1, |
| 42 | optional: true, |
| 43 | additionalParams: true |
| 44 | } |
| 45 | ] |
| 46 | } |
| 47 | |
| 48 | async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 49 | const ttl = nodeData.inputs?.ttl as string |
| 50 | |
| 51 | let client = await getRedisClient(nodeData, options) |
| 52 | const redisClient = new LangchainRedisCache(client) |
| 53 | |
| 54 | redisClient.lookup = async (prompt: string, llmKey: string) => { |
| 55 | try { |
| 56 | const pingResp = await client.ping() |
| 57 | if (pingResp !== 'PONG') { |
| 58 | client = await getRedisClient(nodeData, options) |
| 59 | } |
| 60 | } catch (error) { |
| 61 | client = await getRedisClient(nodeData, options) |
| 62 | } |
| 63 | |
| 64 | let idx = 0 |
| 65 | let key = getCacheKey(prompt, llmKey, String(idx)) |
nothing calls this directly
no outgoing calls
no test coverage detected