add time command

This commit is contained in:
siddontang 2014-10-08 13:55:46 +08:00
parent 05d251da1f
commit fefc39c429
7 changed files with 54 additions and 2 deletions

View File

@ -137,6 +137,7 @@ class Ledis(object):
'PING': lambda r: nativestr(r) == 'PONG', 'PING': lambda r: nativestr(r) == 'PONG',
'SET': lambda r: r and nativestr(r) == 'OK', 'SET': lambda r: r and nativestr(r) == 'OK',
'INFO': parse_info, 'INFO': parse_info,
'TIME': lambda x: (int(x[0]), int(x[1])),
} }
@ -1035,6 +1036,13 @@ class Ledis(object):
def scriptflush(self): def scriptflush(self):
return self.execute_command('SCRIPT', 'FLUSH') return self.execute_command('SCRIPT', 'FLUSH')
def time(self):
"""
Returns the server time as a 2-item tuple of ints:
(seconds since epoch, microseconds into this second).
"""
return self.execute_command('TIME')
class Transaction(Ledis): class Transaction(Ledis):
def __init__(self, connection_pool, response_callbacks): def __init__(self, connection_pool, response_callbacks):

View File

@ -8,6 +8,7 @@ module.exports = [
"info", "info",
"flushall", "flushall",
"flushdb", "flushdb",
"time",
"bget", "bget",
"bdelete", "bdelete",

View File

@ -153,6 +153,7 @@ local commands = {
"info", "info",
"flushall", "flushall",
"flushdb", "flushdb",
"time",
-- [[transaction]] -- [[transaction]]
"begin", "begin",

View File

@ -1,4 +1,4 @@
//This file was generated by .tools/generate_commands.py on Thu Oct 02 2014 15:24:07 +0800 //This file was generated by .tools/generate_commands.py on Wed Oct 08 2014 13:52:31 +0800
package main package main
var helpCommands = [][]string{ var helpCommands = [][]string{
@ -96,6 +96,7 @@ var helpCommands = [][]string{
{"SUNIONSTORE", "destination key [key ...]", "Set"}, {"SUNIONSTORE", "destination key [key ...]", "Set"},
{"SXSCAN", "key [MATCH match] [COUNT count]", "Set"}, {"SXSCAN", "key [MATCH match] [COUNT count]", "Set"},
{"SYNC", "logid", "Replication"}, {"SYNC", "logid", "Replication"},
{"TIME", "-", "Server"},
{"TTL", "key", "KV"}, {"TTL", "key", "KV"},
{"XSCAN", "key [MATCH match] [COUNT count]", "KV"}, {"XSCAN", "key [MATCH match] [COUNT count]", "KV"},
{"ZADD", "key score member [score member ...]", "ZSet"}, {"ZADD", "key score member [score member ...]", "ZSet"},

View File

@ -628,6 +628,11 @@
"arguments" : "-", "arguments" : "-",
"group": "Script", "group": "Script",
"readonly": false "readonly": false
} },
"TIME": {
"arguments" : "-",
"group": "Server",
"readonly": true
}
} }

View File

@ -132,6 +132,7 @@ Table of Contents
- [FLUSHALL](#flushall) - [FLUSHALL](#flushall)
- [FLUSHDB](#flushdb) - [FLUSHDB](#flushdb)
- [INFO [section]](#info-section) - [INFO [section]](#info-section)
- [TIME](#time)
- [Transaction](#transaction) - [Transaction](#transaction)
- [BEGIN](#begin) - [BEGIN](#begin)
- [ROLLBACK](#rollback) - [ROLLBACK](#rollback)
@ -2570,6 +2571,14 @@ The optional parameter can be used to select a specific section of information:
When no parameter is provided, all will return. When no parameter is provided, all will return.
### TIME
The TIME command returns the current server time as a two items lists: a Unix timestamp and the amount of microseconds already elapsed in the current second
**Return value**
array: two elements, one is unix time in seconds, the other is microseconds.
## Transaction ## Transaction
### BEGIN ### BEGIN

View File

@ -2,8 +2,11 @@ package server
import ( import (
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/num"
"strconv" "strconv"
"strings" "strings"
"time"
) )
func pingCommand(c *client) error { func pingCommand(c *client) error {
@ -85,6 +88,29 @@ func flushdbCommand(c *client) error {
return nil return nil
} }
func timeCommand(c *client) error {
if len(c.args) != 0 {
return ErrCmdParams
}
t := time.Now()
//seconds
s := t.Unix()
n := t.UnixNano()
//micro seconds
m := (n - s*1e9) / 1e3
ay := []interface{}{
num.FormatInt64ToSlice(s),
num.FormatInt64ToSlice(m),
}
c.resp.writeArray(ay)
return nil
}
func init() { func init() {
register("ping", pingCommand) register("ping", pingCommand)
register("echo", echoCommand) register("echo", echoCommand)
@ -92,4 +118,5 @@ func init() {
register("info", infoCommand) register("info", infoCommand)
register("flushall", flushallCommand) register("flushall", flushallCommand)
register("flushdb", flushdbCommand) register("flushdb", flushdbCommand)
register("time", timeCommand)
} }