set will write information to the data
store regardless of whether the key for the value already exists
or not. The method is destructive; if the key exists, it will
overwrite any existing value. Typically you want to use
set in cases where you do not care
whether or not you overwrite an existing value, nor do you care
if the key already exists or not. This method is similar to an
INSERT statement in SQL.
For instance, if you have a player location document in a game,
you might not care whether you overwrite the location with a new
value; it is however important that you quickly create a
location document if it does not already exist. In the case of
this type of application logic, you might not want to waste any
code to check if a player location exists; performing rapid
read/writes of the player location and creating the initial
score document may be more important than performing any checks
in your application logic. In this case, using
set would be suitable.
Another scenario is when you populate a database with initial
values. This can be a production or development database. In
this case, you are creating all the initial values for an entire
set of keys. Since you are starting out with an empty database,
and have no risk of overwriting useful data, you would use
set here as well. For instance, if you
are populating your new test database with documents that
represent different planets, you could follow this approach:
Another scenario that is appropriate for using
set is another scenario where you do
not care about overwriting the last value for a key. For
instance if you want to item the last visitor to your site, you
would store that as a key and update it each time a new visitor
is at your site. You might not care who the previous visitors
are; in this case, you use set to
overwrite anything that exists and replace it with the latest
visit information.
This method is the functional equivalent of a RDMS
commit/insert. Set will support the
following parameters which are used during the operation:
Key: unique identifier for the information you want to store. This can be a string, symbol, or numeric sequence. A required parameter.
Value: the information you want to store. This can be in byte-stream, object, JSON document, or even string. A required parameter.
Options: this includes expiration, also known as TTL (time to live), which specifies how long the information remains in the data store before it is automatically flagged for removal and then deleted. You can also specify formatting options and flags.
The following shows a simple example of using set using the Ruby SDK:
c.set("foo", "bar", :ttl => 2)
This operation takes the key foo and sets the
string 'bar' for the key which will expire after 2 seconds. This
next example is part of a data loader script in PHP which reads
in different JSON files in a specified directory. It then sends
requests to write each file to Couchbase Server:
function import($cb, $dir) { $d = dir($dir); while (false !== ($file = $d->read())) { if (substr($file, -5) != '.json') continue; echo "adding $file\n"; $json = json_decode(file_get_contents($dir . $file), true); unset($json["_id"]); echo $cb->set(substr($file, 0, -5), json_encode($json)); echo "\n"; } }
The first part of this function takes a Couchbase client
instance as a parameter and a directory. It then assigns the
directory to a local variable $d and opens
it. The while loop will continue reading each
file in the directory so long as we have not finished reading
all the files. In the loop we detect if the file has the
.json file type or not so we know to handle
it, and store it. If the file is json we decode it, remove the
attribute _id and then set the key as the
filename with the other file contents as value. We choose this
different key for better identification in our system. The
following illustrates a sample JSON file which you can use with
this loader:
{ "_id":"beer_#40_Golden_Lager", "brewery":"Minocqua Brewing Company", "name":"#40 Golden Lager", "updated":"2010-07-22 20:00:20" }
To view the complete loader application and sample data available on GitHub, see Couchbase Labs: Beer Sample
In Couchbase SDKs you can store a value with
set while simultaneously providing a
document expiration.
Set will return a boolean of true if it
is able to successfully commit data to the databases; it can
also return a CAS value, which is a unique identifier for the
item, and is used for optimistic locking.
The associated memcached protocol in ASCII is
set which stores a key. For more
information, see
memcached
protocol
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 item incompatible with Couchbase Server.
For more information about connections and connection-level settings, see Section 6.4.4, “Optimizing Client Instances” and Section 6.6.1, “Client-Side Timeouts”