randomkey
随机弹出一个key
test:2>randomKey
"ants_monitor_recent_app_change_delete_list"
select
切换库
test:2>select 3
"OK"
test:3>
swapdb
交换库
sit:2>swapdb 1 2
"OK"
sit:2>
move
移动数据到库
sit:2>set abc 123
"OK"
sit:2>move abc 3
"1"
rename
更换键值对名称,如果新key原来有数据 直接替换
sit:2>set a 1
"OK"
sit:2>set b a
"OK"
sit:2>rename b a
"OK"
sit:2>get a
"a"
sit:2>get b
null
renamenx
如果新key原来有数据 不替换
sit:2>set a 1
"OK"
sit:2>set b 2
"OK"
sit:2>renamenx a b
"0"
sit:2>get a
"1"
sit:2>get b
"2"
ping
ping pong 主从 分布式中常用
sit:2>ping
"PONG"
echo
打印
sit:2>echo helloworld
"helloworld"
save
rbd保存
bgsave
异步保存
bgrewriteaof
异步执行一个 AOF 文件重写操作
shutdown
关闭redis
lastsave
返回最近一次 Redis 成功将数据保存到磁盘上的时间
type
返回类型
sit:2>type a
"string"
multi/exec/discard/unwatch/watch
事务开始
/* Exec the command */
if (c->flags & CLIENT_MULTI &&
c->cmd->proc != execCommand && c->cmd->proc != discardCommand &&
c->cmd->proc != multiCommand && c->cmd->proc != watchCommand)
{
queueMultiCommand(c);
addReply(c,shared.queued);
} else {
call(c,CMD_CALL_FULL);
c->woff = server.master_repl_offset;
if (listLength(server.ready_keys))
handleClientsBlockedOnLists();
}
事务的本质实际上就是保存命令到mstate.commands
中,然后执行exec的时候,依次执行上面命令。discard的时候取消(释放命令)
里面涉及三个状态
#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
#define CLIENT_MULTI (1<<3) /* This client is in a MULTI context */
#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
- CLIENT_MULTI 标记当前服务器处于事务处理阶段
- CLIENT_DIRTY_CAS 是否监听的数据被其他人改了
- CLIENT_DIRTY_EXEC 当前提交是否有发生错误
sit:2>multi
"OK"
sit:2>set a 2
"QUEUED"
sit:2>set a 3
"QUEUED"
sit:2>exec
1) "OK"
2) "OK"
sit:2>get a
"3"
sync/psync
sync 是全量同步命令,psync是部分同步命令,在redis都是用的相同的方法
/* SYNC and PSYNC command implemenation. */
void syncCommand(client *c) {
/* ignore SYNC if already slave or in monitor mode */
if (c->flags & CLIENT_SLAVE) return;
/* Refuse SYNC requests if we are a slave but the link with our master
* is not ok... */
if (server.masterhost && server.repl_state != REPL_STATE_CONNECTED) {
addReplySds(c,sdsnew("-NOMASTERLINK Can't SYNC while not connected with my master\r\n"));
return;
}
/* SYNC can't be issued when the server has pending data to send to
* the client about already issued commands. We need a fresh reply
* buffer registering the differences between the BGSAVE and the current
* dataset, so that we can copy to other slaves if needed. */
if (clientHasPendingReplies(c)) {
addReplyError(c,"SYNC and PSYNC are invalid with pending output");
return;
}
serverLog(LL_NOTICE,"Slave %s asks for synchronization",
replicationGetSlaveName(c));
/* Try a partial resynchronization if this is a PSYNC command.
* If it fails, we continue with usual full resynchronization, however
* when this happens masterTryPartialResynchronization() already
* replied with:
*
* +FULLRESYNC <replid> <offset>
*
* So the slave knows the new replid and offset to try a PSYNC later
* if the connection with the master is lost. */
if (!strcasecmp(c->argv[0]->ptr,"psync")) {
if (masterTryPartialResynchronization(c) == C_OK) {
server.stat_sync_partial_ok++;
return; /* No full resync needed, return. */
} else {
char *master_replid = c->argv[1]->ptr;
/* Increment stats for failed PSYNCs, but only if the
* replid is not "?", as this is used by slaves to force a full
* resync on purpose when they are not albe to partially
* resync. */
if (master_replid[0] != '?') server.stat_sync_partial_err++;
}
} else {
/* If a slave uses SYNC, we are dealing with an old implementation
* of the replication protocol (like redis-cli --slave). Flag the client
* so that we don't expect to receive REPLCONF ACK feedbacks. */
c->flags |= CLIENT_PRE_PSYNC;
}
/* Full resynchronization. */
server.stat_sync_full++;
/* Setup the slave as one waiting for BGSAVE to start. The following code
* paths will change the state if we handle the slave differently. */
c->replstate = SLAVE_STATE_WAIT_BGSAVE_START;
if (server.repl_disable_tcp_nodelay)
anetDisableTcpNoDelay(NULL, c->fd); /* Non critical if it fails. */
c->repldbfd = -1;
c->flags |= CLIENT_SLAVE;
listAddNodeTail(server.slaves,c);
/* Create the replication backlog if needed. */
if (listLength(server.slaves) == 1 && server.repl_backlog == NULL) {
/* When we create the backlog from scratch, we always use a new
* replication ID and clear the ID2, since there is no valid
* past history. */
changeReplicationId();
clearReplicationId2();
createReplicationBacklog();
}
/* CASE 1: BGSAVE is in progress, with disk target. */
if (server.rdb_child_pid != -1 &&
server.rdb_child_type == RDB_CHILD_TYPE_DISK)