With the touch method you can update
the expiration time on a given key. This can be useful for
situations where you want to prevent an item from expiring
without resetting the associated value. For example, for a
session database you might want to keep the session alive in the
database each time the user accesses a web page without
explicitly updating the session value, keeping the user's
session active and available.
The other context when you might want to use touch is if you
want to test if a key actually exists. As we mentioned earlier
for our discussion of get, developers
will typically rely on get to determine
if a key exists. The unintended problem this can cause is if the
value for a key is several megabytes instead of mere bytes. If
you constantly use get only to test if
a key exists, you nonetheless retrieve the entire value; this
can cause a performance loss if the value is large, and
especially if you perform the get over
thousands or millions of documents.
Using touch as an alternative way to
test if a key exists may be a preferable. Since
touch only updates the expiration and
does not retrieve the item value, the request payload and
response are both small. The most important drawback to be aware
of is that when you use touch to determine if a key exists, you
assume that the key does not have an expiration time that is
used in other parts of your application. If the expiration time
for a key is important, when you use
touch it will overwrite that expiration
and will impact application behavior. If you are certain the key
expiration is not important, than using
touch in this context is safe.
The other drawback of using this approach is that if you perform
a touch to determine if a key exists,
you are working on the assumption that it will not expire by the
time you perform your next operation on that key. If you only
want to test for the existence of a key, and you do not want to
update the expiration, you can provide the existing expiration,
or set it to 0, which indicates no expiration. The following
illustrations demonstrate how you can use the result from
touch to decide whether you store a
key, or store an alternate key:
The following shows and example of using
touch in the Ruby SDK:
# updates the expiration time to 10 seconds for 'foo' document c.touch("foo", :ttl => 10)
As we discussed earlier in this chapter, the SDKs provide a
convenience method you can use to retrieve a document and update
the expiration. With these so called get-and-touch operations
you do not need to perform a separate setting operation to
update expiration when you are retrieving the document. This
will also provide better performance compared to doing a
separate get and
touch requests. If you use separate
calls to get and
touch you will create two requests to
Couchbase Server and two responses from the server per document;
in contrast you create only one request and response when you
use a get-and-touch method.
The equivalent call in the memcached protocol is
touch which updates the item
expiration. For more information, see
memcached
protocol.
As we mention previously, you can perform a
touch to explicitly test whether a key
exists or not, and then create a key; you can try this with
set or add. If
a key is missing, Couchbase Server will return a 'key not found'
type error which you can check. Be aware that when you use this
approach, you are assuming the key will not
expire before you perform your next operation on it.
If the key is missing and you did not expect this result, you should check any application logic that creates that type of key, or any logic that deletes it may inadvertently cause this result. Another reason why you might get this result is that the item expired and Couchbase Server deleted it as part of the regular maintenance. So you will want to check any logic that sets an explicit expiration set for that key.
The types of errors that can occur during this operation include 1) inability to connect to a node, or 2) some error exists while attempting to format a value being retrieved. If you have a connection-level error you may need to reattempt connection, and possibly check the status of the server. If you have an error with the size of your value or formatting, you need to check the value itself, and how it is encoded and see if there are any issues that make the document incompatible with Couchbase Server.
For more information about connections and connection-level settings, see Section 7.5.4, “Optimizing Client Instances” and Section 7.7.1, “Client-Side Timeouts”