thirdparty/google_appengine/google/appengine/api/memcache/__init__.py
changeset 2864 2e0b0af889be
parent 2413 d0b7dac5325c
child 3031 7678f72140e6
equal deleted inserted replaced
2862:27971a13089f 2864:2e0b0af889be
   344       self._make_sync_call('memcache', 'Stats', request, response)
   344       self._make_sync_call('memcache', 'Stats', request, response)
   345     except apiproxy_errors.Error:
   345     except apiproxy_errors.Error:
   346       return None
   346       return None
   347 
   347 
   348     if not response.has_stats():
   348     if not response.has_stats():
   349       return None
   349       return {
       
   350         STAT_HITS: 0,
       
   351         STAT_MISSES: 0,
       
   352         STAT_BYTE_HITS: 0,
       
   353         STAT_ITEMS: 0,
       
   354         STAT_BYTES: 0,
       
   355         STAT_OLDEST_ITEM_AGES: 0,
       
   356       }
   350 
   357 
   351     stats = response.stats()
   358     stats = response.stats()
   352     return {
   359     return {
   353       STAT_HITS: stats.hits(),
   360       STAT_HITS: stats.hits(),
   354       STAT_MISSES: stats.misses(),
   361       STAT_MISSES: stats.misses(),
   768     """
   775     """
   769     return self._set_multi_with_policy(MemcacheSetRequest.REPLACE, mapping,
   776     return self._set_multi_with_policy(MemcacheSetRequest.REPLACE, mapping,
   770                                        time=time, key_prefix=key_prefix,
   777                                        time=time, key_prefix=key_prefix,
   771                                        namespace=namespace)
   778                                        namespace=namespace)
   772 
   779 
   773   def incr(self, key, delta=1, namespace=None):
   780   def incr(self, key, delta=1, namespace=None, initial_value=None):
   774     """Atomically increments a key's value.
   781     """Atomically increments a key's value.
   775 
   782 
   776     Internally, the value is a unsigned 64-bit integer.  Memcache
   783     Internally, the value is a unsigned 64-bit integer.  Memcache
   777     doesn't check 64-bit overflows.  The value, if too large, will
   784     doesn't check 64-bit overflows.  The value, if too large, will
   778     wrap around.
   785     wrap around.
   779 
   786 
   780     The key must already exist in the cache to be incremented.  To
   787     Unless an initial_value is specified, the key must already exist
   781     initialize a counter, set() it to the initial value, as an
   788     in the cache to be incremented.  To initialize a counter, either
       
   789     specify initial_value or set() it to the initial value, as an
   782     ASCII decimal integer.  Future get()s of the key, post-increment,
   790     ASCII decimal integer.  Future get()s of the key, post-increment,
   783     will still be an ASCII decimal value.
   791     will still be an ASCII decimal value.
   784 
   792 
   785     Args:
   793     Args:
   786       key: Key to increment.  See Client's docstring for details.
   794       key: Key to increment.  See Client's docstring for details.
   787       delta: Non-negative integer value (int or long) to increment key by,
   795       delta: Non-negative integer value (int or long) to increment key by,
   788         defaulting to 1.
   796         defaulting to 1.
   789       namespace: a string specifying an optional namespace to use in
   797       namespace: a string specifying an optional namespace to use in
   790         the request.
   798         the request.
       
   799       initial_value: initial value to put in the cache, if it doesn't
       
   800         already exist.  The default value, None, will not create a cache
       
   801         entry if it doesn't already exist.
   791 
   802 
   792     Returns:
   803     Returns:
   793       New long integer value, or None if key was not in the cache, could not
   804       New long integer value, or None if key was not in the cache, could not
   794       be incremented for any other reason, or a network/RPC/server error
   805       be incremented for any other reason, or a network/RPC/server error
   795       occurred.
   806       occurred.
   796 
   807 
   797     Raises:
   808     Raises:
   798       ValueError: If number is negative.
   809       ValueError: If number is negative.
   799       TypeError: If delta isn't an int or long.
   810       TypeError: If delta isn't an int or long.
   800     """
   811     """
   801     return self._incrdecr(key, False, delta, namespace=namespace)
   812     return self._incrdecr(key, False, delta, namespace=namespace,
   802 
   813                           initial_value=initial_value)
   803   def decr(self, key, delta=1, namespace=None):
   814 
       
   815   def decr(self, key, delta=1, namespace=None, initial_value=None):
   804     """Atomically decrements a key's value.
   816     """Atomically decrements a key's value.
   805 
   817 
   806     Internally, the value is a unsigned 64-bit integer.  Memcache
   818     Internally, the value is a unsigned 64-bit integer.  Memcache
   807     caps decrementing below zero to zero.
   819     caps decrementing below zero to zero.
   808 
   820 
   813       key: Key to decrement.  See Client's docstring for details.
   825       key: Key to decrement.  See Client's docstring for details.
   814       delta: Non-negative integer value (int or long) to decrement key by,
   826       delta: Non-negative integer value (int or long) to decrement key by,
   815         defaulting to 1.
   827         defaulting to 1.
   816       namespace: a string specifying an optional namespace to use in
   828       namespace: a string specifying an optional namespace to use in
   817         the request.
   829         the request.
       
   830       initial_value: initial value to put in the cache, if it doesn't
       
   831         already exist.  The default value, None, will not create a cache
       
   832         entry if it doesn't already exist.
   818 
   833 
   819     Returns:
   834     Returns:
   820       New long integer value, or None if key wasn't in cache and couldn't
   835       New long integer value, or None if key wasn't in cache and couldn't
   821       be decremented, or a network/RPC/server error occurred.
   836       be decremented, or a network/RPC/server error occurred.
   822 
   837 
   823     Raises:
   838     Raises:
   824       ValueError: If number is negative.
   839       ValueError: If number is negative.
   825       TypeError: If delta isn't an int or long.
   840       TypeError: If delta isn't an int or long.
   826     """
   841     """
   827     return self._incrdecr(key, True, delta, namespace=namespace)
   842     return self._incrdecr(key, True, delta, namespace=namespace,
   828 
   843                           initial_value=initial_value)
   829   def _incrdecr(self, key, is_negative, delta, namespace=None):
   844 
       
   845   def _incrdecr(self, key, is_negative, delta, namespace=None,
       
   846                 initial_value=None):
   830     """Increment or decrement a key by a provided delta.
   847     """Increment or decrement a key by a provided delta.
   831 
   848 
   832     Args:
   849     Args:
   833       key: Key to increment or decrement.
   850       key: Key to increment or decrement.
   834       is_negative: Boolean, if this is a decrement.
   851       is_negative: Boolean, if this is a decrement.
   835       delta: Non-negative integer amount (int or long) to increment
   852       delta: Non-negative integer amount (int or long) to increment
   836         or decrement by.
   853         or decrement by.
   837       namespace: a string specifying an optional namespace to use in
   854       namespace: a string specifying an optional namespace to use in
   838         the request.
   855         the request.
       
   856       initial_value: initial value to put in the cache, if it doesn't
       
   857         already exist.  The default value, None, will not create a cache
       
   858         entry if it doesn't already exist.
   839 
   859 
   840     Returns:
   860     Returns:
   841       New long integer value, or None on cache miss or network/RPC/server
   861       New long integer value, or None on cache miss or network/RPC/server
   842       error.
   862       error.
   843 
   863 
   857     request.set_delta(delta)
   877     request.set_delta(delta)
   858     if is_negative:
   878     if is_negative:
   859       request.set_direction(MemcacheIncrementRequest.DECREMENT)
   879       request.set_direction(MemcacheIncrementRequest.DECREMENT)
   860     else:
   880     else:
   861       request.set_direction(MemcacheIncrementRequest.INCREMENT)
   881       request.set_direction(MemcacheIncrementRequest.INCREMENT)
       
   882     if initial_value is not None:
       
   883       request.set_initial_value(long(initial_value))
   862 
   884 
   863     try:
   885     try:
   864       self._make_sync_call('memcache', 'Increment', request, response)
   886       self._make_sync_call('memcache', 'Increment', request, response)
   865     except apiproxy_errors.Error:
   887     except apiproxy_errors.Error:
   866       return None
   888       return None