My favorites
▼
|
Sign in
googleappengine
Google App Engine
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
python
/
google
/
appengine
/
api
/
memcache
/
__init__.py
‹r245
r265
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Memcache API.
Provides memcached-alike API to application developers to store
data in memory when reliable storage via the DataStore API isn't
required and higher performance is desired.
"""
import cPickle
import cStringIO
import hashlib
import math
import types
from google.appengine.api import api_base_pb
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import capabilities
from google.appengine.api import namespace_manager
from google.appengine.api.memcache import memcache_service_pb
from google.appengine.runtime import apiproxy_errors
MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse
MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest
MemcacheGetResponse = memcache_service_pb.MemcacheGetResponse
MemcacheGetRequest = memcache_service_pb.MemcacheGetRequest
MemcacheDeleteResponse = memcache_service_pb.MemcacheDeleteResponse
MemcacheDeleteRequest = memcache_service_pb.MemcacheDeleteRequest
MemcacheIncrementResponse = memcache_service_pb.MemcacheIncrementResponse
MemcacheIncrementRequest = memcache_service_pb.MemcacheIncrementRequest
MemcacheBatchIncrementResponse = memcache_service_pb.MemcacheBatchIncrementResponse
MemcacheBatchIncrementRequest = memcache_service_pb.MemcacheBatchIncrementRequest
MemcacheFlushResponse = memcache_service_pb.MemcacheFlushResponse
MemcacheFlushRequest = memcache_service_pb.MemcacheFlushRequest
MemcacheStatsRequest = memcache_service_pb.MemcacheStatsRequest
MemcacheStatsResponse = memcache_service_pb.MemcacheStatsResponse
DELETE_NETWORK_FAILURE = 0
DELETE_ITEM_MISSING = 1
DELETE_SUCCESSFUL = 2
STORED = MemcacheSetResponse.STORED
NOT_STORED = MemcacheSetResponse.NOT_STORED
ERROR = MemcacheSetResponse.ERROR
EXISTS = MemcacheSetResponse.EXISTS
MAX_KEY_SIZE = 250
MAX_VALUE_SIZE = 10 ** 6
STAT_HITS = 'hits'
STAT_MISSES = 'misses'
STAT_BYTE_HITS = 'byte_hits'
STAT_ITEMS = 'items'
STAT_BYTES = 'bytes'
STAT_OLDEST_ITEM_AGES = 'oldest_item_age'
FLAG_TYPE_MASK = 7
FLAG_COMPRESSED = 1 << 3
TYPE_STR = 0
TYPE_UNICODE = 1
TYPE_PICKLED = 2
TYPE_INT = 3
TYPE_LONG = 4
TYPE_BOOL = 5
CAPABILITY = capabilities.CapabilitySet('memcache')
def _is_pair(obj):
"""Helper to test if something is a pair (2-tuple)."""
return isinstance(obj, tuple) and len(obj) == 2
def _add_name_space(message, namespace=None):
"""Populate the name_space field in a messagecol buffer.
Args:
message: A messagecol buffer supporting the set_name_space() operation.
namespace: The name of the namespace part. If None, use the
default namespace. The empty namespace (i.e. '') will clear
the name_space field.
"""
if namespace is None:
namespace = namespace_manager.get_namespace()
if not namespace:
message.clear_name_space()
else:
message.set_name_space(namespace)
def _key_string(key, key_prefix='', server_to_user_dict=None):
"""Utility function to handle different ways of requesting keys.
Args:
key: Either a string or tuple of (shard_number, string). In Google App
Engine the sharding is automatic so the shard number is ignored.
To memcache, the key is just bytes (no defined encoding).
key_prefix: Optional string prefix to prepend to key.
server_to_user_dict: Optional dictionary to populate with a mapping of
server-side key (which includes the key_prefix) to user-supplied key
(which does not have the prefix).
Returns:
The key as a non-unicode string prepended with key_prefix. This is
the key sent to and stored by the server. If the resulting key is
longer then MAX_KEY_SIZE, it will be hashed with sha1 and will be
replaced with the hex representation of the said hash.
Raises:
TypeError: If provided key isn't a string or tuple of (int, string)
or key_prefix.
"""
if _is_pair(key):
key = key[1]
if not isinstance(key, basestring):
raise TypeError('Key must be a string instance, received %r' % key)
if not isinstance(key_prefix, basestring):
raise TypeError('key_prefix must be a string instance, received %r' %
key_prefix)
server_key = key_prefix + key
if isinstance(server_key, unicode):
server_key = server_key.encode('utf-8')
if len(server_key) > MAX_KEY_SIZE:
server_key = hashlib.sha1(server_key).hexdigest()
if server_to_user_dict is not None:
assert isinstance(server_to_user_dict, dict)
server_to_user_dict[server_key] = key
return server_key
def _validate_encode_value(value, do_pickle):
"""Utility function to validate and encode server keys and values.
Args:
value: Value to store in memcache. If it's a string, it will get passed
along as-is. If it's a unicode string, it will be marked appropriately,
such that retrievals will yield a unicode value. If it's any other data
type, this function will attempt to pickle the data and then store the
serialized result, unpickling it upon retrieval.
do_pickle: Callable that takes an object and returns a non-unicode
string containing the pickled object.
Returns:
Tuple (stored_value, flags) where:
stored_value: The value as a non-unicode string that should be stored
in memcache.
flags: An integer with bits set from the FLAG_* constants in this file
to indicate the encoding of the key and value.
Raises:
ValueError: If the encoded value is too large.
pickle.PicklingError: If the value is not a string and could not be pickled.
RuntimeError: If a complicated data structure could not be pickled due to
too many levels of recursion in its composition.
"""
flags = 0
stored_value = value
if isinstance(value, str):
pass
elif isinstance(value, unicode):
stored_value = value.encode('utf-8')
flags |= TYPE_UNICODE
elif isinstance(value, bool):
stored_value = str(int(value))
flags |= TYPE_BOOL
elif isinstance(value, int):
stored_value = str(value)
flags |= TYPE_INT
elif isinstance(value, long):
stored_value = str(value)
flags |= TYPE_LONG
else:
stored_value = do_pickle(value)
flags |= TYPE_PICKLED
if len(stored_value) > MAX_VALUE_SIZE:
raise ValueError('Values may not be more than %d bytes in length; '
'received %d bytes' % (MAX_VALUE_SIZE, len(stored_value)))
return (stored_value, flags)
def _decode_value(stored_value, flags, do_unpickle):
"""Utility function for decoding values retrieved from memcache.
Args:
stored_value: The value as a non-unicode string that was stored.
flags: An integer with bits set from the FLAG_* constants in this file
that indicate the encoding of the key and value.
do_unpickle: Callable that takes a non-unicode string object that contains
a pickled object and returns the pickled object.
Returns:
The original object that was stored, be it a normal string, a unicode
string, int, long, or a Python object that was pickled.
Raises:
pickle.UnpicklingError: If the value could not be unpickled.
"""
assert isinstance(stored_value, str)
assert isinstance(flags, (int, long))
type_number = flags & FLAG_TYPE_MASK
value = stored_value
if type_number == TYPE_STR:
return value
elif type_number == TYPE_UNICODE:
return unicode(value, 'utf-8')
elif type_number == TYPE_PICKLED:
return do_unpickle(value)
elif type_number == TYPE_BOOL:
return bool(int(value))
elif type_number == TYPE_INT:
return int(value)
elif type_number == TYPE_LONG:
return long(value)
else:
assert False, "Unknown stored type"
assert False, "Shouldn't get here."
def create_rpc(deadline=None, callback=None):
"""Creates an RPC object for use with the memcache API.
Args:
deadline: Optional deadline in seconds for the operation; the default
is a system-specific deadline (typically 5 seconds).
callback: Optional callable to invoke on completion.
Returns:
An apiproxy_stub_map.UserRPC object specialized for this service.
"""
return apiproxy_stub_map.UserRPC('memcache', deadline, callback)
class Client(object):
"""Memcache client object, through which one invokes all memcache operations.
Several methods are no-ops to retain source-level compatibility
with the existing popular Python memcache library.
Any method that takes a 'key' argument will accept that key as a string
(unicode or not) or a tuple of (hash_value, string) where the hash_value,
normally used for sharding onto a memcache instance, is instead ignored, as
Google App Engine deals with the sharding transparently. Keys in memcache are
just bytes, without a specified encoding. All such methods may raise TypeError
if provided a bogus key value and a ValueError if the key is too large.
Any method that takes a 'value' argument will accept as that value any
string (unicode or not), int, long, or pickle-able Python object, including
all native types. You'll get back from the cache the same type that you
originally put in.
The Client class is not thread-safe with respect to the gets(), cas() and
cas_multi() methods (and other compare-and-set-related methods). Therefore,
Client objects should not be used by more than one thread for CAS purposes.
Note that the global Client for the module-level functions is okay because it
does not expose any of the CAS methods.
"""
def __init__(self, servers=None, debug=0,
pickleProtocol=cPickle.HIGHEST_PROTOCOL,
pickler=cPickle.Pickler,
unpickler=cPickle.Unpickler,
pload=None,
pid=None,
make_sync_call=None,
_app_id=None,
_num_memcacheg_backends=None,
_ignore_shardlock=None,
_memcache_pool_hint=None):
"""Create a new Client object.
No parameters are required.
Arguments:
servers: Ignored; only for compatibility.
debug: Ignored; only for compatibility.
pickleProtocol: Pickle protocol to use for pickling the object.
pickler: pickle.Pickler sub-class to use for pickling.
unpickler: pickle.Unpickler sub-class to use for unpickling.
pload: Callable to use for retrieving objects by persistent id.
pid: Callable to use for determine the persistent id for objects, if any.
make_sync_call: Ignored; only for compatibility with an earlier version.
"""
self._pickler_factory = pickler
self._unpickler_factory = unpickler
self._pickle_protocol = pickleProtocol
self._persistent_id = pid
self._persistent_load = pload
self._app_id = _app_id
self._num_memcacheg_backends = _num_memcacheg_backends
self._ignore_shardlock = _ignore_shardlock
self._memcache_pool_hint = _memcache_pool_hint
self._cas_ids = {}
if _app_id and not(_num_memcacheg_backends and _memcache_pool_hint):
raise ValueError('If you specify an _app_id, you must also provide '
'_num_memcacheg_backends and _memcache_pool_hint')
def cas_reset(self):
"""Clear the remembered CAS ids."""
self._cas_ids.clear()
def _make_async_call(self, rpc, method, request, response,
get_result_hook, user_data):
"""Internal helper to schedule an asynchronous RPC.
Args:
rpc: None or a UserRPC object.
method: Method name, e.g. 'Get'.
request: Request protobuf.
response: Response protobuf.
get_result_hook: None or hook function used to process results
(See UserRPC.make_call() for more info).
user_data: None or user data for hook function.
Returns:
A UserRPC object; either the one passed in as the first argument,
or a new one (if the first argument was None).
"""
if rpc is None:
rpc = create_rpc()
assert rpc.service == 'memcache', repr(rpc.service)
rpc.make_call(method, request, response, get_result_hook, user_data)
return rpc
def _do_pickle(self, value):
"""Pickles a provided value."""
pickle_data = cStringIO.StringIO()
pickler = self._pickler_factory(pickle_data,
protocol=self._pickle_protocol)
if self._persistent_id is not None:
pickler.persistent_id = self._persistent_id
pickler.dump(value)
return pickle_data.getvalue()
def _do_unpickle(self, value):
"""Unpickles a provided value."""
pickle_data = cStringIO.StringIO(value)
unpickler = self._unpickler_factory(pickle_data)
if self._persistent_load is not None:
unpickler.persistent_load = self._persistent_load
return unpickler.load()
def _add_app_id(self, message):
"""Populates override field in message if accessing another app's memcache.
Args:
message: A protocol buffer supporting the mutable_override() operation.
"""
if self._app_id:
app_override = message.mutable_override()
app_override.set_app_id(self._app_id)
app_override.set_num_memcacheg_backends(self._num_memcacheg_backends)
if self._ignore_shardlock:
app_override.set_ignore_shardlock(self._ignore_shardlock)
app_override.set_memcache_pool_hint(self._memcache_pool_hint)
def set_servers(self, servers):
"""Sets the pool of memcache servers used by the client.
This is purely a compatibility method. In Google App Engine, it's a no-op.
"""
pass
def disconnect_all(self):
"""Closes all connections to memcache servers.
This is purely a compatibility method. In Google App Engine, it's a no-op.
"""
pass
def forget_dead_hosts(self):
"""Resets all servers to the alive status.
This is purely a compatibility method. In Google App Engine, it's a no-op.
"""
pass
def debuglog(self):
"""Logging function for debugging information.
This is purely a compatibility method. In Google App Engine, it's a no-op.
"""
pass
def get_stats(self):
"""Gets memcache statistics for this application.
All of these statistics may reset due to various transient conditions. They
provide the best information available at the time of being called.
Returns:
Dictionary mapping statistic names to associated values. Statistics and
their associated meanings:
hits: Number of cache get requests resulting in a cache hit.
misses: Number of cache get requests resulting in a cache miss.
byte_hits: Sum of bytes transferred on get requests. Rolls over to
zero on overflow.
items: Number of key/value pairs in the cache.
bytes: Total size of all items in the cache.
oldest_item_age: How long in seconds since the oldest item in the
cache was accessed. Effectively, this indicates how long a new
item will survive in the cache without being accessed. This is
_not_ the amount of time that has elapsed since the item was
created.
On error, returns None.
"""
rpc = self.get_stats_async()
return rpc.get_result()
def get_stats_async(self, rpc=None):
"""Async version of get_stats().
Returns:
A UserRPC instance whose get_result() method returns None if
there was a network error, otherwise a dict just like
get_stats() returns.
"""
request = MemcacheStatsRequest()
self._add_app_id(request)
response = MemcacheStatsResponse()
return self._make_async_call(rpc, 'Stats', request, response,
self.__get_stats_hook, None)
def __get_stats_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return None
response = rpc.response
if not response.has_stats():
return {
STAT_HITS: 0,
STAT_MISSES: 0,
STAT_BYTE_HITS: 0,
STAT_ITEMS: 0,
STAT_BYTES: 0,
STAT_OLDEST_ITEM_AGES: 0,
}
stats = response.stats()
return {
STAT_HITS: stats.hits(),
STAT_MISSES: stats.misses(),
STAT_BYTE_HITS: stats.byte_hits(),
STAT_ITEMS: stats.items(),
STAT_BYTES: stats.bytes(),
STAT_OLDEST_ITEM_AGES: stats.oldest_item_age(),
}
def flush_all(self):
"""Deletes everything in memcache.
Returns:
True on success, False on RPC or server error.
"""
rpc = self.flush_all_async()
return rpc.get_result()
def flush_all_async(self, rpc=None):
"""Async version of flush_all().
Returns:
A UserRPC instance whose get_result() method returns True on
success, False on RPC or server error.
"""
request = MemcacheFlushRequest()
self._add_app_id(request)
response = MemcacheFlushResponse()
return self._make_async_call(rpc, 'FlushAll', request, response,
self.__flush_all_hook, None)
def __flush_all_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return False
return True
def get(self, key, namespace=None, for_cas=False):
"""Looks up a single key in memcache.
If you have multiple items to load, though, it's much more efficient
to use get_multi() instead, which loads them in one bulk operation,
reducing the networking latency that'd otherwise be required to do
many serialized get() operations.
Args:
key: The key in memcache to look up. See docs on Client
for details of format.
namespace: a string specifying an optional namespace to use in
the request.
for_cas: If True, request and store CAS ids on the client (see
cas() operation below).
Returns:
The value of the key, if found in memcache, else None.
"""
if _is_pair(key):
key = key[1]
rpc = self.get_multi_async([key], namespace=namespace, for_cas=for_cas)
results = rpc.get_result()
return results.get(key)
def gets(self, key, namespace=None):
"""An alias for get(..., for_cas=True)."""
return self.get(key, namespace=namespace, for_cas=True)
def get_multi(self, keys, key_prefix='', namespace=None, for_cas=False):
"""Looks up multiple keys from memcache in one operation.
This is the recommended way to do bulk loads.
Args:
keys: List of keys to look up. Keys may be strings or
tuples of (hash_value, string). Google App Engine
does the sharding and hashing automatically, though, so the hash
value is ignored. To memcache, keys are just series of bytes,
and not in any particular encoding.
key_prefix: Prefix to prepend to all keys when talking to the server;
not included in the returned dictionary.
namespace: a string specifying an optional namespace to use in
the request.
for_cas: If True, request and store CAS ids on the client.
Returns:
A dictionary of the keys and values that were present in memcache.
Even if the key_prefix was specified, that key_prefix won't be on
the keys in the returned dictionary.
"""
rpc = self.get_multi_async(keys, key_prefix, namespace, for_cas)
return rpc.get_result()
def get_multi_async(self, keys, key_prefix='', namespace=None,
for_cas=False, rpc=None):
"""Async version of get_multi().
Returns:
A UserRPC instance whose get_result() method returns {} if
there was a network error, otherwise a dict just like
get_multi() returns.
"""
request = MemcacheGetRequest()
self._add_app_id(request)
_add_name_space(request, namespace)
if for_cas:
request.set_for_cas(True)
response = MemcacheGetResponse()
user_key = {}
for key in keys:
request.add_key(_key_string(key, key_prefix, user_key))
return self._make_async_call(rpc, 'Get', request, response,
self.__get_hook, user_key)
def __get_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return {}
for_cas = rpc.request.for_cas()
response = rpc.response
user_key = rpc.user_data
return_value = {}
for returned_item in response.item_list():
value = _decode_value(returned_item.value(), returned_item.flags(),
self._do_unpickle)
raw_key = returned_item.key()
if for_cas:
self._cas_ids[raw_key] = returned_item.cas_id()
return_value[user_key[raw_key]] = value
return return_value
def delete(self, key, seconds=0, namespace=None):
"""Deletes a key from memcache.
Args:
key: Key to delete. See docs on Client for detils.
seconds: Optional number of seconds to make deleted items 'locked'
for 'add' operations. Value can be a delta from current time (up to
1 month), or an absolute Unix epoch time. Defaults to 0, which means
items can be immediately added. With or without this option,
a 'set' operation will always work. Float values will be rounded up to
the nearest whole second.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
DELETE_NETWORK_FAILURE (0) on network failure,
DELETE_ITEM_MISSING (1) if the server tried to delete the item but
didn't have it, or
DELETE_SUCCESSFUL (2) if the item was actually deleted.
This can be used as a boolean value, where a network failure is the
only bad condition.
"""
rpc = self.delete_multi_async([key], seconds, namespace=namespace)
results = rpc.get_result()
if not results:
return DELETE_NETWORK_FAILURE
return results[0]
def delete_multi(self, keys, seconds=0, key_prefix='', namespace=None):
"""Delete multiple keys at once.
Args:
keys: List of keys to delete.
seconds: Optional number of seconds to make deleted items 'locked'
for 'add' operations. Value can be a delta from current time (up to
1 month), or an absolute Unix epoch time. Defaults to 0, which means
items can be immediately added. With or without this option,
a 'set' operation will always work. Float values will be rounded up to
the nearest whole second.
key_prefix: Prefix to put on all keys when sending specified
keys to memcache. See docs for get_multi() and set_multi().
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if all operations completed successfully. False if one
or more failed to complete.
"""
rpc = self.delete_multi_async(keys, seconds, key_prefix, namespace)
results = rpc.get_result()
return bool(results)
def delete_multi_async(self, keys, seconds=0, key_prefix='',
namespace=None, rpc=None):
"""Async version of delete_multi() -- note different return value.
Returns:
A UserRPC instance whose get_result() method returns None if
there was a network error, or a list of status values otherwise,
where each status corresponds to a key and is either
DELETE_SUCCESSFUL, DELETE_ITEM_MISSING, or DELETE_NETWORK_FAILURE
(see delete() docstring for details).
"""
if not isinstance(seconds, (int, long, float)):
raise TypeError('Delete timeout must be a number.')
if seconds < 0:
raise ValueError('Delete timeout must not be negative.')
request = MemcacheDeleteRequest()
self._add_app_id(request)
_add_name_space(request, namespace)
response = MemcacheDeleteResponse()
for key in keys:
delete_item = request.add_item()
delete_item.set_key(_key_string(key, key_prefix=key_prefix))
delete_item.set_delete_time(int(math.ceil(seconds)))
return self._make_async_call(rpc, 'Delete', request, response,
self.__delete_hook, None)
def __delete_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return None
result = []
for status in rpc.response.delete_status_list():
if status == MemcacheDeleteResponse.DELETED:
result.append(DELETE_SUCCESSFUL)
elif status == MemcacheDeleteResponse.NOT_FOUND:
result.append(DELETE_ITEM_MISSING)
else:
result.append(DELETE_NETWORK_FAILURE)
return result
def set(self, key, value, time=0, min_compress_len=0, namespace=None):
"""Sets a key's value, regardless of previous contents in cache.
Unlike add() and replace(), this method always sets (or
overwrites) the value in memcache, regardless of previous
contents.
Args:
key: Key to set. See docs on Client for details.
value: Value to set. Any type. If complex, will be pickled.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
min_compress_len: Ignored option for compatibility.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if set. False on error.
"""
return self._set_with_policy(MemcacheSetRequest.SET, key, value, time=time,
namespace=namespace)
def add(self, key, value, time=0, min_compress_len=0, namespace=None):
"""Sets a key's value, iff item is not already in memcache.
Args:
key: Key to set. See docs on Client for details.
value: Value to set. Any type. If complex, will be pickled.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
min_compress_len: Ignored option for compatibility.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if added. False on error.
"""
return self._set_with_policy(MemcacheSetRequest.ADD, key, value, time=time,
namespace=namespace)
def replace(self, key, value, time=0, min_compress_len=0, namespace=None):
"""Replaces a key's value, failing if item isn't already in memcache.
Args:
key: Key to set. See docs on Client for details.
value: Value to set. Any type. If complex, will be pickled.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
min_compress_len: Ignored option for compatibility.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if replaced. False on RPC error or cache miss.
"""
return self._set_with_policy(MemcacheSetRequest.REPLACE,
key, value, time=time, namespace=namespace)
def cas(self, key, value, time=0, min_compress_len=0, namespace=None):
"""Compare-And-Set update.
This requires that the key has previously been successfully
fetched with gets() or get(..., for_cas=True), and that no changes
have been made to the key since that fetch. Typical usage is:
key = ...
client = memcache.Client()
value = client.gets(key) # OR client.get(key, for_cas=True)
<updated value>
ok = client.cas(key, value)
If two processes run similar code, the first one calling cas()
will succeed (ok == True), while the second one will fail (ok ==
False). This can be used to detect race conditions.
NOTE: some state (the CAS id) is stored on the Client object for
each key ever used with gets(). To prevent ever-increasing memory
usage, you must use a Client object when using cas(), and the
lifetime of your Client object should be limited to that of one
incoming HTTP request. You cannot use the global-function-based
API.
Args:
key: Key to set. See docs on Client for details.
value: The new value.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
min_compress_len: Ignored option for compatibility.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if updated. False on RPC error or if the CAS id didn't match.
"""
return self._set_with_policy(MemcacheSetRequest.CAS, key, value,
time, namespace)
def _set_with_policy(self, policy, key, value, time=0, namespace=None):
"""Sets a single key with a specified policy.
Helper function for set(), add(), and replace().
Args:
policy: One of MemcacheSetRequest.SET, .ADD, .REPLACE or .CAS.
key: Key to add, set, or replace. See docs on Client for details.
value: Value to set.
time: Expiration time, defaulting to 0 (never expiring).
namespace: a string specifying an optional namespace to use in
the request.
Returns:
True if stored, False on RPC error or policy error, e.g. a replace
that failed due to the item not already existing, or an add
failing due to the item not already existing.
"""
rpc = self._set_multi_async_with_policy(policy, {key: value},
time, '', namespace)
status_dict = rpc.get_result()
if not status_dict:
return False
return status_dict.get(key) == MemcacheSetResponse.STORED
def _set_multi_with_policy(self, policy, mapping, time=0, key_prefix='',
namespace=None):
"""Set multiple keys with a specified policy.
Helper function for set_multi(), add_multi(), and replace_multi(). This
reduces the network latency of doing many requests in serial.
Args:
policy: One of MemcacheSetRequest.SET, .ADD, .REPLACE or .CAS.
mapping: Dictionary of keys to values. If policy == CAS, the
values must be (value, cas_id) tuples.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
key_prefix: Prefix for to prepend to all keys.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
A list of keys whose values were NOT set. On total success,
this list should be empty. On network/RPC/server errors,
a list of all input keys is returned; in this case the keys
may or may not have been updated.
"""
rpc = self._set_multi_async_with_policy(policy, mapping, time,
key_prefix, namespace)
status_dict = rpc.get_result()
server_keys, user_key = rpc.user_data
if not status_dict:
return user_key.values()
unset_list = []
for server_key in server_keys:
key = user_key[server_key]
set_status = status_dict[key]
if set_status != MemcacheSetResponse.STORED:
unset_list.append(key)
return unset_list
def _set_multi_async_with_policy(self, policy, mapping, time=0,
key_prefix='', namespace=None, rpc=None):
"""Async version of _set_multi_with_policy() -- note different return.
Returns:
A UserRPC instance whose get_result() method returns None if
there was a network error, or a dict mapping (user) keys to
status values otherwise, where each status is one of STORED,
NOT_STORED, ERROR, or EXISTS.
"""
if not isinstance(time, (int, long, float)):
raise TypeError('Expiration must be a number.')
if time < 0.0:
raise ValueError('Expiration must not be negative.')
request = MemcacheSetRequest()
self._add_app_id(request)
_add_name_space(request, namespace)
user_key = {}
server_keys = []
set_cas_id = (policy == MemcacheSetRequest.CAS)
for key, value in mapping.iteritems():
server_key = _key_string(key, key_prefix, user_key)
stored_value, flags = _validate_encode_value(value, self._do_pickle)
server_keys.append(server_key)
item = request.add_item()
item.set_key(server_key)
item.set_value(stored_value)
item.set_flags(flags)
item.set_set_policy(policy)
item.set_expiration_time(int(math.ceil(time)))
if set_cas_id:
cas_id = self._cas_ids.get(server_key)
if cas_id is not None:
item.set_cas_id(cas_id)
item.set_for_cas(True)
response = MemcacheSetResponse()
return self._make_async_call(rpc, 'Set', request, response,
self.__set_with_policy_hook,
(server_keys, user_key))
def __set_with_policy_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return None
response = rpc.response
server_keys, user_key = rpc.user_data
assert response.set_status_size() == len(server_keys)
status_dict = {}
for server_key, status in zip(server_keys, response.set_status_list()):
status_dict[user_key[server_key]] = status
return status_dict
def set_multi(self, mapping, time=0, key_prefix='', min_compress_len=0,
namespace=None):
"""Set multiple keys' values at once, regardless of previous contents.
Args:
mapping: Dictionary of keys to values.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
key_prefix: Prefix for to prepend to all keys.
min_compress_len: Unimplemented compatibility option.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
A list of keys whose values were NOT set. On total success,
this list should be empty.
"""
return self._set_multi_with_policy(MemcacheSetRequest.SET, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace)
def set_multi_async(self, mapping, time=0, key_prefix='',
min_compress_len=0, namespace=None, rpc=None):
"""Async version of set_multi() -- note different return value.
Returns:
See _set_multi_async_with_policy().
"""
return self._set_multi_async_with_policy(MemcacheSetRequest.SET, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace, rpc=rpc)
def add_multi(self, mapping, time=0, key_prefix='', min_compress_len=0,
namespace=None):
"""Set multiple keys' values iff items are not already in memcache.
Args:
mapping: Dictionary of keys to values.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
key_prefix: Prefix for to prepend to all keys.
min_compress_len: Unimplemented compatibility option.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
A list of keys whose values were NOT set because they did not already
exist in memcache. On total success, this list should be empty.
"""
return self._set_multi_with_policy(MemcacheSetRequest.ADD, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace)
def add_multi_async(self, mapping, time=0, key_prefix='',
min_compress_len=0, namespace=None, rpc=None):
"""Async version of add_multi() -- note different return value.
Returns:
See _set_multi_async_with_policy().
"""
return self._set_multi_async_with_policy(MemcacheSetRequest.ADD, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace, rpc=rpc)
def replace_multi(self, mapping, time=0, key_prefix='', min_compress_len=0,
namespace=None):
"""Replace multiple keys' values, failing if the items aren't in memcache.
Args:
mapping: Dictionary of keys to values.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
key_prefix: Prefix for to prepend to all keys.
min_compress_len: Unimplemented compatibility option.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
A list of keys whose values were NOT set because they already existed
in memcache. On total success, this list should be empty.
"""
return self._set_multi_with_policy(MemcacheSetRequest.REPLACE, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace)
def replace_multi_async(self, mapping, time=0, key_prefix='',
min_compress_len=0, namespace=None, rpc=None):
"""Async version of replace_multi() -- note different return value.
Returns:
See _set_multi_async_with_policy().
"""
return self._set_multi_async_with_policy(MemcacheSetRequest.REPLACE,
mapping,
time=time, key_prefix=key_prefix,
namespace=namespace, rpc=rpc)
def cas_multi(self, mapping, time=0, key_prefix='', min_compress_len=0,
namespace=None):
"""Compare-And-Set update for multiple keys.
See cas() docstring for an explanation.
Args:
mapping: Dictionary of keys to values.
time: Optional expiration time, either relative number of seconds
from current time (up to 1 month), or an absolute Unix epoch time.
By default, items never expire, though items may be evicted due to
memory pressure. Float values will be rounded up to the nearest
whole second.
key_prefix: Prefix for to prepend to all keys.
min_compress_len: Unimplemented compatibility option.
namespace: a string specifying an optional namespace to use in
the request.
Returns:
A list of keys whose values were NOT set because the compare
failed. On total success, this list should be empty.
"""
return self._set_multi_with_policy(MemcacheSetRequest.CAS, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace)
def cas_multi_async(self, mapping, time=0, key_prefix='',
min_compress_len=0, namespace=None, rpc=None):
"""Async version of cas_multi() -- note different return value.
Returns:
See _set_multi_async_with_policy().
"""
return self._set_multi_async_with_policy(MemcacheSetRequest.CAS, mapping,
time=time, key_prefix=key_prefix,
namespace=namespace, rpc=rpc)
def incr(self, key, delta=1, namespace=None, initial_value=None):
"""Atomically increments a key's value.
Internally, the value is a unsigned 64-bit integer. Memcache
doesn't check 64-bit overflows. The value, if too large, will
wrap around.
Unless an initial_value is specified, the key must already exist
in the cache to be incremented. To initialize a counter, either
specify initial_value or set() it to the initial value, as an
ASCII decimal integer. Future get()s of the key, post-increment,
will still be an ASCII decimal value.
Args:
key: Key to increment. If an iterable collection, each one of the keys
will be offset. See Client's docstring for details.
delta: Non-negative integer value (int or long) to increment key by,
defaulting to 1.
namespace: a string specifying an optional namespace to use in
the request.
initial_value: initial value to put in the cache, if it doesn't
already exist. The default value, None, will not create a cache
entry if it doesn't already exist.
Returns:
If key was a single value, the new long integer value, or None if key
was not in the cache, could not be incremented for any other reason, or
a network/RPC/server error occurred.
If key was an iterable collection, a dictionary will be returned
mapping supplied keys to values, with the values having the same meaning
as the singular return value of this method.
Raises:
ValueError: If number is negative.
TypeError: If delta isn't an int or long.
"""
return self._incrdecr(key, False, delta, namespace=namespace,
initial_value=initial_value)
def incr_async(self, key, delta=1, namespace=None, initial_value=None,
rpc=None):
"""Async version of incr().
Returns:
A UserRPC instance whose get_result() method returns the same
kind of value as incr() returns.
"""
return self._incrdecr_async(key, False, delta, namespace=namespace,
initial_value=initial_value, rpc=rpc)
def decr(self, key, delta=1, namespace=None, initial_value=None):
"""Atomically decrements a key's value.
Internally, the value is a unsigned 64-bit integer. Memcache
caps decrementing below zero to zero.
The key must already exist in the cache to be decremented. See
docs on incr() for details.
Args:
key: Key to decrement. If an iterable collection, each one of the keys
will be offset. See Client's docstring for details.
delta: Non-negative integer value (int or long) to decrement key by,
defaulting to 1.
namespace: a string specifying an optional namespace to use in
the request.
initial_value: initial value to put in the cache, if it doesn't
already exist. The default value, None, will not create a cache
entry if it doesn't already exist.
Returns:
If key was a single value, the new long integer value, or None if key
was not in the cache, could not be decremented for any other reason, or
a network/RPC/server error occurred.
If key was an iterable collection, a dictionary will be returned
mapping supplied keys to values, with the values having the same meaning
as the singular return value of this method.
Raises:
ValueError: If number is negative.
TypeError: If delta isn't an int or long.
"""
return self._incrdecr(key, True, delta, namespace=namespace,
initial_value=initial_value)
def decr_async(self, key, delta=1, namespace=None, initial_value=None,
rpc=None):
"""Async version of decr().
Returns:
A UserRPC instance whose get_result() method returns the same
kind of value as decr() returns.
"""
return self._incrdecr_async(key, True, delta, namespace=namespace,
initial_value=initial_value, rpc=rpc)
def _incrdecr(self, key, is_negative, delta, namespace=None,
initial_value=None):
"""Increment or decrement a key by a provided delta.
Args:
key: Key to increment or decrement. If an iterable collection, each
one of the keys will be offset.
is_negative: Boolean, if this is a decrement.
delta: Non-negative integer amount (int or long) to increment
or decrement by.
namespace: a string specifying an optional namespace to use in
the request.
initial_value: initial value to put in the cache, if it doesn't
already exist. The default value, None, will not create a cache
entry if it doesn't already exist.
Returns:
New long integer value, or None on cache miss or network/RPC/server
error.
Raises:
ValueError: If delta is negative.
TypeError: If delta isn't an int or long.
"""
rpc = self._incrdecr_async(key, is_negative, delta, namespace,
initial_value)
return rpc.get_result()
def _incrdecr_async(self, key, is_negative, delta, namespace=None,
initial_value=None, rpc=None):
"""Async version of _incrdecr().
Returns:
A UserRPC instance whose get_result() method returns the same
kind of value as _incrdecr() returns.
"""
if not isinstance(delta, (int, long)):
raise TypeError('Delta must be an integer or long, received %r' % delta)
if delta < 0:
raise ValueError('Delta must not be negative.')
if not isinstance(key, basestring):
try:
it = iter(key)
except TypeError:
pass
else:
if is_negative:
delta = -delta
return self.offset_multi_async(dict((k, delta) for k in it),
namespace=namespace,
initial_value=initial_value,
rpc=rpc)
request = MemcacheIncrementRequest()
self._add_app_id(request)
_add_name_space(request, namespace)
response = MemcacheIncrementResponse()
request.set_key(_key_string(key))
request.set_delta(delta)
if is_negative:
request.set_direction(MemcacheIncrementRequest.DECREMENT)
else:
request.set_direction(MemcacheIncrementRequest.INCREMENT)
if initial_value is not None:
request.set_initial_value(long(initial_value))
initial_flags = None
if isinstance(initial_value, int):
initial_flags = TYPE_INT
elif isinstance(initial_value, long):
initial_flags = TYPE_LONG
if initial_flags is not None:
request.set_initial_flags(initial_flags)
return self._make_async_call(rpc, 'Increment', request, response,
self.__incrdecr_hook, None)
def __incrdecr_hook(self, rpc):
try:
rpc.check_success()
except apiproxy_errors.Error:
return None
response = rpc.response
if response.has_new_value():
return response.new_value()
return None
def offset_multi(self, mapping, key_prefix='',
namespace=None, initial_value=None):
"""Offsets multiple keys by a delta, incrementing and decrementing in batch.
Args:
mapping: Dictionary mapping keys to deltas (positive or negative integers)
to apply to each corresponding key.
key_prefix: Prefix for to prepend to all keys.
initial_value: Initial value to put in the cache, if it doesn't
already exist. The default value, None, will not create a cache
entry if it doesn't already exist.
namespace: A string specifying an optional namespace to use in
the request.
Returns:
Dictionary mapping input keys to new integer values. The new value will
be None if an error occurs, the key does not already exist, or the value
was not an integer type. The values will wrap-around at unsigned 64-bit
integer-maximum and underflow will be floored at zero.
"""
rpc = self.offset_multi_async(mapping, key_prefix,
namespace, initial_value)
return rpc.get_result()
def offset_multi_async(self, mapping, key_prefix='',
namespace=None, initial_value=None, rpc=None):
"""Async version of offset_multi().
Returns:
A UserRPC instance whose get_result() method returns a dict just
like offset_multi() returns.
"""
initial_flags = None
if initial_value is not None:
if not isinstance(initial_value, (int, long)):
raise TypeError('initial_value must be an integer')
if initial_value < 0:
raise ValueError('initial_value must be >= 0')
if isinstance(initial_value, int):
initial_flags = TYPE_INT
else:
initial_flags = TYPE_LONG
request = MemcacheBatchIncrementRequest()
self._add_app_id(request)
response = MemcacheBatchIncrementResponse()
_add_name_space(request, namespace)
for key, delta in mapping.iteritems():
if not isinstance(delta, (int, long)):
raise TypeError('Delta must be an integer or long, received %r' % delta)
if delta >= 0:
direction = MemcacheIncrementRequest.INCREMENT
else:
delta = -delta
direction = MemcacheIncrementRequest.DECREMENT
server_key = _key_string(key, key_prefix)
item = request.add_item()
item.set_key(server_key)
item.set_delta(delta)
item.set_direction(direction)
if initial_value is not None:
item.set_initial_value(initial_value)
item.set_initial_flags(initial_flags)
return self._make_async_call(rpc, 'BatchIncrement', request, response,
self.__offset_hook, mapping)
def __offset_hook(self, rpc):
mapping = rpc.user_data
try:
rpc.check_success()
except apiproxy_errors.Error:
return dict((k, None) for k in mapping.iterkeys())
response = rpc.response
assert response.item_size() == len(mapping)
result_dict = {}
for key, resp_item in zip(mapping.iterkeys(), response.item_list()):
if (resp_item.increment_status() == MemcacheIncrementResponse.OK and
resp_item.has_new_value()):
result_dict[key] = resp_item.new_value()
else:
result_dict[key] = None
return result_dict
_CLIENT = None
def setup_client(client_obj):
"""Sets the Client object instance to use for all module-level methods.
Use this method if you want to have customer persistent_id() or
persistent_load() functions associated with your client.
NOTE: We don't expose the _async methods as functions; they're too
obscure; and we don't expose gets(), cas() and cas_multi() because
they maintain state on the client object.
Args:
client_obj: Instance of the memcache.Client object.
"""
global _CLIENT
var_dict = globals()
_CLIENT = client_obj
var_dict['set_servers'] = _CLIENT.set_servers
var_dict['disconnect_all'] = _CLIENT.disconnect_all
var_dict['forget_dead_hosts'] = _CLIENT.forget_dead_hosts
var_dict['debuglog'] = _CLIENT.debuglog
var_dict['get'] = _CLIENT.get
var_dict['get_multi'] = _CLIENT.get_multi
var_dict['set'] = _CLIENT.set
var_dict['set_multi'] = _CLIENT.set_multi
var_dict['add'] = _CLIENT.add
var_dict['add_multi'] = _CLIENT.add_multi
var_dict['replace'] = _CLIENT.replace
var_dict['replace_multi'] = _CLIENT.replace_multi
var_dict['delete'] = _CLIENT.delete
var_dict['delete_multi'] = _CLIENT.delete_multi
var_dict['incr'] = _CLIENT.incr
var_dict['decr'] = _CLIENT.decr
var_dict['flush_all'] = _CLIENT.flush_all
var_dict['get_stats'] = _CLIENT.get_stats
var_dict['offset_multi'] = _CLIENT.offset_multi
setup_client(Client())
Show details
Hide details
Change log
r262
by mar...@google.com on May 22 (5 days ago)
Diff
[No log message]
Go to:
/trunk/python/README
/trunk/python/RELEASE_NOTES
/trunk/python/VERSION
/trunk/python/api_server.py
/trunk/python/appcfg.py
/trunk/python/bulkload_client.py
/trunk/python/bulkloader.py
/trunk/python/dev_appserver.py
/trunk/python/download_appstats.py
/trunk/python/gen_protorpc.py
...e/appengine/api/apiproxy_stub.py
...pengine/api/apiproxy_stub_map.py
.../google/appengine/api/appinfo.py
...gle/appengine/api/backendinfo.py
.../capabilities/capability_stub.py
...appengine/api/channel/channel.py
...pi/channel/channel_service_pb.py
.../channel/channel_service_stub.py
...hon/google/appengine/api/conf.py
...google/appengine/api/croninfo.py
...oogle/appengine/api/datastore.py
...appengine/api/datastore_types.py
.../google/appengine/api/dosinfo.py
...ogle/appengine/api/files/file.py
...e/api/files/file_service_stub.py
...ine/api/logservice/logservice.py
...pengine/api/memcache/__init__.py
...ne/api/memcache/memcache_stub.py
...appengine/api/oauth/oauth_api.py
...e/appengine/api/pagespeedinfo.py
...oogle/appengine/api/queueinfo.py
...appengine/api/search/__init__.py
...e/appengine/api/search/search.py
.../api/search/search_service_pb.py
...ngine/api/taskqueue/taskqueue.py
...e/appengine/api/urlfetch_stub.py
...pengine/api/user_service_stub.py
...gine/api/xmpp/xmpp_service_pb.py
...ne/api/xmpp/xmpp_service_stub.py
...ne/cron/groctimespecification.py
...le/appengine/datastore/acl_pb.py
...ine/datastore/datastore_index.py
...tastore/datastore_sqlite_stub.py
...datastore/datastore_stub_util.py
...pengine/datastore/document_pb.py
...appengine/datastore/entity_pb.py
.../appengine/ext/admin/__init__.py
...min/datastore_stats_generator.py
...templates/datastore_indexes.html
...n/templates/datastore_stats.html
Older revisions
r245
by mar...@google.com on Mar 28, 2012
Diff
[No log message]
r233
by mar...@google.com on Jan 31, 2012
Diff
[No log message]
r224
by mar...@google.com on Dec 13, 2011
Diff
[No log message]
All revisions of this file
File info
Size: 52907 bytes, 1477 lines
View raw file
File properties
svn:executable
*
Powered by
Google Project Hosting