{"id":26319,"date":"2022-07-16T11:53:29","date_gmt":"2022-07-16T01:53:29","guid":{"rendered":"https:\/\/database.guide\/?p=26319"},"modified":"2022-07-16T11:53:29","modified_gmt":"2022-07-16T01:53:29","slug":"redis-touch-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-touch-command-explained\/","title":{"rendered":"Redis TOUCH Command Explained"},"content":{"rendered":"\n<p>The Redis <code>TOUCH<\/code> command alters the last access time of a key or keys. It allows us to alter the access time of a key without accessing it directly.<\/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>TOUCH key &#91;key ...]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we create a key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET player \"Jet\"<\/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>And we then use <code>OBJECT IDLETIME<\/code> to find out how many seconds has gone past since that key was accessed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME player<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 12<\/pre>\n\n\n\n<p>So far it&#8217;s been 12 seconds. If we wait a bit, and run it again, the idle time increases:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME player<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 102<\/pre>\n\n\n\n<p>The idle time increases with each second that passes. Now it&#8217;s been 102 seconds.<\/p>\n\n\n\n<p>Now let&#8217;s use <code>TOUCH<\/code> to reset the idle time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TOUCH player<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>We get an integer reply of 1, which means that one key was touched.<\/p>\n\n\n\n<p>Now let&#8217;s quickly run <code>OBJECT IDLETIME<\/code> again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME player<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 8<\/pre>\n\n\n\n<p>It&#8217;s back down to 8 and increasing again with every second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Keys<\/h2>\n\n\n\n<p>We can apply <code>TOUCH<\/code> to multiple keys at once.<\/p>\n\n\n\n<p>Let&#8217;s check another key&#8217;s idle time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME age<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 536642<\/pre>\n\n\n\n<p>This key is a lot older than the previous one and it hasn&#8217;t been accessed in a while.<\/p>\n\n\n\n<p>Let&#8217;s use <code>TOUCH<\/code> against both keys:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TOUCH player age<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 2<\/pre>\n\n\n\n<p>This time we get an integer reply of 2.<\/p>\n\n\n\n<p>And then check their idle time again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME player<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 7<\/pre>\n\n\n\n<p>And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OBJECT IDLETIME age<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 14<\/pre>\n\n\n\n<p>I guess 7 seconds must&#8217;ve passed between the time I ran the first one and the second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Keys<\/h2>\n\n\n\n<p>If a specified key doesn&#8217;t exist, then it&#8217;s ignored:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TOUCH nonexistentkey<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Redis TOUCH command alters the last access time of a key or keys. It allows us to alter the access time of a key without accessing it directly.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-26319","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\/26319","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=26319"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26319\/revisions"}],"predecessor-version":[{"id":26328,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26319\/revisions\/26328"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}