When you perform a get you may also
want to update the expiration for that document. This is called
a 'get-and-touch' operation and can be completed in a single
request to Couchbase Server. This saves you the time from having
to do a get and then a separate operation to update the
expiration. This method is useful for scenarios where you have a
document that should eventually expire, but perhaps you want to
keep that document around when it is still in use by the
application. Therefore when you retrieve the document, you also
update the expiration so it will be in Couchbase Server for a
bit longer.
Going back to our spaceship game example, imagine that we have a special mode that a player can achieve. For instance, if they hit a special target they have temporary powers and can score more points for the next 30 seconds of play.
In this case, you could represent this temporary play mode as a document named username_power_up_mode for instance. The document could have attributes related to this special play mode, such as double-points or triple-point scoring. Since the special play mode will only last 30 seconds, when you get the power_up_mode document you could also update the expiration so that it will also only exist for the next 30 seconds. To do this, you would perform a get-and-touch operation.
If you need to constantly retrieve a document and update it to
keep it stored longer, this method will also improve your
application performance, when you compare it to using separate
get and touch
calls. When you use the separate calls, you effectively double
the number of requests and responses between your application
and Couchbase Server, thereby increasing response and request
times and decreasing application performance. Therefore
get-and-touch is preferable for heavy retrieve operations where
you also want to update document expiration.
The next example demonstrates a get-and-touch in Ruby:
val = c.get("foo", :ttl => 10)
The Couchbase SDK get-and-touch methods are based on the
memcached protocol command get with a
specified expiration. For more information about the protocol,
see
memcached
protocol.
If a key does not exist, you will get a 'key does not exist' type error in response. If 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 returns a 'key not found.' So you will want to check any explicit expiration set for that key.
One option to handle this result is to create the value and set
the new expiration; you can attempt this with
set or add.
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”