插件参考:aaz.redis
协议参考:http://doc.redisfans.com/topic/protocol.html
改进:
1、按参考协议实现传输内容编解码;
2、增加subscribe实现,通过拉起异步套接字,实现event事件。
3、函数通过元方法统一访问。
参考代码:
//redis客户端
import myplu.redis.protocol;
import wsock.tcp.client;
import wsock.tcp.asynClient;
namespace myplu
class redis{
ctor(){
..table.gc(this, "close")
};
close = function(){
if(this.tcpClient){
this.tcpClient.close();
this.tcpClient = null;
}
};
connect = function(ip="127.0.0.1", port=6379, timeout=3){
this.ip = ip;
this.port = port;
this.timeout = timeout;
this.tcpClient := ..wsock.tcp.client();
if(!this.tcpClient.connectTimeout(ip, port, timeout)){
return false, "连接服务器失败";
}
return true;
};
setTimeouts = function(send=5000,receive=5000){
if(this.tcpClient){
return this.tcpClient.setTimeouts(send, receive);
}
};
subscribe = function(callback, ...){
if(!this.tcpClient1){
//import wsock.tcp.asynClient;
this.tcpClient1 = ..wsock.tcp.asynClient();
this.tcpClient1.connect(this.ip,this.port);
//因为是异步套接字,这里不需要检查返回值
}
if(callback && type(callback) == "function"){
this.tcpClient1.onReceive = function(err){
var buffer = ..raw.buffer(0x100000);
var readSize = this.tcpClient1.recvBuffer(buffer);
if( readSize ) {
buffer = ..string.str(buffer);
buffer = this.decode(buffer);
callback(buffer);
}
}
}
var msg = this.encode("SUBSCRIBE", ...);
this.tcpClient1.send(msg);
};
scanHandle = function(str,reg,pos=1){
var data = ..string.match(str,reg,pos);
var index,last = ..string.find(str,reg,pos);
return data,last+1;
};
scanString = function(str,pos=1){
var res = ..string.match(str,"\$(\d+)\r\n",pos);
return this.scanHandle(str,"\$\d+\r\n([\w\W]{"+res+"})<\r\n>?",pos);
};
scanNumber = function(str,pos=1){
var data,index = this.scanHandle(str,"\:(\d+)<\r\n>?",pos);
return tonumber(data),index;
};
scanArray = function(str,pos=1){
var res = ..string.match(str,"\*(\d+)<\r\n>?",pos);
res = tonumber(res);
var index,last = ..string.find(str,"\*(\d+)<\r\n>?",pos);
pos = last + 1;
var data = {};
var dt;
for(i=1;res;1){
dt,pos = this.scanAny(str,pos);
..table.push(data,dt);
}
return data,pos;
};
scanStatus = function(str,pos=1){
return this.scanHandle(str,"\+([\w\W]+)<\r\n>?",pos);
};
scanError = function(str,pos=1){
return this.scanHandle(str,"-([\w\W]+)<\r\n>?",pos);
};
scanAny = function(str,pos=1){
var prefix = str[[pos]];
select(prefix) {
case "+" {
return this.scanStatus(str,pos);
}
case "-" {
return this.scanError(str,pos);
}
case ":" {
return this.scanNumber(str,pos);
}
case "$" {
return this.scanString(str,pos);
}
case "*" {
return this.scanArray(str,pos);
}else {
return null,#str;
}
}
};
decode = function (str){
var pos = 1;
var data;
var res = {};
do{
data,pos = this.scanAny(str,pos);
..table.push(res,data);
}while(pos<#str)
if(#res==1){
return res[1];
}
return res;
};
encode = function (...){
var args = {...};
var data = ..string.format('*%i\r\n', #args);
for(k,v in args){
v = tostring(v);
data += ..string.format('$%i\r\n%s\r\n', #v, v);
}
return data;
};
@_meta;
}
namespace redis
_meta = {
_get = function(k,ownerCall){
//console.log("*********************");
if(ownerCall){
return function(...){
var msg = owner.encode(..string.upper(k), ...);
var data,err = owner.tcpClient.send(msg);
if(data){
var buffer = ..raw.buffer(0x100000);
var readSize = owner.tcpClient.recvBuffer(buffer);
if( readSize ) {
buffer = ..string.str(buffer);
return owner.decode(buffer);
}
return null;
}else {
error(err,2);
}
}
}
}
}
/*****intellisense()
myplu.redis = 导入redis库
myplu.redis() = 创建redis实例\n!redis_client.
end intellisense*****/
/*****intellisense(!redis_client)
connect(.(ip,6379,3) = 创建redis链接,所有参数都可选
close() = 关闭redis链接
setTimeouts(.(超时时间,响应超时时间) = 设置超时时间
subscribe(__) = 订阅通知
del(.(__) = 执行del命令
dump(.(__) = 执行dump命令
exists(.(__) = 执行exists命令
expire(.(__) = 执行expire命令
expireat(.(__) = 执行expireat命令
keys(.(__) = 执行keys命令
migrate(.(__) = 执行migrate命令
move(.(__) = 执行move命令
object(.(__) = 执行object命令
persist(.(__) = 执行persist命令
pexpire(.(__) = 执行pexpire命令
pexpireat(.(__) = 执行pexpireat命令
pttl(.(__) = 执行pttl命令
randomkey(.(__) = 执行randomkey命令
rename(.(__) = 执行rename命令
renamenx(.(__) = 执行renamenx命令
restore(.(__) = 执行restore命令
sort(.(__) = 执行sort命令
ttl(.(__) = 执行ttl命令
type(.(__) = 执行type命令
scan(.(__) = 执行scan命令
append(.(__) = 执行append命令
bitcount(.(__) = 执行bitcount命令
bitop(.(__) = 执行bitop命令
decr(.(__) = 执行decr命令
decrby(.(__) = 执行decrby命令
get(.(__) = 执行get命令
getbit(.(__) = 执行getbit命令
getrange(.(__) = 执行getrange命令
getset(.(__) = 执行getset命令
incr(.(__) = 执行incr命令
incrby(.(__) = 执行incrby命令
incrbyfloat(.(__) = 执行incrbyfloat命令
mget(.(__) = 执行mget命令
mset(.(__) = 执行mset命令
msetnx(.(__) = 执行msetnx命令
psetex(.(__) = 执行psetex命令
set(.(__) = 执行set命令
setbit(.(__) = 执行setbit命令
setex(.(__) = 执行setex命令
setnx(.(__) = 执行setnx命令
setrange(.(__) = 执行setrange命令
strlen(.(__) = 执行strlen命令
hdel(.(__) = 执行hdel命令
hexists(.(__) = 执行hexists命令
hget(.(__) = 执行hget命令
hgetall(.(__) = 执行hgetall命令
hincrby(.(__) = 执行hincrby命令
hincrbyfloat(.(__) = 执行hincrbyfloat命令
hkeys(.(__) = 执行hkeys命令
hlen(.(__) = 执行hlen命令
hmget(.(__) = 执行hmget命令
hmset(.(__) = 执行hmset命令
hset(.(__) = 执行hset命令
hsetnx(.(__) = 执行hsetnx命令
hvals(.(__) = 执行hvals命令
hscan(.(__) = 执行hscan命令
blpop(.(__) = 执行blpop命令
brpop(.(__) = 执行brpop命令
brpoplpush(.(__) = 执行brpoplpush命令
lindex(.(__) = 执行lindex命令
linsert(.(__) = 执行linsert命令
llen(.(__) = 执行llen命令
lpop(.(__) = 执行lpop命令
lpush(.(__) = 执行lpush命令
lpushx(.(__) = 执行lpushx命令
lrange(.(__) = 执行lrange命令
lrem(.(__) = 执行lrem命令
lset(.(__) = 执行lset命令
ltrim(.(__) = 执行ltrim命令
rpop(.(__) = 执行rpop命令
rpoplpush(.(__) = 执行rpoplpush命令
rpush(.(__) = 执行rpush命令
rpushx(.(__) = 执行rpushx命令
sadd(.(__) = 执行sadd命令
scard(.(__) = 执行scard命令
sdiff(.(__) = 执行sdiff命令
sdiffstore(.(__) = 执行sdiffstore命令
sinter(.(__) = 执行sinter命令
sinterstore(.(__) = 执行sinterstore命令
sismember(.(__) = 执行sismember命令
smembers(.(__) = 执行smembers命令
smove(.(__) = 执行smove命令
spop(.(__) = 执行spop命令
srandmember(.(__) = 执行srandmember命令
srem(.(__) = 执行srem命令
sunion(.(__) = 执行sunion命令
sunionstore(.(__) = 执行sunionstore命令
sscan(.(__) = 执行sscan命令
zadd(.(__) = 执行zadd命令
zcard(.(__) = 执行zcard命令
zcount(.(__) = 执行zcount命令
zincrby(.(__) = 执行zincrby命令
zrange(.(__) = 执行zrange命令
zrangebyscore(.(__) = 执行zrangebyscore命令
zrank(.(__) = 执行zrank命令
zrem(.(__) = 执行zrem命令
zremrangebyrank(.(__) = 执行zremrangebyrank命令
zremrangebyscore(.(__) = 执行zremrangebyscore命令
zrevrange(.(__) = 执行zrevrange命令
zrevrangebyscore(.(__) = 执行zrevrangebyscore命令
zrevrank(.(__) = 执行zrevrank命令
zscore(.(__) = 执行zscore命令
zunionstore(.(__) = 执行zunionstore命令
zinterstore(.(__) = 执行zinterstore命令
zscan(.(__) = 执行zscan命令
psubscribe(.(__) = 执行psubscribe命令
publish(.(__) = 执行publish命令
pubsub(.(__) = 执行pubsub命令
punsubscribe(.(__) = 执行punsubscribe命令
subscribe(.(__) = 执行subscribe命令
unsubscribe(.(__) = 执行unsubscribe命令
discard(.(__) = 执行discard命令
exec(.(__) = 执行exec命令
multi(.(__) = 执行multi命令
unwatch(.(__) = 执行unwatch命令
watch(.(__) = 执行watch命令
eval(.(__) = 执行eval命令
evalsha(.(__) = 执行evalsha命令
script_exists(.(__) = 执行script_exists命令
script_flush(.(__) = 执行script_flush命令
script_kill(.(__) = 执行script_kill命令
script_load(.(__) = 执行script_load命令
auth(.(__) = 执行auth命令
echo(.(__) = 执行echo命令
ping(.(__) = 执行ping命令
quit(.(__) = 执行quit命令
select(.(__) = 执行select命令
bgrewriteaof(.(__) = 执行bgrewriteaof命令
bgsave(.(__) = 执行bgsave命令
client_getname(.(__) = 执行client_getname命令
client_kill(.(__) = 执行client_kill命令
client_list(.(__) = 执行client_list命令
client_setname(.(__) = 执行client_setname命令
config_get(.(__) = 执行config_get命令
config_resetstat(.(__) = 执行config_resetstat命令
config_rewrite(.(__) = 执行config_rewrite命令
config_set(.(__) = 执行config_set命令
dbsize(.(__) = 执行dbsize命令
debug_object(.(__) = 执行debug_object命令
debug_segfault(.(__) = 执行debug_segfault命令
flushall(.(__) = 执行flushall命令
flushdb(.(__) = 执行flushdb命令
info(.(__) = 执行info命令
lastsave(.(__) = 执行lastsave命令
monitor(.(__) = 执行monitor命令
psync(.(__) = 执行psync命令
save(.(__) = 执行save命令
shutdown(.(__) = 执行shutdown命令
slaveof(.(__) = 执行slaveof命令
slowlog(.(__) = 执行slowlog命令
sync(.(__) = 执行sync命令
time(.(__) = 执行time命令
end intellisense*****/