https://github.com/qingant/tiny-redis
如果你安装了Deno的话,可以用一行命令启动一个 Redis 实例:
deno -A https://raw.githubusercontent.com/qingant/tiny-redis/master/mod.ts -h 127.0.0.1 -p 6666
当然现在只有有限的几个命令,不过我会在后面逐步补充完整。TinyRedis 目前不是为了做一个 product ready 的 Redis 替代品,而是为了学习、展示如何用 TypeScript 和 Deno 做网络编程。你可以用 TinyRedis 来构造你自己的 Redis like 应用,例如,跑在浏览器里的 Redis,或者写一个 Redis Client 。
示例:
Client Sample:
// also you can run this by `deno -A https://raw.githubusercontent.com/qingant/tiny-redis/master/samples/cli.ts`
import { encode, RedisValueOf, RedisParser, show } from 'https://raw.githubusercontent.com/qingant/tiny-redis/master/mod.ts';
const main = async () => {
const opts = {
port: 6379,
hostname: "127.0.0.1"
};
// connect to redis server
const conn = await Deno.connect(opts);
// create a redis command and encode it to [Uint8Array]
const cmdEncoded = encode(RedisValueOf.array([
"INFO",
"MEMORY"
]));
// send the command to redis server
for (let i in cmdEncoded) {
await conn.write(cmdEncoded[i]);
}
// create a parser and get the result
const p = new RedisParser(conn);
const req = await p.parse();
console.log(show(req));
};
await main();
Server Side (if you want to implement something that talks redis protocol , Look at this):
import {RedisArray, RedisValue, RedisClient, RedisValueOf, BaseHandler} from 'https://raw.githubusercontent.com/qingant/tiny-redis/master/mod.ts';
class MyHandler extends BaseHandler {
commands = {
'TINY': this.command_TINY
}
private async command_TINY(request: RedisArray): Promise<RedisValue> {
return RedisValueOf.string('REDIS');
}
}
const main = async () => {
const opts = {
port: 6666,
hostname: "0.0.0.0"
};
const listener = Deno.listen(opts);
const handler = new MyHandler();
console.log("Tiny Redis 0.0.1");
console.log(`listening on: ${opts.hostname}:${opts.port}`);
for await (const conn of listener) {
(new RedisClient(conn, handler)).loop();
}
}
main()
Then you can request your TINY
command by:
redis-cli -p 6666 'tiny'
And you will get "REDIS" as response.
It's not that amazing. But if you want your service talk redis protocol so that it can be accessed anywhere with any language, this may be a quick start in TypeScript/Deno world.
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.