{"id":26082,"date":"2022-07-04T13:14:08","date_gmt":"2022-07-04T03:14:08","guid":{"rendered":"https:\/\/database.guide\/?p=26082"},"modified":"2022-07-17T11:54:31","modified_gmt":"2022-07-17T01:54:31","slug":"redis-restore-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-restore-command-explained\/","title":{"rendered":"Redis RESTORE Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>RESTORE<\/code> command creates a key associated with a value that is obtained by deserialising the provided serialised value (obtained via the <code><a href=\"https:\/\/database.guide\/redis-dump-command-explained\/\" data-type=\"post\" data-id=\"26074\">DUMP<\/a><\/code> command).<\/p>\n\n\n\n<p>The serialisation format contains a 64-bit checksum, as well as the RDB version. The <code>RESTORE<\/code> command checks the RDB version and data checksum. If they don&#8217;t match an error is returned.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>RESTORE key ttl serialized-value &#91;REPLACE] &#91;ABSTTL] &#91;IDLETIME seconds] &#91;FREQ frequency]<\/code><\/pre>\n\n\n\n<p>Explanation of the arguments: <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>key<\/code><\/td><td>The key to create and store the deserialised data.<\/td><\/tr><tr><td><code>ttl<\/code><\/td><td>The time to live. If&nbsp;<code>ttl<\/code>&nbsp;is <code>0<\/code> the key is created without any expire, otherwise the specified expire time (in milliseconds) is set.<\/td><\/tr><tr><td><code>serialized-value<\/code><\/td><td>The serialised value to restore.<\/td><\/tr><tr><td><code>[REPLACE]<\/code><\/td><td>Replaces the key if it already exists. Without this, an error will occur if the key already exists.<\/td><\/tr><tr><td><code>[ABSTTL]<\/code><\/td><td>When this modifier is used,&nbsp;<code>ttl<\/code> represents an absolute&nbsp;Unix timestamp&nbsp;(in milliseconds) in which the key will expire.<\/td><\/tr><tr><td><code>[IDLETIME seconds]<\/code><\/td><td>This is the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key. <\/td><\/tr><tr><td><code>[FREQ freqency]<\/code><\/td><td>The access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key. Can be used for eviction purposes.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RESTORE type 0 \"\\x00\\x03Dog\\n\\x00\\x14\\x069\\xb24\\x14\\xe4\\xd6\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>In this case, I created a key called <code>type<\/code> and restored the serialised value to it. I also set a <code>ttl<\/code> of <code>0<\/code>, which means that it has no expiry.<\/p>\n\n\n\n<p>Let&#8217;s check the value of our new <code>type<\/code> key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Dog\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When the Key Already Exists<\/h2>\n\n\n\n<p>By default, if the key already exists, an error is returned. <\/p>\n\n\n\n<p>To demonstrate this let&#8217;s run the previous code again (but with a slightly different serialised value):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RESTORE type 0 \"\\x00\\x03Cat\\n\\x00\\x10\\x1e\\xeaH\\xafMl\\x17\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) BUSYKEY Target key name already exists.<\/pre>\n\n\n\n<p>As expected, we got an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>REPLACE<\/code> Argument<\/h2>\n\n\n\n<p>We can use the <code>REPLACE<\/code> argument to replace the key if it already exists.<\/p>\n\n\n\n<p>Let&#8217;s try running the previous code again, but this time we&#8217;ll add the <code>REPLACE<\/code> argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RESTORE type 0 \"\\x00\\x03Cat\\n\\x00\\x10\\x1e\\xeaH\\xafMl\\x17\" REPLACE<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>This time the operation succeeded without error.<\/p>\n\n\n\n<p>Let&#8217;s check the key&#8217;s value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Cat\"<\/pre>\n\n\n\n<p>So we can see that the new deserialised value has been assigned to the key. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set an Expiry Time<\/h2>\n\n\n\n<p>The previous examples set a <code>ttl<\/code> of <code>0<\/code>, which means that the key has no expiry. We can use a value other than <code>0<\/code> to set an expire time, in milliseconds, for the key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RESTORE type 60000 \"\\x00\\x03Cat\\n\\x00\\x10\\x1e\\xeaH\\xafMl\\x17\" REPLACE<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Now let&#8217;s check the time to live of the key before it expires:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 55940<\/pre>\n\n\n\n<p>Running <code><a href=\"https:\/\/database.guide\/redis-pttl-command-explained\/\" data-type=\"post\" data-id=\"26158\">PTTL<\/a><\/code> returns the current time to live, in milliseconds. We can alternatively use the <code><a href=\"https:\/\/database.guide\/redis-ttl-command-explained\/\" data-type=\"post\" data-id=\"26179\">TTL<\/a><\/code> command to return the time to live in seconds. Anyway, if we run <code>PTTL<\/code> again, we can see that it has decremented:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PTTL type<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 15263<\/pre>\n\n\n\n<p>We can alternatively specify the time to live as an absolute&nbsp;Unix timestamp&nbsp;(in milliseconds). When doing this, we need to add the <code>ABSTTL<\/code> argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>DUMP<\/code> Command<\/h2>\n\n\n\n<p>We can get serialised data by using the <code>DUMP<\/code> command. The <code><a href=\"https:\/\/database.guide\/redis-dump-command-explained\/\" data-type=\"post\" data-id=\"26074\">DUMP<\/a><\/code> command serialises the value stored at a specified key in a Redis-specific format and returns it to the user. <\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DUMP type<\/code><\/pre>\n\n\n\n<p>That returns the value of the key as a serialised value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the RESTORE command creates a key associated with a value that is obtained by deserialising the provided serialised value (obtained via the DUMP command). The serialisation format contains a 64-bit checksum, as well as the RDB version. The RESTORE command checks the RDB version and data checksum. If they don&#8217;t match an error &#8230; <a title=\"Redis RESTORE Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-restore-command-explained\/\" aria-label=\"Read more about Redis RESTORE Command Explained\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-26082","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=26082"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26082\/revisions"}],"predecessor-version":[{"id":26443,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26082\/revisions\/26443"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}