Redis 4.0最强指令即其原理

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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker Redis 4.0是通过Docker容器运行Redis 4.0版本的一个实例。以下是安装和使用Docker Redis 4.0的步骤: 1. 首先,您需要通过Docker命令查询Redis 4.0镜像并将其拉取到本地系统中。可以使用以下命令执行这一步骤: `docker search redis` `docker pull redis:4.0` 2. 下一步是创建一个目录,并在其中创建一个名为redis.conf的配置文件。如果您不手动创建该文件,Docker在运行时将自动创建一个目录,但没有配置文件。可以使用以下命令执行这一步骤: `mkdir -p /usr/local/redis/conf` `cd /usr/local/redis/conf` `touch redis.conf` 3. 然后,您需要启动容器来运行Redis 4.0实例。在这个例子中,我们使用的端口号是16379,您可以根据需要修改为常用的6379端口。可以使用以下命令执行这一步骤: `docker run -p 16379:6379 --name redis4_0_11 --privileged=true -v /usr/local/redis/data:/data -v /usr/local/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:4.0.11 redis-server /etc/redis/redis.conf --requirepass "123456" --appendonly yes` 4. 当容器成功启动后,您可以使用以下命令来查看Docker容器的状态: `docker ps -a` 5. 最后,您可以使用Redis Desktop Manager或其他工具来测试与Docker Redis 4.0的连接。确保连接正常后,您就可以开始使用Redis 4.0了。 请注意,上述步骤中的命令可能需要根据您的实际情况进行调整,例如目录路径或容器名称等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Docker——Docker安装Redis4.0](https://blog.csdn.net/weixin_43835659/article/details/103910746)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [基于docker的redis4.0单机集群搭建](https://blog.csdn.net/qq_34341457/article/details/103801238)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值