update 0.1.1
基于 MoonBit 语言特性实现的内存型非关系型数据库,参考 Redis 设计。
⚠️ 项目能力边界 moonbitDB 是一个单进程内存型数据库库,以下能力暂不在项目范围内: 暂不提供网络服务 — 无 TCP/HTTP 服务器,无远程连接能力,不支持客户端-服务器架构 暂不提供数据持久化 — 所有数据仅存储于内存中(Map[String, RedisValue]),进程退出后数据即丢失 暂不提供并发处理 — 无异步操作,无线程安全保证,无锁机制 适用场景:嵌入式场景、测试模拟、本地缓存、学习参考 README 中的 PING/ECHO/INFO 等命令为 API 方法,非网络服务协议命令。
⚠️ 项目能力边界
moonbitDB 是一个单进程内存型数据库库,以下能力暂不在项目范围内:
Map[String, RedisValue]
适用场景:嵌入式场景、测试模拟、本地缓存、学习参考
README 中的 PING/ECHO/INFO 等命令为 API 方法,非网络服务协议命令。
运行 moonbitDB 需要 MoonBit CLI 工具。
macOS / Linux:
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
Windows:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
npm 安装(跨平台):
npm install -g @moonbit/cli
更多安装方式请参考 MoonBit 官方安装文档。
moon version
成功安装后将输出 MoonBit CLI 版本信息。
moon run . --target native
moon test
当前测试覆盖:64 个测试用例,全部通过。
在其他 MoonBit 项目中使用 moonbitDB 作为依赖,需要以下步骤:
在你的项目 moon.mod.json 的 deps 字段中添加 moonbitDB 依赖:
moon.mod.json
deps
{ "name": "your-name/your-project", "deps": { "JIA2JIA2/moonbitdb": "0.1.1" } }
moon update
在你的包 moon.pkg.json 的 import 字段中引入核心库:
moon.pkg.json
import
{ "import": ["JIA2JIA2/moonbitdb/lib"] }
fn main { let db = @lib.Database::new() db.set("greeting", "Hello, moonbitDB!") let value = db.get("greeting") println(value) // Some("Hello, moonbitDB!") }
注意:moon update 是必须步骤,添加依赖后未执行此命令将导致编译错误。 导入路径为 JIA2JIA2/moonbitdb/lib(注意包含 /lib 后缀),不是 JIA2JIA2/moonbitdb。
JIA2JIA2/moonbitdb/lib
/lib
JIA2JIA2/moonbitdb
moonbitDB/ ├── lib/ # 核心库(非 main 包,可被其他包 import) │ ├── database.mbt # 核心数据库实现(~3200 行) │ └── moon.pkg.json ├── demo.mbt # 快速开始演示(主包入口) ├── moon.pkg.json # 主包配置(is_main: true,依赖 lib) ├── examples/ # 场景部署示例(独立子包,均可运行) │ ├── basic_usage/ # 基础 API 使用示例 │ │ ├── main.mbt │ │ └── moon.pkg.json │ ├── leaderboard/ # 游戏排行榜场景 │ │ ├── main.mbt │ │ └── moon.pkg.json │ ├── shopping_cart/ # 电商购物车 / 缓存场景 │ │ ├── main.mbt │ │ └── moon.pkg.json │ └── cli_repl/ # 命令行交互演示 │ ├── main.mbt │ └── moon.pkg.json ├── moon.mod.json # 模块配置(moonbitdb/moonbitdb) └── README.md # 本文档
包架构说明:核心逻辑放在 lib/ 子包(非 main),主包和 examples 均依赖 lib。这种设计符合 MoonBit 规范,避免了 “examples depend on main package” 的警告。
lib/
共支持 80+ 个命令,覆盖 Redis 核心功能。
SET
(key, value) -> Unit
GET
(key) -> String?
DEL
(key) -> Bool
EXISTS
APPEND
(key, value) -> Int
STRLEN
(key) -> Int
INCR
(key) -> Int?
DECR
KEYS
() -> Array[String]
KEYS_PATTERN
(pattern) -> Array[String]
*
?
TYPE
(key) -> String
EXPIRE
(key, seconds) -> Bool
PEXPIRE
(key, ms) -> Bool
TTL
PTTL
PERSIST
RENAME
(old_key, new_key) -> Bool
RENAMENX
RANDOMKEY
() -> String?
HSET
(key, field, value) -> Unit
HGET
(key, field) -> String?
HDEL
(key, field) -> Bool
HGETALL
(key) -> Map[String, String]
HLEN
HEXISTS
HKEYS
(key) -> Array[String]
HVALS
注意:HEXISTS、HKEYS、HVALS 在 command() 返回列表中已声明,但源码中尚未实现对应方法,待后续版本补充。
command()
LPUSH
RPUSH
LPOP
RPOP
LLEN
LRANGE
(key, start, end) -> Array[String]
LINDEX
(key, index) -> String?
LSET
(key, index, value) -> Bool
LREM
(key, count, value) -> Int
LINSERT
(key, before, pivot, value) -> Int
LPUSHX
RPUSHX
RPOPLPUSH
(source, dest) -> String?
SADD
(key, value) -> Bool
SMEMBERS
SREM
SCARD
SISMEMBER
SINTER
(keys) -> Array[String]
SINTERSTORE
(dest, keys) -> Int
SUNION
SUNIONSTORE
SDIFF
SDIFFSTORE
SMOVE
(source, dest, value) -> Bool
SPOP
ZADD
(key, score, member) -> Bool
ZRANGE
ZCARD
ZSCORE
(key, member) -> Float?
ZREM
(key, member) -> Bool
ZRANGEBYSCORE
(key, min, max) -> Array[String]
ZCOUNT
(key, min, max) -> Int
ZREVRANGE
ZREVRANGEBYSCORE
(key, max, min) -> Array[String]
ZRANK
(key, member) -> Int?
ZREVRANK
ZINCRBY
(key, increment, member) -> Float
ZREMRANGEBYRANK
(key, start, end) -> Int
ZREMRANGEBYSCORE
ZPOPMIN
(key) -> (String, Float)?
ZPOPMAX
MSET
(keys, values) -> Unit
MGET
(keys) -> Array[String?]
MDEL
(keys) -> Int
HMSET
(key, fields, values) -> Unit
HMGET
(key, fields) -> Array[String?]
DBSIZE
() -> Int
FLUSHDB
() -> Unit
FLUSHALL
PING
() -> String
ECHO
(message) -> String
INFO
TIME
() -> (Int, Int)
COMMAND
OBJECT_ENCODING
以下列出 lib/database.mbt 中所有公共 API 的完整签名。通过 @lib 访问。
lib/database.mbt
@lib
new
() -> Database
set_time
(self : Database, time_ms : Int) -> Unit
advance_time
(self : Database, ms : Int) -> Unit
check_expired
(self : Database, key : String) -> Bool
set
(self : Database, key : String, value : String) -> Unit
get
(self : Database, key : String) -> String?
del
exists
append
(self : Database, key : String, value : String) -> Int
strlen
(self : Database, key : String) -> Int
incr
(self : Database, key : String) -> Int?
decr
keys
(self : Database) -> Array[String]
keys_pattern
(self : Database, pattern : String) -> Array[String]
type_of
(self : Database, key : String) -> String
expire
(self : Database, key : String, seconds : Int) -> Bool
pexpire
(self : Database, key : String, milliseconds : Int) -> Bool
ttl
pttl
persist
rename
(self : Database, old_key : String, new_key : String) -> Bool
renamenx
randomkey
(self : Database) -> String?
hset
(self : Database, key : String, field : String, value : String) -> Unit
hget
(self : Database, key : String, field : String) -> String?
hdel
(self : Database, key : String, field : String) -> Bool
hgetall
(self : Database, key : String) -> Map[String, String]
hlen
hexists
hkeys
(self : Database, key : String) -> Array[String]
hvals
注意:hexists、hkeys、hvals 在 command() 返回列表中已声明,但当前版本源码中尚未实现。
lpush
rpush
lpop
rpop
llen
lrange
(self : Database, key : String, start : Int, end : Int) -> Array[String]
lindex
(self : Database, key : String, index : Int) -> String?
lset
(self : Database, key : String, index : Int, value : String) -> Bool
lrem
(self : Database, key : String, count : Int, value : String) -> Int
linsert
(self : Database, key : String, before : Bool, pivot : String, value : String) -> Int
lpushx
rpushx
rpoplpush
(self : Database, source : String, destination : String) -> String?
sadd
(self : Database, key : String, value : String) -> Bool
smembers
srem
scard
sismember
sinter
(self : Database, keys : Array[String]) -> Array[String]
sinterstore
(self : Database, destination : String, keys : Array[String]) -> Int
sunion
sunionstore
sdiff
sdiffstore
smove
(self : Database, source : String, destination : String, value : String) -> Bool
spop
zadd
(self : Database, key : String, score : Float, member_val : String) -> Bool
zrange
zcard
zscore
(self : Database, key : String, member_val : String) -> Float?
zrem
(self : Database, key : String, member_val : String) -> Bool
zrangebyscore
(self : Database, key : String, min_score : Float, max_score : Float) -> Array[String]
zcount
(self : Database, key : String, min_score : Float, max_score : Float) -> Int
zrevrange
zrevrangebyscore
(self : Database, key : String, max_score : Float, min_score : Float) -> Array[String]
zrank
(self : Database, key : String, member_val : String) -> Int?
zrevrank
zincrby
(self : Database, key : String, increment : Float, member_val : String) -> Float
zremrangebyrank
(self : Database, key : String, start : Int, end : Int) -> Int
zremrangebyscore
zpopmin
(self : Database, key : String) -> (String, Float)?
zpopmax
mset
(self : Database, keys : Array[String], values : Array[String]) -> Unit
mget
(self : Database, keys : Array[String]) -> Array[String?]
mdel
(self : Database, keys : Array[String]) -> Int
hmset
(self : Database, key : String, fields : Array[String], values : Array[String]) -> Unit
hmget
(self : Database, key : String, fields : Array[String]) -> Array[String?]
dbsize
(self : Database) -> Int
flushdb
(self : Database) -> Unit
flushall
ping
echo
(self : Database, message : String) -> String
info
(self : Database) -> String
time
(self : Database) -> (Int, Int)
command
object_encoding
双端队列数据结构,作为 List 类型的底层实现暴露为公共 API。
() -> Deque
push_front
(self : Deque, value : String) -> Unit
push_back
pop_front
(self : Deque) -> String?
pop_back
length
(self : Deque) -> Int
to_array
(self : Deque) -> Array[String]
get_at
(self : Deque, index : Int) -> String?
set_at
(self : Deque, index : Int, value : String) -> Bool
remove_at
(self : Deque, index : Int) -> Bool
find_index
(self : Deque, value : String) -> Int
insert_at
match_pattern
(pattern : String, str : String) -> Bool
match_pattern_helper
(pattern : String, str : String, p_idx : Int, s_idx : Int) -> Bool
uint16_to_digit
(c : UInt16) -> Int?
digit_to_uint16
(n : Int) -> UInt16
parse_int
(s : String) -> Int?
int_to_string
(n : Int) -> String
sort_by_score
(items : Array[(String, Float)]) -> Array[(String, Float)]
merge_sort
(arr : Array[(String, Float)]) -> Array[(String, Float)]
merge
(left : Array[(String, Float)], right : Array[(String, Float)]) -> Array[(String, Float)]
所有示例均为独立子包,可单独运行。运行前需先安装 MoonBit CLI 工具。
直接运行主包即可看到 8 大功能模块的完整演示:
输出预览:
======================================== MoonBitDB - 快速开始演示 ======================================== 📌 1. String 基础操作 GET greeting = "Hello, MoonBitDB!" INCR counter = 11 📌 2. Hash 用户信息存储 HLEN user:1001 = 3 📌 3. List 任务队列 LRANGE tasks 0 -1 = [完成报告, 代码审查, 团队会议] 📌 4. Set 标签系统 SCARD = 3 (自动去重) 📌 5. Sorted Set 排行榜 TOP 3 (ZREVRANGE 0 2): #1 玩家D - 3200分 #2 玩家B - 2500分 📌 6. Key 过期机制 65秒后 EXISTS = false 📌 7. 批量操作 📌 8. 服务器信息 ======================================== ✅ 演示完成! ========================================
覆盖所有 5 种数据结构的基础操作演示。
moon run examples/basic_usage --target native
参考 examples/basic_usage/main.mbt
10 阶段完整场景:初始化分数 → TOP 5 查询 → 对局更新 → 排名变化 → 分数统计 → 每日榜单 → 领奖弹出 → 好友关系 → 消息队列 → 状态统计。
moon run examples/leaderboard --target native
参考 examples/leaderboard/main.mbt
展示了如何使用:
10 阶段完整场景:商品缓存 → 购物车 → 浏览历史 → 收藏 → 共同收藏 → 商品标签 → 销量排行 → 会话管理 → 缓存预热 → 状态总览。
moon run examples/shopping_cart --target native
参考 examples/shopping_cart/main.mbt
模拟 Redis 风格的命令行交互,自动执行 45 条演示命令并输出结果。
moon run examples/cli_repl --target native
参考 examples/cli_repl/main.mbt
支持解析和执行 60+ 命令,包括 SET/GET/HSET/LPUSH/SADD/ZADD/MSET 等全部命令类型。
本项目采用 Apache License 2.0 许可证(SPDX 标识符:Apache-2.0)。
Apache-2.0
详见项目根目录 LICENSE 文件。
本项目参考 Redis 的 API 设计思路,基于 MoonBit 语言独立实现。本项目不是 Redis 的移植或 fork,所有代码均为原创实现。
本项目参考了 Redis 的以下功能设计:
数据结构(5 种):
命令语义(8 大命令组,80+ 个命令):
Key 过期机制:
内部编码命名(OBJECT_ENCODING 命令):
embstr
listpack
hashtable
skiplist
quicklist
根据moonbit的特性,设计非关系型数据库
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802047560号
moonbitDB
基于 MoonBit 语言特性实现的内存型非关系型数据库,参考 Redis 设计。
安装
前置条件
运行 moonbitDB 需要 MoonBit CLI 工具。
安装 MoonBit CLI
macOS / Linux:
Windows:
npm 安装(跨平台):
验证安装
成功安装后将输出 MoonBit CLI 版本信息。
快速开始
运行演示
运行测试
当前测试覆盖:64 个测试用例,全部通过。
库导入指南
在其他 MoonBit 项目中使用 moonbitDB 作为依赖,需要以下步骤:
1. 添加模块依赖
在你的项目
moon.mod.json的deps字段中添加 moonbitDB 依赖:2. 同步依赖
3. 声明包导入
在你的包
moon.pkg.json的import字段中引入核心库:4. 使用
项目结构
支持的命令
共支持 80+ 个命令,覆盖 Redis 核心功能。
String 命令
SET(key, value) -> UnitGET(key) -> String?DEL(key) -> BoolEXISTS(key) -> BoolAPPEND(key, value) -> IntSTRLEN(key) -> IntINCR(key) -> Int?DECR(key) -> Int?Key 命令
KEYS() -> Array[String]KEYS_PATTERN(pattern) -> Array[String]*?)TYPE(key) -> StringEXPIRE(key, seconds) -> BoolPEXPIRE(key, ms) -> BoolTTL(key) -> IntPTTL(key) -> IntPERSIST(key) -> BoolRENAME(old_key, new_key) -> BoolRENAMENX(old_key, new_key) -> BoolRANDOMKEY() -> String?Hash 命令
HSET(key, field, value) -> UnitHGET(key, field) -> String?HDEL(key, field) -> BoolHGETALL(key) -> Map[String, String]HLEN(key) -> IntHEXISTS(key, field) -> BoolHKEYS(key) -> Array[String]HVALS(key) -> Array[String]List 命令
LPUSH(key, value) -> IntRPUSH(key, value) -> IntLPOP(key) -> String?RPOP(key) -> String?LLEN(key) -> IntLRANGE(key, start, end) -> Array[String]LINDEX(key, index) -> String?LSET(key, index, value) -> BoolLREM(key, count, value) -> IntLINSERT(key, before, pivot, value) -> IntLPUSHX(key, value) -> IntRPUSHX(key, value) -> IntRPOPLPUSH(source, dest) -> String?Set 命令
SADD(key, value) -> BoolSMEMBERS(key) -> Array[String]SREM(key, value) -> BoolSCARD(key) -> IntSISMEMBER(key, value) -> BoolSINTER(keys) -> Array[String]SINTERSTORE(dest, keys) -> IntSUNION(keys) -> Array[String]SUNIONSTORE(dest, keys) -> IntSDIFF(keys) -> Array[String]SDIFFSTORE(dest, keys) -> IntSMOVE(source, dest, value) -> BoolSPOP(key) -> String?Sorted Set 命令
ZADD(key, score, member) -> BoolZRANGE(key, start, end) -> Array[String]ZCARD(key) -> IntZSCORE(key, member) -> Float?ZREM(key, member) -> BoolZRANGEBYSCORE(key, min, max) -> Array[String]ZCOUNT(key, min, max) -> IntZREVRANGE(key, start, end) -> Array[String]ZREVRANGEBYSCORE(key, max, min) -> Array[String]ZRANK(key, member) -> Int?ZREVRANK(key, member) -> Int?ZINCRBY(key, increment, member) -> FloatZREMRANGEBYRANK(key, start, end) -> IntZREMRANGEBYSCORE(key, min, max) -> IntZPOPMIN(key) -> (String, Float)?ZPOPMAX(key) -> (String, Float)?批量命令
MSET(keys, values) -> UnitMGET(keys) -> Array[String?]MDEL(keys) -> IntHMSET(key, fields, values) -> UnitHMGET(key, fields) -> Array[String?]服务器命令
DBSIZE() -> IntFLUSHDB() -> UnitFLUSHALL() -> UnitPING() -> StringECHO(message) -> StringINFO() -> StringTIME() -> (Int, Int)COMMAND() -> Array[String]OBJECT_ENCODING(key) -> String?完整公共 API 文档
以下列出
lib/database.mbt中所有公共 API 的完整签名。通过@lib访问。Database 结构体
构造函数与辅助方法
new() -> Databaseset_time(self : Database, time_ms : Int) -> Unitadvance_time(self : Database, ms : Int) -> Unitcheck_expired(self : Database, key : String) -> BoolString 操作
set(self : Database, key : String, value : String) -> Unitget(self : Database, key : String) -> String?del(self : Database, key : String) -> Boolexists(self : Database, key : String) -> Boolappend(self : Database, key : String, value : String) -> Intstrlen(self : Database, key : String) -> Intincr(self : Database, key : String) -> Int?decr(self : Database, key : String) -> Int?Key 操作
keys(self : Database) -> Array[String]keys_pattern(self : Database, pattern : String) -> Array[String]type_of(self : Database, key : String) -> Stringexpire(self : Database, key : String, seconds : Int) -> Boolpexpire(self : Database, key : String, milliseconds : Int) -> Boolttl(self : Database, key : String) -> Intpttl(self : Database, key : String) -> Intpersist(self : Database, key : String) -> Boolrename(self : Database, old_key : String, new_key : String) -> Boolrenamenx(self : Database, old_key : String, new_key : String) -> Boolrandomkey(self : Database) -> String?Hash 操作
hset(self : Database, key : String, field : String, value : String) -> Unithget(self : Database, key : String, field : String) -> String?hdel(self : Database, key : String, field : String) -> Boolhgetall(self : Database, key : String) -> Map[String, String]hlen(self : Database, key : String) -> Inthexists(self : Database, key : String, field : String) -> Boolhkeys(self : Database, key : String) -> Array[String]hvals(self : Database, key : String) -> Array[String]List 操作
lpush(self : Database, key : String, value : String) -> Intrpush(self : Database, key : String, value : String) -> Intlpop(self : Database, key : String) -> String?rpop(self : Database, key : String) -> String?llen(self : Database, key : String) -> Intlrange(self : Database, key : String, start : Int, end : Int) -> Array[String]lindex(self : Database, key : String, index : Int) -> String?lset(self : Database, key : String, index : Int, value : String) -> Boollrem(self : Database, key : String, count : Int, value : String) -> Intlinsert(self : Database, key : String, before : Bool, pivot : String, value : String) -> Intlpushx(self : Database, key : String, value : String) -> Intrpushx(self : Database, key : String, value : String) -> Intrpoplpush(self : Database, source : String, destination : String) -> String?Set 操作
sadd(self : Database, key : String, value : String) -> Boolsmembers(self : Database, key : String) -> Array[String]srem(self : Database, key : String, value : String) -> Boolscard(self : Database, key : String) -> Intsismember(self : Database, key : String, value : String) -> Boolsinter(self : Database, keys : Array[String]) -> Array[String]sinterstore(self : Database, destination : String, keys : Array[String]) -> Intsunion(self : Database, keys : Array[String]) -> Array[String]sunionstore(self : Database, destination : String, keys : Array[String]) -> Intsdiff(self : Database, keys : Array[String]) -> Array[String]sdiffstore(self : Database, destination : String, keys : Array[String]) -> Intsmove(self : Database, source : String, destination : String, value : String) -> Boolspop(self : Database, key : String) -> String?Sorted Set 操作
zadd(self : Database, key : String, score : Float, member_val : String) -> Boolzrange(self : Database, key : String, start : Int, end : Int) -> Array[String]zcard(self : Database, key : String) -> Intzscore(self : Database, key : String, member_val : String) -> Float?zrem(self : Database, key : String, member_val : String) -> Boolzrangebyscore(self : Database, key : String, min_score : Float, max_score : Float) -> Array[String]zcount(self : Database, key : String, min_score : Float, max_score : Float) -> Intzrevrange(self : Database, key : String, start : Int, end : Int) -> Array[String]zrevrangebyscore(self : Database, key : String, max_score : Float, min_score : Float) -> Array[String]zrank(self : Database, key : String, member_val : String) -> Int?zrevrank(self : Database, key : String, member_val : String) -> Int?zincrby(self : Database, key : String, increment : Float, member_val : String) -> Floatzremrangebyrank(self : Database, key : String, start : Int, end : Int) -> Intzremrangebyscore(self : Database, key : String, min_score : Float, max_score : Float) -> Intzpopmin(self : Database, key : String) -> (String, Float)?zpopmax(self : Database, key : String) -> (String, Float)?批量操作
mset(self : Database, keys : Array[String], values : Array[String]) -> Unitmget(self : Database, keys : Array[String]) -> Array[String?]mdel(self : Database, keys : Array[String]) -> Inthmset(self : Database, key : String, fields : Array[String], values : Array[String]) -> Unithmget(self : Database, key : String, fields : Array[String]) -> Array[String?]服务器命令
dbsize(self : Database) -> Intflushdb(self : Database) -> Unitflushall(self : Database) -> Unitping() -> Stringecho(self : Database, message : String) -> Stringinfo(self : Database) -> Stringtime(self : Database) -> (Int, Int)command() -> Array[String]object_encoding(self : Database, key : String) -> String?Deque 结构体
双端队列数据结构,作为 List 类型的底层实现暴露为公共 API。
new() -> Dequepush_front(self : Deque, value : String) -> Unitpush_back(self : Deque, value : String) -> Unitpop_front(self : Deque) -> String?pop_back(self : Deque) -> String?length(self : Deque) -> Intto_array(self : Deque) -> Array[String]get_at(self : Deque, index : Int) -> String?set_at(self : Deque, index : Int, value : String) -> Boolremove_at(self : Deque, index : Int) -> Boolfind_index(self : Deque, value : String) -> Intinsert_at(self : Deque, index : Int, value : String) -> Bool独立函数
match_pattern(pattern : String, str : String) -> Bool*?)match_pattern_helper(pattern : String, str : String, p_idx : Int, s_idx : Int) -> Booluint16_to_digit(c : UInt16) -> Int?digit_to_uint16(n : Int) -> UInt16parse_int(s : String) -> Int?int_to_string(n : Int) -> Stringsort_by_score(items : Array[(String, Float)]) -> Array[(String, Float)]merge_sort(arr : Array[(String, Float)]) -> Array[(String, Float)]merge(left : Array[(String, Float)], right : Array[(String, Float)]) -> Array[(String, Float)]部署示例
所有示例均为独立子包,可单独运行。运行前需先安装 MoonBit CLI 工具。
1. 快速开始演示
直接运行主包即可看到 8 大功能模块的完整演示:
输出预览:
2. 基础 API 使用示例
覆盖所有 5 种数据结构的基础操作演示。
参考 examples/basic_usage/main.mbt
3. 游戏排行榜系统
10 阶段完整场景:初始化分数 → TOP 5 查询 → 对局更新 → 排名变化 → 分数统计 → 每日榜单 → 领奖弹出 → 好友关系 → 消息队列 → 状态统计。
参考 examples/leaderboard/main.mbt
展示了如何使用:
4. 电商购物车 & 缓存
10 阶段完整场景:商品缓存 → 购物车 → 浏览历史 → 收藏 → 共同收藏 → 商品标签 → 销量排行 → 会话管理 → 缓存预热 → 状态总览。
参考 examples/shopping_cart/main.mbt
展示了如何使用:
5. CLI 命令行交互
模拟 Redis 风格的命令行交互,自动执行 45 条演示命令并输出结果。
参考 examples/cli_repl/main.mbt
支持解析和执行 60+ 命令,包括 SET/GET/HSET/LPUSH/SADD/ZADD/MSET 等全部命令类型。
许可证
本项目采用 Apache License 2.0 许可证(SPDX 标识符:
Apache-2.0)。详见项目根目录 LICENSE 文件。
Redis 参考范围说明
本项目参考 Redis 的 API 设计思路,基于 MoonBit 语言独立实现。本项目不是 Redis 的移植或 fork,所有代码均为原创实现。
参考范围
本项目参考了 Redis 的以下功能设计:
数据结构(5 种):
命令语义(8 大命令组,80+ 个命令):
Key 过期机制:
内部编码命名(OBJECT_ENCODING 命令):
embstr— String 类型编码listpack— Hash/List/Set/ZSet 小规模编码hashtable— Hash/Set 大规模编码skiplist— ZSet 大规模编码quicklist— List 大规模编码第三方依赖