← 返回首页
node-redis/docs/programmability.md at master · redis/node-redis · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Latest commit

 

History

History
85 lines (69 loc) · 2.47 KB
 master
Top

File metadata and controls

  • Preview
  • Code
  • Blame
85 lines (69 loc) · 2.47 KB

Redis provides a programming interface allowing code execution on the redis server.

The following example retrieves a key in redis, returning the value of the key, incremented by an integer. For example, if your key foo has the value 17 and we run add('foo', 25), it returns the answer to Life, the Universe and Everything.

#!lua name=library redis.register_function { function_name = 'add', callback = function(keys, args) return redis.call('GET', keys[1]) + args[1] end, flags = { 'no-writes' } }

Here is the same example, but in a format that can be pasted into the redis-cli.

FUNCTION LOAD "#!lua name=library\nredis.register_function{function_name='add', callback=function(keys, args) return redis.call('GET', keys[1])+args[1] end, flags={'no-writes'}}"

Load the prior redis function on the redis server before running the example below.

import { CommandParser, createClient, RedisArgument } from '@redis/client'; import { NumberReply } from '@redis/client/dist/lib/RESP/types.js'; const client = createClient({ functions: { library: { add: { NUMBER_OF_KEYS: 1, parseCommand( parser: CommandParser, key: RedisArgument, toAdd: RedisArgument ) { parser.pushKey(key) parser.push(toAdd) }, transformReply: undefined as unknown as () => NumberReply } } } }); await client.connect(); await client.set('key', '1'); await client.library.add('key', '2'); // 3

The following is an end-to-end example of the prior concept.

import { CommandParser, createClient, defineScript, RedisArgument } from '@redis/client'; import { NumberReply } from '@redis/client/dist/lib/RESP/types.js'; const client = createClient({ scripts: { add: defineScript({ SCRIPT: 'return redis.call("GET", KEYS[1]) + ARGV[1];', NUMBER_OF_KEYS: 1, FIRST_KEY_INDEX: 1, parseCommand( parser: CommandParser, key: RedisArgument, toAdd: RedisArgument ) { parser.pushKey(key) parser.push(toAdd) }, transformReply: undefined as unknown as () => NumberReply }) } }); await client.connect(); await client.set('key', '1'); await client.add('key', '2'); // 3

Footer

© 2026 GitHub, Inc.