本帖最后由 pyaauto 于 2016-11-1 12:32 编辑
[redis.aardio]
- class redis{
- ctor(host,port){
- this._context=wsock.tcp.client();
- if(null===this._context.connect(host,port))
- return null,"Connection server failed";
- this.is_read = true;
- ..table.add_gc( this,"close");
- };
- type=me;
- @_meta;
- close = function(){
- this._context.close();
- }
- _read_str=function(redis_str){
- return string.slice(redis_str,2,#redis_str);
- }
- _read_int=function(redis_str){
- return tonumber(string.slice(redis_str,2,#redis_str));
- }
- _read_bulk=function(redis_str){
- var pos= string.indexOf(redis_str,'\r\n');
- if(!pos) return ;
- var data_num = tonumber(string.slice(redis_str,2,pos));
- if( !data_num or data_num === -1) return ;
- return string.slice(redis_str,pos+2,pos+2+data_num);
- }
- _read_err=function(redis_str){
- return string.slice(redis_str,2,#redis_str);
- }
- _redis_read=function(socket_data){
- socket_data =string.trimleft(socket_data);
- var first_char= string.left(socket_data,1)
- select(first_char) {
- case "+" {
- return this._read_str(socket_data);
- }
- case "-" {
- return this._read_err(socket_data);
- }
- case ":" {
- return this._read_int(socket_data);
- }
- case "$" {
- return this._read_bulk(socket_data);
- }
- case "*" {
- var pos= string.indexOf(socket_data,'\r\n');
- if(!pos) return ;
- var tab={}
-
- var remove = pos + 2;
- while(1){
- socket_data = string.slice(socket_data,remove,#socket_data);
- if(type(socket_data) !== type.string) break ;
- pos= string.indexOf(socket_data,'\r\n');
- if(!pos) break ;
-
- var ret;
- var line_data=string.slice(socket_data,1,pos);
- var line_type = string.left(line_data,1)
-
- ret = this._redis_read( line_data );
- if(line_type==="$"){
-
- var line_num = tonumber(string.slice(line_data,2,#line_data));
-
- if(line_num === -1){
- ret = this._redis_read(line_data );
- }else {
- //多行
- line_data = string.left(socket_data,#line_data+2+line_num)
- ret = this._redis_read( line_data );
- }
- }else {
- ret = this._redis_read( line_data );
- }
- remove = #line_data+2;
- if(ret) table.push(tab,ret);
- }
- return tab:{};
- }
- else {
- return null;
- }
- }
- }
-
- commands=function(...){
- var t={...};
- var r = '\r\n';
- var num = #t
-
- var cmdtxt = "*"++num++r;
- for(i=1;num;1){
- t[i] = tostring(t[i])
- cmdtxt += "$"++#t[i]++r;
- cmdtxt += t[i]++r
- }
-
- this._context.send(cmdtxt,#cmdtxt)
- if( this.is_read ) return this._redis_read(this._context.recv());
- }
-
- get_array =function(redis_array,sn=0,dn=-1) {
- return this.commands("LRANGE",redis_array,sn,dn);
- }
-
- set_array=function (redis_array,redis_array_val) {
- this.is_read =false;
- this.commands("DEL",redis_array);
- for(i=1;#redis_array_val;1){
- this.commands("RPUSH",redis_array,redis_array_val[i]);
- }
- this.is_read = true;
- return this._context.recv();
- }
- }
- namespace redis {
- import wsock.tcp.client;
- string = ..string;
- table = ..table;
- _meta = {
- _get = function( k ){
- if( type(k) == "string" && k[1]=='_'# ) return;
- if(!k) return;
- if(!owner[['commands']]) return ;
-
- var api = {
- _cmd = k;
- _cmdfunc=owner[['commands']];
- @_meta;
- }
- owner[[k]] = api;
- return api;
- }
- _call = function( ... ){
- return owner._cmdfunc(owner._cmd,...)
- }
- }
- }
复制代码import console
import redis
console.open()
db=redis('127.0.0.1',6379)
//返回服务器时间
console.varDump(db.time())
//将字符串值 value 关联到 key
db.set('key',12345)
//返回 key 所关联的字符串值
console.log(db.get('key'))
//追加字符串
db.append("key","56789")
console.log("test",db.get("key"))
//保存数组
db.set_array("isarray",{"hello";"test";"test1"})
//读取数组
console.varDump(db.get_array("isarray"))
//删除
console.log(db.del('isarray'))
console.log(db.del('key'))
//模糊读取
db.set("key_1","value_1")
db.set("key_2","value_2")
db.set("key_3","value_3")
db.set("key_11","value_4")
db.set("key_12","value_5")
console.varDump(db.keys("key_*"))
console.varDump(db.keys("key_?"))
//计数
console.log("crkey",1)
db.set("crkey",1)
console.log("将 key 中储存的数字值加一");
db.incr("crkey");
console.log(db.get("crkey"))
console.log("将 key 中储存的数字值减一");
db.decr("crkey");
console.log(db.get("crkey"))
//以秒为单位设置过期时间
console.log(db.setex("exkey",2,"exvalue"))
console.log(db.get("exkey"),type( db.get("exkey")))
console.log("返回剩余生存时间:",db.pttl("exkey"))
sleep(2000)
console.log(db.get("exkey"),type( db.get("exkey")))
console.pause() |