1
0
Fork 0
forked from fun/fun

Added a Redis/Valkey extension named redis. (0.42.0)

This commit is contained in:
Johannes Findeisen 2026-06-03 21:48:45 +02:00
commit cbb81c0b93
28 changed files with 798 additions and 15 deletions

View file

@ -0,0 +1,29 @@
Redis extension examples (hiredis)
This folder contains small Fun scripts that demonstrate how to use the Redis extension.
Prerequisites
- Build Fun with Redis support enabled (FUN_WITH_REDIS=ON). This is ON by default in cmake/Extensions/REDIS.cmake.
- A Redis-compatible server reachable at 127.0.0.1:6379.
How to run
- Using the Fun CLI from the repository root:
- Debug profile path: build_debug/fun
- Release profile path: build_release/fun
Examples
1. basic_ping.fun
- Connects, PINGs, then closes.
2. kv_set_get.fun
- SET/GET, EXISTS and DEL for a demo key.
3. list_ops.fun
- Demonstrates LPUSH and LRANGE on a list.
4. hash_ops.fun
- Demonstrates HSET, HGET and HGETALL on a hash.
Note
- All examples use direct inline command strings with redis_cmd(handle, "COMMAND args...").
- Close the connection with redis_close(handle) when finished.

View file

@ -0,0 +1,26 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-06-03
*/
/*
* Simple Redis test using Fun builtins backed by hiredis.
*
* Basic PING
*/
h = redis_connect('127.0.0.1', 6379)
print('handle type: ' + typeof(h))
print(redis_cmd(h, 'PING'))
redis_close(h)

View file

@ -0,0 +1,38 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-06-03
*/
/*
* Simple Redis test using Fun builtins backed by hiredis.
*
* Hash operations (HSET/HGET/HGETALL)
*/
h = redis_connect('127.0.0.1', 6379)
key = 'fun:examples:redis:hash:user1'
// Start fresh
_ = redis_cmd(h, 'DEL ' + key)
// Set a couple of fields
print(redis_cmd(h, 'HSET ' + key + ' name Alice'))
print(redis_cmd(h, 'HSET ' + key + ' age 30'))
// Fetch a single field
print('HGET name -> ' + redis_cmd(h, 'HGET ' + key + ' name'))
// Fetch all fields (returns a flat array [field, value, field, value, ...])
all = redis_cmd(h, 'HGETALL ' + key)
print('HGETALL ->')
print(all)
redis_close(h)

View file

@ -0,0 +1,37 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-06-03
*/
/*
* Simple Redis test using Fun builtins backed by hiredis.
*
* Simple key/value set-get-delete
*/
h = redis_connect('127.0.0.1', 6379)
key = 'fun:examples:redis:key'
// Clean slate
_ = redis_cmd(h, 'DEL ' + key)
// Set and get
print(redis_cmd(h, 'SET ' + key + ' 42'))
print('GET -> ' + redis_cmd(h, 'GET ' + key))
// Check existence
print('EXISTS -> ' + to_string(redis_cmd(h, 'EXISTS ' + key)))
// Delete
print('DEL -> ' + to_string(redis_cmd(h, 'DEL ' + key)))
print('EXISTS(after DEL) -> ' + to_string(redis_cmd(h, 'EXISTS ' + key)))
redis_close(h)

View file

@ -0,0 +1,36 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-06-03
*/
/*
* Simple Redis test using Fun builtins backed by hiredis.
*
* List operations (LPUSH/LRANGE)
*/
h = redis_connect('127.0.0.1', 6379)
key = 'fun:examples:redis:list'
// Start fresh
_ = redis_cmd(h, 'DEL ' + key)
// Push some values to the left
print(redis_cmd(h, 'LPUSH ' + key + ' a'))
print(redis_cmd(h, 'LPUSH ' + key + ' b'))
print(redis_cmd(h, 'LPUSH ' + key + ' c'))
// Read entire list
vals = redis_cmd(h, 'LRANGE ' + key + ' 0 -1')
print('LRANGE 0 -1 -> ')
print(vals)
redis_close(h)

View file

@ -0,0 +1,28 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-06-03
*/
/*
* Simple Redis test using Fun builtins backed by hiredis.
*/
h = redis_connect('127.0.0.1', 6379)
print(typeof(h))
print(redis_cmd(h, 'PING'))
_ = redis_cmd(h, 'SET fun_demo_key 42')
print(redis_cmd(h, 'GET fun_demo_key'))
redis_close(h)