add will also write information to the
Couchbase Server, but unlike set,
add will fail if the value for a given
key already exists in Couchbase Server.
The reason you would want to use add
instead of set is so that you can
create a new key, without accidentally overwriting an existing
key with the same name. For Couchbase Server, keys must be
unique for every bucket; therefore when you commit new keys to
Couchbase Server, using add may be
preferable based on your application logic.
For example, if you create an application where you store all
new users with a unique username and you want to use usernames
as a keys, you would want to store the new key with
add instead of
set.
If a user already exists in your system with the unique
username, you would not want to overwrite the user with a new
user's information. Instead, you could perform some real-time
feedback and let the user know if the username has already been
taken when the new user fills out their profile information. You
can catch this type of error and report it back in your
application when you use add to create
the document.
Because add fails and reports an error
when a key exists, some Couchbase Server developers prefer it to
set in cases where they create a new
document.
#stores successfully c.add("foo", "bar") #raises Couchbase::Error::KeyExists: failed to store value #failed to store value (key="foo", error=0x0c) c.add("foo", "baz")
This next example demonstrates an add
in PHP:
$script_name=$_SERVER["SCRIPT_NAME"]; $script_access_count=$cb_obj->get($script_name); if($cb_obj->getResultCode() == COUCHBASE_KEY_ENOENT){ #the add will fail if it has already been added $cb_obj->add($script_name,0);
In this example we try to get a script name for the script that currently runs on our web application server. We then try to retrieve any script name that is already stored in Couchbase Server. If we receive a 'key not found' error, we add the script name to Couchbase Server.
In Couchbase SDKs you can store a value with
add while simultaneously providing a
document expiration.
The memcached protocol equivalent for this method is
add. For more information about the
underlying protocol, see
memcached
protocol
If you receive an unexpected 'key exists' error when you use
add you should log the error, and then
go back into your code to determine why the key already exists.
You will want to go back into the application logic that creates
these keys and find out if there is a problem in the logic. One
approach to use to ensure you have unique keys for all your
documents is to use a key generator that creates unique keys for
all documents.
There are application scenarios where you receive a 'key exists' error and you want that error to occur so you can handle it in your application logic. For instance, if you are handling a coupon, and if the coupon key already exists you know the coupon code has already been redeemed. In that case you can use the error to trigger a message to the user that the coupon has already been used.
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 set. 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”