12. Rust
» borrow checker: a smart and severe mentor
» cargo: simple dependency management
» Stable release cycle
The system won't collapse when you reopen it
3 month later
15. build our own lib from redismodule.h
Redis doesn't provide a lib, only a header file
#include "redismodule.h"
int Export_RedisModule_Init(RedisModuleCtx *ctx,
const char *name,
int ver,
int apiver) {
return RedisModule_Init(ctx, name, ver, apiver);
}
16. build our own lib from redismodule.h
build.rs: Compile C/C++ code into a Rust lib
extern crate gcc;
fn main() {
gcc::Config::new()
.file("src/redismodule.c")
.include("include/")
.compile("libredismodule.a");
}
18. Free resourcewith Droptrait
» RedisModuleString: string type provided by Redis
» Need to be explicitly freed when you're done.
pub struct RedisString {
ctx: *mut raw::RedisModuleCtx,
str_inner: *mut raw::RedisModuleString,
}
19. Free resourcewith Droptrait
impl RedisString {
fn create(ctx: *mut raw::RedisModuleCtx, s: &str) -> RedisString {
let str_inner = raw::create_string(ctx, format!("{}0", s).as_ptr(), s.len());
// ...
}
}
impl Drop for RedisString {
// Frees resources appropriately as a RedisString goes out of scope.
fn drop(&mut self) {
raw::free_string(self.ctx, self.str_inner);
}
}
20. Function Pointer
typedef int (*RedisModuleCmdFunc)
(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
int REDISMODULE_API_FUNC(
RedisModule_CreateCommand
)(..., RedisModuleCmdFunc cmdfunc, ...)
22. Performance
“Informal benchmarks show that redis-cell is pretty
fast, taking a little under twice as long to run as a
basic Redis SET”
https://github.com/brandur/redis-cell