用 TypeScript 为 Deno 平台做了一个 Tiny Redis - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
maxint
V2EX    分享创造

用 TypeScript 为 Deno 平台做了一个 Tiny Redis

  •  
  •   maxint 2020-03-22 09:41:47 +08:00 2873 次点击
    这是一个创建于 2077 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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.

    6 条回复    2020-03-23 13:17:49 +08:00
    Cbdy
        1
    Cbdy  
       2020-03-22 09:44:35 +08:00
    妙啊
    maxint
        2
    maxint  
    OP
       2020-03-22 09:45:23 +08:00
    @Cbdy 哈哈,Star 一下吧
    codehz
        3
    codehz  
       2020-03-22 22:19:43 +08:00
    其实 githubusercontent.com 这么长一串可以用 denopkg.com 来代替的(自动 301 重定向,还能附带自动重定向根目录到 mod.ts 的功能 https://denopkg.com/qingant/tiny-redis@master
    autoxbc
        4
    autoxbc  
       2020-03-22 22:24:15 +08:00
    真有勇气,Deno 接口天天变,我魔改一个 https server 每次更新 Deno 都启动不了
    maxint
        5
    maxint  
    OP
       2020-03-23 00:17:56 +08:00 via iPhone
    @codehz 感谢
    maxint
        6
    maxint  
    OP
       2020-03-23 13:17:49 +08:00 via iPhone
    @autoxbc 慢慢就好了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     853 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 20:39 PVG 04:39 LAX 12:39 JFK 15:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86