本文整理汇总了Python中couchbase._libcouchbase.Connection类的典型用法代码示例。如果您正苦于以下问题:Python Connection类的具体用法?Python Connection怎么用?Python Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set
def set(self, key, value, cas=0, ttl=0, format=None):
"""Unconditionally store the object in Couchbase.
:param key: The key to set the value with. By default, the key must be
either a :class:`bytes` or :class:`str` object encodable as UTF-8.
If a custom `transcoder` class is used (see :meth:`__init__`), then
the key object is passed directly to the transcoder, which may
serialize it how it wishes.
:type key: string or bytes
:param value: The value to set for the key. The type for `value`
follows the same rules as for `key`
:param int cas: The _CAS_ value to use. If supplied, the value will only
be stored if it already exists with the supplied CAS
:param int ttl: If specified, the key will expire after this many
seconds
:param int format: If specified, indicates the `format` to use when
encoding the value. If none is specified, it will use the
`default_format`
For more info see
:attr:`~couchbase.libcouchbase.Connection.default_format`
:raise: :exc:`couchbase.exceptions.ArgumentError` if an
argument is supplied that is not applicable in this context.
For example setting the CAS as a string.
:raise: :exc:`couchbase.exceptions.ConnectError` if the
connection closed
:raise: :exc:`couchbase.exceptions.KeyExistsError` if the key
already exists on the server with a different CAS value.
:raise: :exc:`couchbase.exceptions.ValueFormatError` if the
value cannot be serialized with chosen encoder, e.g. if you
try to store a dictionaty in plain mode.
:return: :class:`~couchbase.libcouchbase.Result`
Simple set::
cb.set('key', 'value')
Force JSON document format for value::
cb.set('foo', {'bar': 'baz'}, format=couchbase.FMT_JSON)
Perform optimistic locking by specifying last known CAS version::
cb.set('foo', 'bar', cas=8835713818674332672)
Several sets at the same time (mutli-set)::
cb.set_multi({'foo': 'bar', 'baz': 'value'})
.. seealso::
:meth:`set_multi`
"""
return _Base.set(self, key, value, cas, ttl, format)
示例2: append
def append(self, key, value, cas=0, ttl=0, format=None):
"""
Append a string to an existing value in Couchbase.
This follows the same conventions as
:meth:`~couchbase.libcouchbase.Connection.set`.
The `format` argument must be one of :const:`~couchbase.FMT_UTF8` or
:const:`~couchbase.FMT_BYTES`. If not specified, it will be
:const:`~couchbase.FMT_UTF8`
(overriding the :attr:`default_format` attribute).
This is because JSON or Pickle formats will be nonsensical when
random data is appended to them. If you wish to modify a JSON or
Pickle encoded object, you will need to retrieve it (via :meth:`get`),
modify it, and then store it again (using :meth:`set`).
Additionally, you must ensure the value (and flags) for the current
value is compatible with the data to be appended. For an example,
you may append a :const:`~couchbase.FMT_BYTES` value to an existing
:const:`~couchbase.FMT_JSON` value, but an error will be thrown when
retrieving the value using
:meth:`get` (you may still use the :attr:`data_passthrough` to
overcome this).
:raise: :exc:`couchbase.exceptions.NotStoredError` if the key does
not exist
.. seealso::
:meth:`set`
:meth:`append_multi`
"""
return _Base.append(self, key, value, ttl=ttl, cas=cas, format=format)
示例3: set_multi
def set_multi(self, keys, ttl=0, format=None):
"""Set multiple keys
This follows the same semantics as
:meth:`~couchbase.libcouchbase.Connection.set`
:param dict keys: A dictionary of keys to set. The keys are the keys
as they should be on the server, and the values are the values for
the keys to be stored
:param int ttl: If specified, sets the expiration value for all
keys
:param int format:
If specified, this is the conversion format which will be used for
_all_ the keys.
:return: A :class:`~couchbase.libcouchbase.MultiResult` object, which
is a `dict` subclass.
The multi methods are more than just a convenience, they also save on
network performance by batch-scheduling operations, reducing latencies.
This is especially noticeable on smaller value sizes.
.. seealso::
:meth:`set`
"""
return _Base.set_multi(self, keys, ttl=ttl, format=format)
示例4: add_multi
def add_multi(self, keys, ttl=0, format=None):
"""Add multiple keys.
Multi variant of :meth:`~couchbase.connection.Connection.add`
.. seealso:: :meth:`add`, :meth:`set_multi`, :meth:`set`
"""
return _Base.add_multi(self, keys, ttl=ttl, format=format)
示例5: get
def get(self, key, ttl=0, quiet=None):
"""Obtain an object stored in Couchbase by given key.
:param string key: The key to fetch. The type of key is the same
as mentioned in :meth:`set`
:param int ttl:
If specified, indicates that the key's expiration time should be
*modified* when retrieving the value.
:param boolean quiet: causes `get` to return None instead of
raising an exception when the key is not found. It defaults
to the value set by
:attr:`~couchbase.libcouchbase.Connection.quiet` on the instance.
In `quiet` mode, the error may still be obtained by inspecting
the :attr:`~couchbase.libcouchbase.Result.rc` attribute of the
:class:`Result` object, or checking :attr:`Result.success`.
Note that the default value is `None`, which means to use
the :attr:`quiet`. If it is a boolean (i.e. `True` or `False) it will
override the `Connection`-level `quiet` attribute.
:raise: :exc:`couchbase.exceptions.NotFoundError` if the key
is missing in the bucket
:raise: :exc:`couchbase.exceptions.ConnectError` if the
connection closed
:raise: :exc:`couchbase.exceptions.ValueFormatError` if the
value cannot be deserialized with chosen decoder, e.g. if you
try to retreive an object stored with an unrecognized format
:return: A :class:`~couchbase.libcouchbase.Result` object
Simple get::
value = cb.get('key').value
Get multiple values::
cb.get_multi(['foo', 'bar'])
# { 'foo' : <Result(...)>, 'bar' : <Result(...)> }
Inspect the flags::
rv = cb.get("key")
value, flags, cas = rv.value, rv.flags, rv.cas
Update the expiration time::
rv = cb.get("key", ttl=10)
# Expires in ten seconds
.. seealso::
:meth:`get_multi`
"""
return _Base.get(self, key, ttl, quiet)
示例6: add_multi
def add_multi(self, keys, ttl=0, format=None, persist_to=0, replicate_to=0):
"""Add multiple keys.
Multi variant of :meth:`~couchbase.connection.Connection.add`
.. seealso:: :meth:`add`, :meth:`set_multi`, :meth:`set`
"""
return _Base.add_multi(self, keys, ttl=ttl, format=format,
persist_to=persist_to, replicate_to=replicate_to)
示例7: rget
def rget(self, key, replica_index=None, quiet=None):
"""
Get a key from a replica
:param string key: The key to fetch
:param int replica_index: The replica index to fetch.
If this is ``None`` then this method will return once any replica
responds. Use :attr:`configured_replica_count` to figure out the
upper bound for this parameter.
The value for this parameter must be a number between 0 and the
value of :attr:`configured_replica_count`-1.
:param boolean quiet: Whether to suppress errors when the key is not
found
This function (if `replica_index` is not supplied) functions like
the :meth:`get` method that has been passed the `replica` parameter::
c.get(key, replica=True)
.. seealso::
:meth:`get` :meth:`rget_multi`
"""
if replica_index is not None:
return _Base._rgetix(self, key, replica=replica_index, quiet=quiet)
else:
return _Base._rget(self, key, quiet=quiet)
示例8: unlock
def unlock(self, key, cas):
"""Unlock a Locked Key in Couchbase.
:param key: The key to unlock
:param cas: The cas returned from
:meth:`lock`'s :class:`Result` object.
Unlock a previously-locked key in Couchbase. A key is
locked by a call to :meth:`lock`.
See :meth:`lock` for an example.
:raise: :exc:`couchbase.exceptions.KeyExistsError` if the CAS
supplied does not match the CAS on the server (possibly because
it was unlocked by previous call).
.. seealso::
:meth:`lock`
:meth:`unlock_multi`
"""
return _Base.unlock(self, key, cas=cas)
示例9: endure_multi
def endure_multi(self, keys, persist_to=-1, replicate_to=-1,
timeout=5.0,
interval=0.010,
check_removed=False):
"""
.. versionadded:: 1.1.0
Check durability requirements for multiple keys
:param keys: The keys to check
The type of keys may be one of the following:
* Sequence of keys
* A :class:`~couchbase.result.MultiResult` object
* A ``dict`` with CAS values as the dictionary value
* A sequence of :class:`~couchbase.result.Result` objects
:return: A :class:`~couchbase.result.MultiResult` object of
:class:`~couchbase.result.OperationResult` items.
.. seealso:: :meth:`endure`
"""
return _Base.endure_multi(self, keys, persist_to, replicate_to,
timeout=timeout,
interval=interval,
check_removed=check_removed)
示例10: touch_multi
def touch_multi(self, keys, ttl=0):
"""Touch multiple keys
Multi variant of :meth:`touch`
:param keys: the keys to touch
:type keys: :ref:`iterable<argtypes>`
``keys`` can also be a dictionary with values being integers, in
whic case the value for each key will be used as the TTL instead
of the global one (i.e. the one passed to this function)
:param int ttl: The new expiration time
:return: A :class:`~couchbase.libcouchbase.MultiResult` object
Update three keys to expire in 10 seconds ::
cb.touch_multi(("key1", "key2", "key3"), ttl=10)
Update three keys with different expiration times ::
cb.touch_multi({"foo" : 1, "bar" : 5, "baz" : 10})
.. seealso::
:meth:`touch`
"""
return _Base.touch_multi(self, keys, ttl=ttl)
示例11: touch
def touch(self, key, ttl=0):
"""Update a key's expiration time
:param string key: The key whose expiration time should be modified
:param int ttl: The new expiration time. If the expiration time is ``0``
then the key never expires (and any existing expiration is removed)
:return: :class:`OperationResult`
Update the expiration time of a key ::
cb.set("key", ttl=100)
# expires in 100 seconds
cb.touch("key", ttl=0)
# key should never expire now
:raise: The same things that :meth:`get` does
.. seealso::
:meth:`get` - which can be used to get *and* update the expiration
:meth:`touch_multi`
"""
return _Base.touch(self, key, ttl=ttl)
示例12: unlock_multi
def unlock_multi(self, keys):
"""Unlock multiple keys
Multi variant of :meth:`unlock`
:param dict keys: the keys to unlock
:return: a :class:`MultiResult` object
The value of the ``keys`` argument should be either the CAS, or a
previously returned :class:`Result` object from a :meth:`lock` call.
Effectively, this means you may pass a :class:`MultiResult` as the
``keys`` argument.
Thus, you can do something like ::
keys = (....)
rvs = cb.lock_multi(keys, ttl=5)
# do something with rvs
cb.unlock_multi(rvs)
.. seealso::
:meth:`unlock`
"""
return _Base.unlock_multi(self, keys)
示例13: delete
def delete(self, key, cas=0, quiet=None):
"""Remove the key-value entry for a given key in Couchbase.
:param key: A string which is the key to delete. The format and type
of the key follows the same conventions as in :meth:`set`
:type key: string, dict, or tuple/list
:param int cas: The CAS to use for the removal operation.
If specified, the key will only be deleted from the server if
it has the same CAS as specified. This is useful to delete a
key only if its value has not been changed from the version
currently visible to the client.
If the CAS on the server does not match the one specified,
an exception is thrown.
:param boolean quiet:
Follows the same semantics as `quiet` in :meth:`get`
:raise: :exc:`couchbase.exceptions.NotFoundError` if the key
does not exist on the bucket
:raise: :exc:`couchbase.exceptions.KeyExistsError` if a CAS
was specified, but the CAS on the server had changed
:raise: :exc:`couchbase.exceptions.ConnectError` if the
connection was closed
:return: A :class:`~couchbase.libcouchbase.Result` object.
Simple delete::
ok = cb.delete("key").success
Don't complain if key does not exist::
ok = cb.delete("key", quiet=True)
Only delete if CAS matches our version::
rv = cb.get("key")
cb.delete("key", cas=rv.cas)
Remove multiple keys::
oks = cb.delete_multi(["key1", "key2", "key3"])
Remove multiple keys with CAS::
oks = cb.delete({
"key1" : cas1,
"key2" : cas2,
"key3" : cas3
})
.. seealso::
:meth:`delete_multi`
"""
return _Base.delete(self, key, cas, quiet)
示例14: prepend_multi
def prepend_multi(self, keys, ttl=0, format=None):
"""Prepend to multiple keys.
Multi variant of :meth:`prepend`
.. seealso:: :meth:`prepend`, :meth:`set_multi`, :meth:`set`
"""
return _Base.prepend_multi(self, keys, ttl=ttl, format=format)
示例15: replace_multi
def replace_multi(self, keys, ttl=0, format=None):
"""Replace multiple keys.
Multi variant of :meth:`replace`
.. seealso:: :meth:`replace`, :meth:`set_multi`, :meth:`set`
"""
return _Base.replace_multi(self, keys, ttl=ttl, format=format)