You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

go-redis是否支持Redis 7.x的Functions功能?求示例与文档

Go-Redis 调用 Redis Functions 指南

go-redis(v9及以上版本)已经支持Redis Functions功能,以下是具体的使用示例和说明:

1. 加载函数到Redis

你可以通过Do方法执行FUNCTION LOAD命令来加载Lua函数:

package main

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // 填写你的Redis密码
		DB:       0,  // 使用默认数据库
	})

	// 定义一个简单的Redis Function
	luaScript := `
		redis.register_function('myfunc', function(keys, args)
			local key = keys[1]
			local value = args[1]
			redis.call('SET', key, value)
			return redis.call('GET', key)
		end)
	`

	// 加载函数到Redis
	err := rdb.Do(ctx, "FUNCTION", "LOAD", luaScript).Err()
	if err != nil {
		panic(err)
	}
	fmt.Println("Function loaded successfully")
}

2. 调用已加载的函数

使用Do方法执行FCALL(可写)或FCALL_RO(只读)命令来调用函数:

// 调用myfunc函数,写入并读取指定键值
result, err := rdb.Do(ctx, "FCALL", "myfunc", 1, "test-key", "hello-redis").Result()
if err != nil {
	panic(err)
}
fmt.Println("Function result:", result) // 输出: hello-redis

3. 查看已加载的函数

通过FUNCTION LIST命令可以查看Redis中所有已加载的函数:

list, err := rdb.Do(ctx, "FUNCTION", "LIST").Result()
if err != nil {
	panic(err)
}
fmt.Println("Loaded functions:", list)

注意事项

  • 确保你的Redis服务器版本为7.0及以上,Functions是Redis 7.x版本才引入的特性。
  • 建议使用go-redis v9及以上版本,该版本对Redis 7.x的新特性支持更完善。
  • 目前go-redis没有专门的高阶API封装Redis Functions,但通过Do方法直接执行原生Redis命令的方式,完全可以实现所有Functions相关操作,满足业务需求。

内容的提问来源于stack exchange,提问作者Mopparthy Ravindranath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:40:25