29 import pickle |
29 import pickle |
30 import types |
30 import types |
31 |
31 |
32 from google.appengine.api import api_base_pb |
32 from google.appengine.api import api_base_pb |
33 from google.appengine.api import apiproxy_stub_map |
33 from google.appengine.api import apiproxy_stub_map |
|
34 from google.appengine.api import namespace_manager |
34 from google.appengine.api.memcache import memcache_service_pb |
35 from google.appengine.api.memcache import memcache_service_pb |
35 from google.appengine.runtime import apiproxy_errors |
36 from google.appengine.runtime import apiproxy_errors |
36 |
37 |
37 MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse |
38 MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse |
38 MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest |
39 MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest |
369 self._make_sync_call('memcache', 'FlushAll', request, response) |
370 self._make_sync_call('memcache', 'FlushAll', request, response) |
370 except apiproxy_errors.Error: |
371 except apiproxy_errors.Error: |
371 return False |
372 return False |
372 return True |
373 return True |
373 |
374 |
374 def get(self, key): |
375 def get(self, key, namespace=None): |
375 """Looks up a single key in memcache. |
376 """Looks up a single key in memcache. |
376 |
377 |
377 If you have multiple items to load, though, it's much more efficient |
378 If you have multiple items to load, though, it's much more efficient |
378 to use get_multi() instead, which loads them in one bulk operation, |
379 to use get_multi() instead, which loads them in one bulk operation, |
379 reducing the networking latency that'd otherwise be required to do |
380 reducing the networking latency that'd otherwise be required to do |
380 many serialized get() operations. |
381 many serialized get() operations. |
381 |
382 |
382 Args: |
383 Args: |
383 key: The key in memcache to look up. See docs on Client |
384 key: The key in memcache to look up. See docs on Client |
384 for details of format. |
385 for details of format. |
|
386 namespace: a string specifying an optional namespace to use in |
|
387 the request. |
385 |
388 |
386 Returns: |
389 Returns: |
387 The value of the key, if found in memcache, else None. |
390 The value of the key, if found in memcache, else None. |
388 """ |
391 """ |
389 request = MemcacheGetRequest() |
392 request = MemcacheGetRequest() |
390 request.add_key(_key_string(key)) |
393 request.add_key(_key_string(key)) |
|
394 namespace_manager._add_name_space(request, namespace) |
391 response = MemcacheGetResponse() |
395 response = MemcacheGetResponse() |
392 try: |
396 try: |
393 self._make_sync_call('memcache', 'Get', request, response) |
397 self._make_sync_call('memcache', 'Get', request, response) |
394 except apiproxy_errors.Error: |
398 except apiproxy_errors.Error: |
395 return None |
399 return None |
399 |
403 |
400 return _decode_value(response.item(0).value(), |
404 return _decode_value(response.item(0).value(), |
401 response.item(0).flags(), |
405 response.item(0).flags(), |
402 self._do_unpickle) |
406 self._do_unpickle) |
403 |
407 |
404 def get_multi(self, keys, key_prefix=''): |
408 def get_multi(self, keys, key_prefix='', namespace=None): |
405 """Looks up multiple keys from memcache in one operation. |
409 """Looks up multiple keys from memcache in one operation. |
406 |
410 |
407 This is the recommended way to do bulk loads. |
411 This is the recommended way to do bulk loads. |
408 |
412 |
409 Args: |
413 Args: |
412 does the sharding and hashing automatically, though, so the hash |
416 does the sharding and hashing automatically, though, so the hash |
413 value is ignored. To memcache, keys are just series of bytes, |
417 value is ignored. To memcache, keys are just series of bytes, |
414 and not in any particular encoding. |
418 and not in any particular encoding. |
415 key_prefix: Prefix to prepend to all keys when talking to the server; |
419 key_prefix: Prefix to prepend to all keys when talking to the server; |
416 not included in the returned dictionary. |
420 not included in the returned dictionary. |
|
421 namespace: a string specifying an optional namespace to use in |
|
422 the request. |
417 |
423 |
418 Returns: |
424 Returns: |
419 A dictionary of the keys and values that were present in memcache. |
425 A dictionary of the keys and values that were present in memcache. |
420 Even if the key_prefix was specified, that key_prefix won't be on |
426 Even if the key_prefix was specified, that key_prefix won't be on |
421 the keys in the returned dictionary. |
427 the keys in the returned dictionary. |
422 """ |
428 """ |
423 request = MemcacheGetRequest() |
429 request = MemcacheGetRequest() |
|
430 namespace_manager._add_name_space(request, namespace) |
424 response = MemcacheGetResponse() |
431 response = MemcacheGetResponse() |
425 user_key = {} |
432 user_key = {} |
426 for key in keys: |
433 for key in keys: |
427 request.add_key(_key_string(key, key_prefix, user_key)) |
434 request.add_key(_key_string(key, key_prefix, user_key)) |
428 try: |
435 try: |
435 value = _decode_value(returned_item.value(), returned_item.flags(), |
442 value = _decode_value(returned_item.value(), returned_item.flags(), |
436 self._do_unpickle) |
443 self._do_unpickle) |
437 return_value[user_key[returned_item.key()]] = value |
444 return_value[user_key[returned_item.key()]] = value |
438 return return_value |
445 return return_value |
439 |
446 |
440 def delete(self, key, seconds=0): |
447 def delete(self, key, seconds=0, namespace=None): |
441 """Deletes a key from memcache. |
448 """Deletes a key from memcache. |
442 |
449 |
443 Args: |
450 Args: |
444 key: Key to delete. See docs on Client for detils. |
451 key: Key to delete. See docs on Client for detils. |
445 seconds: Optional number of seconds to make deleted items 'locked' |
452 seconds: Optional number of seconds to make deleted items 'locked' |
446 for 'add' operations. Value can be a delta from current time (up to |
453 for 'add' operations. Value can be a delta from current time (up to |
447 1 month), or an absolute Unix epoch time. Defaults to 0, which means |
454 1 month), or an absolute Unix epoch time. Defaults to 0, which means |
448 items can be immediately added. With or without this option, |
455 items can be immediately added. With or without this option, |
449 a 'set' operation will always work. Float values will be rounded up to |
456 a 'set' operation will always work. Float values will be rounded up to |
450 the nearest whole second. |
457 the nearest whole second. |
|
458 namespace: a string specifying an optional namespace to use in |
|
459 the request. |
451 |
460 |
452 Returns: |
461 Returns: |
453 DELETE_NETWORK_FAILURE (0) on network failure, |
462 DELETE_NETWORK_FAILURE (0) on network failure, |
454 DELETE_ITEM_MISSING (1) if the server tried to delete the item but |
463 DELETE_ITEM_MISSING (1) if the server tried to delete the item but |
455 didn't have it, or |
464 didn't have it, or |
461 raise TypeError('Delete timeout must be a number.') |
470 raise TypeError('Delete timeout must be a number.') |
462 if seconds < 0: |
471 if seconds < 0: |
463 raise ValueError('Delete timeout must be non-negative.') |
472 raise ValueError('Delete timeout must be non-negative.') |
464 |
473 |
465 request = MemcacheDeleteRequest() |
474 request = MemcacheDeleteRequest() |
|
475 namespace_manager._add_name_space(request, namespace) |
466 response = MemcacheDeleteResponse() |
476 response = MemcacheDeleteResponse() |
467 |
477 |
468 delete_item = request.add_item() |
478 delete_item = request.add_item() |
469 delete_item.set_key(_key_string(key)) |
479 delete_item.set_key(_key_string(key)) |
470 delete_item.set_delete_time(int(math.ceil(seconds))) |
480 delete_item.set_delete_time(int(math.ceil(seconds))) |
478 return DELETE_SUCCESSFUL |
488 return DELETE_SUCCESSFUL |
479 elif response.delete_status(0) == MemcacheDeleteResponse.NOT_FOUND: |
489 elif response.delete_status(0) == MemcacheDeleteResponse.NOT_FOUND: |
480 return DELETE_ITEM_MISSING |
490 return DELETE_ITEM_MISSING |
481 assert False, 'Unexpected deletion status code.' |
491 assert False, 'Unexpected deletion status code.' |
482 |
492 |
483 def delete_multi(self, keys, seconds=0, key_prefix=''): |
493 def delete_multi(self, keys, seconds=0, key_prefix='', namespace=None): |
484 """Delete multiple keys at once. |
494 """Delete multiple keys at once. |
485 |
495 |
486 Args: |
496 Args: |
487 keys: List of keys to delete. |
497 keys: List of keys to delete. |
488 seconds: Optional number of seconds to make deleted items 'locked' |
498 seconds: Optional number of seconds to make deleted items 'locked' |
491 items can be immediately added. With or without this option, |
501 items can be immediately added. With or without this option, |
492 a 'set' operation will always work. Float values will be rounded up to |
502 a 'set' operation will always work. Float values will be rounded up to |
493 the nearest whole second. |
503 the nearest whole second. |
494 key_prefix: Prefix to put on all keys when sending specified |
504 key_prefix: Prefix to put on all keys when sending specified |
495 keys to memcache. See docs for get_multi() and set_multi(). |
505 keys to memcache. See docs for get_multi() and set_multi(). |
|
506 namespace: a string specifying an optional namespace to use in |
|
507 the request. |
496 |
508 |
497 Returns: |
509 Returns: |
498 True if all operations completed successfully. False if one |
510 True if all operations completed successfully. False if one |
499 or more failed to complete. |
511 or more failed to complete. |
500 """ |
512 """ |
502 raise TypeError('Delete timeout must be a number.') |
514 raise TypeError('Delete timeout must be a number.') |
503 if seconds < 0: |
515 if seconds < 0: |
504 raise ValueError('Delete timeout must not be negative.') |
516 raise ValueError('Delete timeout must not be negative.') |
505 |
517 |
506 request = MemcacheDeleteRequest() |
518 request = MemcacheDeleteRequest() |
|
519 namespace_manager._add_name_space(request, namespace) |
507 response = MemcacheDeleteResponse() |
520 response = MemcacheDeleteResponse() |
508 |
521 |
509 for key in keys: |
522 for key in keys: |
510 delete_item = request.add_item() |
523 delete_item = request.add_item() |
511 delete_item.set_key(_key_string(key, key_prefix=key_prefix)) |
524 delete_item.set_key(_key_string(key, key_prefix=key_prefix)) |
514 self._make_sync_call('memcache', 'Delete', request, response) |
527 self._make_sync_call('memcache', 'Delete', request, response) |
515 except apiproxy_errors.Error: |
528 except apiproxy_errors.Error: |
516 return False |
529 return False |
517 return True |
530 return True |
518 |
531 |
519 def set(self, key, value, time=0, min_compress_len=0): |
532 def set(self, key, value, time=0, min_compress_len=0, namespace=None): |
520 """Sets a key's value, regardless of previous contents in cache. |
533 """Sets a key's value, regardless of previous contents in cache. |
521 |
534 |
522 Unlike add() and replace(), this method always sets (or |
535 Unlike add() and replace(), this method always sets (or |
523 overwrites) the value in memcache, regardless of previous |
536 overwrites) the value in memcache, regardless of previous |
524 contents. |
537 contents. |
530 from current time (up to 1 month), or an absolute Unix epoch time. |
543 from current time (up to 1 month), or an absolute Unix epoch time. |
531 By default, items never expire, though items may be evicted due to |
544 By default, items never expire, though items may be evicted due to |
532 memory pressure. Float values will be rounded up to the nearest |
545 memory pressure. Float values will be rounded up to the nearest |
533 whole second. |
546 whole second. |
534 min_compress_len: Ignored option for compatibility. |
547 min_compress_len: Ignored option for compatibility. |
|
548 namespace: a string specifying an optional namespace to use in |
|
549 the request. |
535 |
550 |
536 Returns: |
551 Returns: |
537 True if set. False on error. |
552 True if set. False on error. |
538 """ |
553 """ |
539 return self._set_with_policy(MemcacheSetRequest.SET, key, value, time=time) |
554 return self._set_with_policy(MemcacheSetRequest.SET, key, value, time=time, |
540 |
555 namespace=namespace) |
541 def add(self, key, value, time=0, min_compress_len=0): |
556 |
|
557 def add(self, key, value, time=0, min_compress_len=0, namespace=None): |
542 """Sets a key's value, iff item is not already in memcache. |
558 """Sets a key's value, iff item is not already in memcache. |
543 |
559 |
544 Args: |
560 Args: |
545 key: Key to set. See docs on Client for details. |
561 key: Key to set. See docs on Client for details. |
546 value: Value to set. Any type. If complex, will be pickled. |
562 value: Value to set. Any type. If complex, will be pickled. |
548 from current time (up to 1 month), or an absolute Unix epoch time. |
564 from current time (up to 1 month), or an absolute Unix epoch time. |
549 By default, items never expire, though items may be evicted due to |
565 By default, items never expire, though items may be evicted due to |
550 memory pressure. Float values will be rounded up to the nearest |
566 memory pressure. Float values will be rounded up to the nearest |
551 whole second. |
567 whole second. |
552 min_compress_len: Ignored option for compatibility. |
568 min_compress_len: Ignored option for compatibility. |
|
569 namespace: a string specifying an optional namespace to use in |
|
570 the request. |
553 |
571 |
554 Returns: |
572 Returns: |
555 True if added. False on error. |
573 True if added. False on error. |
556 """ |
574 """ |
557 return self._set_with_policy(MemcacheSetRequest.ADD, key, value, time=time) |
575 return self._set_with_policy(MemcacheSetRequest.ADD, key, value, time=time, |
558 |
576 namespace=namespace) |
559 def replace(self, key, value, time=0, min_compress_len=0): |
577 |
|
578 def replace(self, key, value, time=0, min_compress_len=0, namespace=None): |
560 """Replaces a key's value, failing if item isn't already in memcache. |
579 """Replaces a key's value, failing if item isn't already in memcache. |
561 |
580 |
562 Args: |
581 Args: |
563 key: Key to set. See docs on Client for details. |
582 key: Key to set. See docs on Client for details. |
564 value: Value to set. Any type. If complex, will be pickled. |
583 value: Value to set. Any type. If complex, will be pickled. |
566 from current time (up to 1 month), or an absolute Unix epoch time. |
585 from current time (up to 1 month), or an absolute Unix epoch time. |
567 By default, items never expire, though items may be evicted due to |
586 By default, items never expire, though items may be evicted due to |
568 memory pressure. Float values will be rounded up to the nearest |
587 memory pressure. Float values will be rounded up to the nearest |
569 whole second. |
588 whole second. |
570 min_compress_len: Ignored option for compatibility. |
589 min_compress_len: Ignored option for compatibility. |
|
590 namespace: a string specifying an optional namespace to use in |
|
591 the request. |
571 |
592 |
572 Returns: |
593 Returns: |
573 True if replaced. False on RPC error or cache miss. |
594 True if replaced. False on RPC error or cache miss. |
574 """ |
595 """ |
575 return self._set_with_policy(MemcacheSetRequest.REPLACE, |
596 return self._set_with_policy(MemcacheSetRequest.REPLACE, |
576 key, value, time=time) |
597 key, value, time=time, namespace=namespace) |
577 |
598 |
578 def _set_with_policy(self, policy, key, value, time=0): |
599 def _set_with_policy(self, policy, key, value, time=0, namespace=None): |
579 """Sets a single key with a specified policy. |
600 """Sets a single key with a specified policy. |
580 |
601 |
581 Helper function for set(), add(), and replace(). |
602 Helper function for set(), add(), and replace(). |
582 |
603 |
583 Args: |
604 Args: |
584 policy: One of MemcacheSetRequest.SET, .ADD, or .REPLACE. |
605 policy: One of MemcacheSetRequest.SET, .ADD, or .REPLACE. |
585 key: Key to add, set, or replace. See docs on Client for details. |
606 key: Key to add, set, or replace. See docs on Client for details. |
586 value: Value to set. |
607 value: Value to set. |
587 time: Expiration time, defaulting to 0 (never expiring). |
608 time: Expiration time, defaulting to 0 (never expiring). |
|
609 namespace: a string specifying an optional namespace to use in |
|
610 the request. |
588 |
611 |
589 Returns: |
612 Returns: |
590 True if stored, False on RPC error or policy error, e.g. a replace |
613 True if stored, False on RPC error or policy error, e.g. a replace |
591 that failed due to the item not already existing, or an add |
614 that failed due to the item not already existing, or an add |
592 failing due to the item not already existing. |
615 failing due to the item not already existing. |
602 stored_value, flags = _validate_encode_value(value, self._do_pickle) |
625 stored_value, flags = _validate_encode_value(value, self._do_pickle) |
603 item.set_value(stored_value) |
626 item.set_value(stored_value) |
604 item.set_flags(flags) |
627 item.set_flags(flags) |
605 item.set_set_policy(policy) |
628 item.set_set_policy(policy) |
606 item.set_expiration_time(int(math.ceil(time))) |
629 item.set_expiration_time(int(math.ceil(time))) |
|
630 namespace_manager._add_name_space(request, namespace) |
607 response = MemcacheSetResponse() |
631 response = MemcacheSetResponse() |
608 try: |
632 try: |
609 self._make_sync_call('memcache', 'Set', request, response) |
633 self._make_sync_call('memcache', 'Set', request, response) |
610 except apiproxy_errors.Error: |
634 except apiproxy_errors.Error: |
611 return False |
635 return False |
612 if response.set_status_size() != 1: |
636 if response.set_status_size() != 1: |
613 return False |
637 return False |
614 return response.set_status(0) == MemcacheSetResponse.STORED |
638 return response.set_status(0) == MemcacheSetResponse.STORED |
615 |
639 |
616 def _set_multi_with_policy(self, policy, mapping, time=0, key_prefix=''): |
640 def _set_multi_with_policy(self, policy, mapping, time=0, key_prefix='', |
|
641 namespace=None): |
617 """Set multiple keys with a specified policy. |
642 """Set multiple keys with a specified policy. |
618 |
643 |
619 Helper function for set_multi(), add_multi(), and replace_multi(). This |
644 Helper function for set_multi(), add_multi(), and replace_multi(). This |
620 reduces the network latency of doing many requests in serial. |
645 reduces the network latency of doing many requests in serial. |
621 |
646 |
626 from current time (up to 1 month), or an absolute Unix epoch time. |
651 from current time (up to 1 month), or an absolute Unix epoch time. |
627 By default, items never expire, though items may be evicted due to |
652 By default, items never expire, though items may be evicted due to |
628 memory pressure. Float values will be rounded up to the nearest |
653 memory pressure. Float values will be rounded up to the nearest |
629 whole second. |
654 whole second. |
630 key_prefix: Prefix for to prepend to all keys. |
655 key_prefix: Prefix for to prepend to all keys. |
|
656 namespace: a string specifying an optional namespace to use in |
|
657 the request. |
631 |
658 |
632 Returns: |
659 Returns: |
633 A list of keys whose values were NOT set. On total success, |
660 A list of keys whose values were NOT set. On total success, |
634 this list should be empty. On network/RPC/server errors, |
661 this list should be empty. On network/RPC/server errors, |
635 a list of all input keys is returned; in this case the keys |
662 a list of all input keys is returned; in this case the keys |
652 item.set_key(server_key) |
679 item.set_key(server_key) |
653 item.set_value(stored_value) |
680 item.set_value(stored_value) |
654 item.set_flags(flags) |
681 item.set_flags(flags) |
655 item.set_set_policy(policy) |
682 item.set_set_policy(policy) |
656 item.set_expiration_time(int(math.ceil(time))) |
683 item.set_expiration_time(int(math.ceil(time))) |
|
684 namespace_manager._add_name_space(request, namespace) |
657 |
685 |
658 response = MemcacheSetResponse() |
686 response = MemcacheSetResponse() |
659 try: |
687 try: |
660 self._make_sync_call('memcache', 'Set', request, response) |
688 self._make_sync_call('memcache', 'Set', request, response) |
661 except apiproxy_errors.Error: |
689 except apiproxy_errors.Error: |
668 if set_status != MemcacheSetResponse.STORED: |
696 if set_status != MemcacheSetResponse.STORED: |
669 unset_list.append(user_key[server_key]) |
697 unset_list.append(user_key[server_key]) |
670 |
698 |
671 return unset_list |
699 return unset_list |
672 |
700 |
673 def set_multi(self, mapping, time=0, key_prefix='', min_compress_len=0): |
701 def set_multi(self, mapping, time=0, key_prefix='', min_compress_len=0, |
|
702 namespace=None): |
674 """Set multiple keys' values at once, regardless of previous contents. |
703 """Set multiple keys' values at once, regardless of previous contents. |
675 |
704 |
676 Args: |
705 Args: |
677 mapping: Dictionary of keys to values. |
706 mapping: Dictionary of keys to values. |
678 time: Optional expiration time, either relative number of seconds |
707 time: Optional expiration time, either relative number of seconds |
680 By default, items never expire, though items may be evicted due to |
709 By default, items never expire, though items may be evicted due to |
681 memory pressure. Float values will be rounded up to the nearest |
710 memory pressure. Float values will be rounded up to the nearest |
682 whole second. |
711 whole second. |
683 key_prefix: Prefix for to prepend to all keys. |
712 key_prefix: Prefix for to prepend to all keys. |
684 min_compress_len: Unimplemented compatibility option. |
713 min_compress_len: Unimplemented compatibility option. |
|
714 namespace: a string specifying an optional namespace to use in |
|
715 the request. |
685 |
716 |
686 Returns: |
717 Returns: |
687 A list of keys whose values were NOT set. On total success, |
718 A list of keys whose values were NOT set. On total success, |
688 this list should be empty. |
719 this list should be empty. |
689 """ |
720 """ |
690 return self._set_multi_with_policy(MemcacheSetRequest.SET, mapping, |
721 return self._set_multi_with_policy(MemcacheSetRequest.SET, mapping, |
691 time=time, key_prefix=key_prefix) |
722 time=time, key_prefix=key_prefix, |
692 |
723 namespace=namespace) |
693 def add_multi(self, mapping, time=0, key_prefix='', min_compress_len=0): |
724 |
|
725 def add_multi(self, mapping, time=0, key_prefix='', min_compress_len=0, |
|
726 namespace=None): |
694 """Set multiple keys' values iff items are not already in memcache. |
727 """Set multiple keys' values iff items are not already in memcache. |
695 |
728 |
696 Args: |
729 Args: |
697 mapping: Dictionary of keys to values. |
730 mapping: Dictionary of keys to values. |
698 time: Optional expiration time, either relative number of seconds |
731 time: Optional expiration time, either relative number of seconds |
700 By default, items never expire, though items may be evicted due to |
733 By default, items never expire, though items may be evicted due to |
701 memory pressure. Float values will be rounded up to the nearest |
734 memory pressure. Float values will be rounded up to the nearest |
702 whole second. |
735 whole second. |
703 key_prefix: Prefix for to prepend to all keys. |
736 key_prefix: Prefix for to prepend to all keys. |
704 min_compress_len: Unimplemented compatibility option. |
737 min_compress_len: Unimplemented compatibility option. |
|
738 namespace: a string specifying an optional namespace to use in |
|
739 the request. |
705 |
740 |
706 Returns: |
741 Returns: |
707 A list of keys whose values were NOT set because they did not already |
742 A list of keys whose values were NOT set because they did not already |
708 exist in memcache. On total success, this list should be empty. |
743 exist in memcache. On total success, this list should be empty. |
709 """ |
744 """ |
710 return self._set_multi_with_policy(MemcacheSetRequest.ADD, mapping, |
745 return self._set_multi_with_policy(MemcacheSetRequest.ADD, mapping, |
711 time=time, key_prefix=key_prefix) |
746 time=time, key_prefix=key_prefix, |
712 |
747 namespace=namespace) |
713 def replace_multi(self, mapping, time=0, key_prefix='', min_compress_len=0): |
748 |
|
749 def replace_multi(self, mapping, time=0, key_prefix='', min_compress_len=0, |
|
750 namespace=None): |
714 """Replace multiple keys' values, failing if the items aren't in memcache. |
751 """Replace multiple keys' values, failing if the items aren't in memcache. |
715 |
752 |
716 Args: |
753 Args: |
717 mapping: Dictionary of keys to values. |
754 mapping: Dictionary of keys to values. |
718 time: Optional expiration time, either relative number of seconds |
755 time: Optional expiration time, either relative number of seconds |
720 By default, items never expire, though items may be evicted due to |
757 By default, items never expire, though items may be evicted due to |
721 memory pressure. Float values will be rounded up to the nearest |
758 memory pressure. Float values will be rounded up to the nearest |
722 whole second. |
759 whole second. |
723 key_prefix: Prefix for to prepend to all keys. |
760 key_prefix: Prefix for to prepend to all keys. |
724 min_compress_len: Unimplemented compatibility option. |
761 min_compress_len: Unimplemented compatibility option. |
|
762 namespace: a string specifying an optional namespace to use in |
|
763 the request. |
725 |
764 |
726 Returns: |
765 Returns: |
727 A list of keys whose values were NOT set because they already existed |
766 A list of keys whose values were NOT set because they already existed |
728 in memcache. On total success, this list should be empty. |
767 in memcache. On total success, this list should be empty. |
729 """ |
768 """ |
730 return self._set_multi_with_policy(MemcacheSetRequest.REPLACE, mapping, |
769 return self._set_multi_with_policy(MemcacheSetRequest.REPLACE, mapping, |
731 time=time, key_prefix=key_prefix) |
770 time=time, key_prefix=key_prefix, |
732 |
771 namespace=namespace) |
733 def incr(self, key, delta=1): |
772 |
|
773 def incr(self, key, delta=1, namespace=None): |
734 """Atomically increments a key's value. |
774 """Atomically increments a key's value. |
735 |
775 |
736 Internally, the value is a unsigned 64-bit integer. Memcache |
776 Internally, the value is a unsigned 64-bit integer. Memcache |
737 doesn't check 64-bit overflows. The value, if too large, will |
777 doesn't check 64-bit overflows. The value, if too large, will |
738 wrap around. |
778 wrap around. |
744 |
784 |
745 Args: |
785 Args: |
746 key: Key to increment. See Client's docstring for details. |
786 key: Key to increment. See Client's docstring for details. |
747 delta: Non-negative integer value (int or long) to increment key by, |
787 delta: Non-negative integer value (int or long) to increment key by, |
748 defaulting to 1. |
788 defaulting to 1. |
|
789 namespace: a string specifying an optional namespace to use in |
|
790 the request. |
749 |
791 |
750 Returns: |
792 Returns: |
751 New long integer value, or None if key was not in the cache, could not |
793 New long integer value, or None if key was not in the cache, could not |
752 be incremented for any other reason, or a network/RPC/server error |
794 be incremented for any other reason, or a network/RPC/server error |
753 occurred. |
795 occurred. |
754 |
796 |
755 Raises: |
797 Raises: |
756 ValueError: If number is negative. |
798 ValueError: If number is negative. |
757 TypeError: If delta isn't an int or long. |
799 TypeError: If delta isn't an int or long. |
758 """ |
800 """ |
759 return self._incrdecr(key, False, delta) |
801 return self._incrdecr(key, False, delta, namespace=namespace) |
760 |
802 |
761 def decr(self, key, delta=1): |
803 def decr(self, key, delta=1, namespace=None): |
762 """Atomically decrements a key's value. |
804 """Atomically decrements a key's value. |
763 |
805 |
764 Internally, the value is a unsigned 64-bit integer. Memcache |
806 Internally, the value is a unsigned 64-bit integer. Memcache |
765 caps decrementing below zero to zero. |
807 caps decrementing below zero to zero. |
766 |
808 |
769 |
811 |
770 Args: |
812 Args: |
771 key: Key to decrement. See Client's docstring for details. |
813 key: Key to decrement. See Client's docstring for details. |
772 delta: Non-negative integer value (int or long) to decrement key by, |
814 delta: Non-negative integer value (int or long) to decrement key by, |
773 defaulting to 1. |
815 defaulting to 1. |
|
816 namespace: a string specifying an optional namespace to use in |
|
817 the request. |
774 |
818 |
775 Returns: |
819 Returns: |
776 New long integer value, or None if key wasn't in cache and couldn't |
820 New long integer value, or None if key wasn't in cache and couldn't |
777 be decremented, or a network/RPC/server error occurred. |
821 be decremented, or a network/RPC/server error occurred. |
778 |
822 |
779 Raises: |
823 Raises: |
780 ValueError: If number is negative. |
824 ValueError: If number is negative. |
781 TypeError: If delta isn't an int or long. |
825 TypeError: If delta isn't an int or long. |
782 """ |
826 """ |
783 return self._incrdecr(key, True, delta) |
827 return self._incrdecr(key, True, delta, namespace=namespace) |
784 |
828 |
785 def _incrdecr(self, key, is_negative, delta): |
829 def _incrdecr(self, key, is_negative, delta, namespace=None): |
786 """Increment or decrement a key by a provided delta. |
830 """Increment or decrement a key by a provided delta. |
787 |
831 |
788 Args: |
832 Args: |
789 key: Key to increment or decrement. |
833 key: Key to increment or decrement. |
790 is_negative: Boolean, if this is a decrement. |
834 is_negative: Boolean, if this is a decrement. |
791 delta: Non-negative integer amount (int or long) to increment |
835 delta: Non-negative integer amount (int or long) to increment |
792 or decrement by. |
836 or decrement by. |
|
837 namespace: a string specifying an optional namespace to use in |
|
838 the request. |
793 |
839 |
794 Returns: |
840 Returns: |
795 New long integer value, or None on cache miss or network/RPC/server |
841 New long integer value, or None on cache miss or network/RPC/server |
796 error. |
842 error. |
797 |
843 |
803 raise TypeError('Delta must be an integer or long, received %r' % delta) |
849 raise TypeError('Delta must be an integer or long, received %r' % delta) |
804 if delta < 0: |
850 if delta < 0: |
805 raise ValueError('Delta must not be negative.') |
851 raise ValueError('Delta must not be negative.') |
806 |
852 |
807 request = MemcacheIncrementRequest() |
853 request = MemcacheIncrementRequest() |
|
854 namespace_manager._add_name_space(request, namespace) |
808 response = MemcacheIncrementResponse() |
855 response = MemcacheIncrementResponse() |
809 request.set_key(_key_string(key)) |
856 request.set_key(_key_string(key)) |
810 request.set_delta(delta) |
857 request.set_delta(delta) |
811 if is_negative: |
858 if is_negative: |
812 request.set_direction(MemcacheIncrementRequest.DECREMENT) |
859 request.set_direction(MemcacheIncrementRequest.DECREMENT) |