def cache_retrieve(key):
result = memcache.get_multi(['%s.%s' % (key, i) for i in xrange(32)])
serialized = ''.join([v for k, v in sorted(result.items()) if v is not None])
if serialized=='':
return None
return pickle.loads(serialized)
python类get_multi()的实例源码
def get(self):
# [START batch]
values = {'comment': 'I did not ... ', 'comment_by': 'Bill Holiday'}
if not memcache.set_multi(values):
logging.error('Unable to set Memcache values')
tvalues = memcache.get_multi(('comment', 'comment_by'))
self.response.write(tvalues)
# [END batch]
def fetch_keys(self, keys):
cache_key_mapping = dict((self.key_to_cache_key(key), key) for key in keys)
objects = memcache.get_multi(cache_key_mapping.keys())
object_map = dict((cache_key_mapping[k], v) for (k, v) in objects.iteritems())
# DEBUG!
#get_size = len(pickle.dumps(objects))
#logging.info("BatchLookup: memcache get_multi return size: %s", get_size)
logging.info("BatchLookup: memcache get_multi objects found: %s", objects.keys())
return object_map
def get_multi(self, cls, object_ids, allow_cache=True, allow_fail=False):
self.request_multi(cls, object_ids, allow_cache=allow_cache)
self.batch_fetch()
return self.fetched_data_multi(cls, object_ids, allow_fail=allow_fail)
def lookup_debug_tokens(access_tokens):
# We use a prod config here, so we can lookup access tokens from prod apps
app_fbl = FBLookup(None, facebook._PROD_FACEBOOK_CONFIG['app_access_token'])
app_fbl.make_passthrough()
result = app_fbl.get_multi(_LookupDebugToken, access_tokens)
if result and not result[0]['empty']:
return result
else:
app_fbl = FBLookup(None, facebook.FACEBOOK_CONFIG['app_access_token'])
app_fbl.make_passthrough()
return app_fbl.get_multi(_LookupDebugToken, access_tokens)
def load_summary_protos(java_application=False):
"""Load all valid summary records from memcache.
Args:
java_application: Boolean. If true, this function is being invoked
by the download_appstats tool on a java application.
Returns:
A list of StatsProto instances, in reverse chronological order
(i.e. most recent first).
NOTE: This is limited to returning at most config.KEY_MODULUS records,
since there are only that many distinct keys. See also make_key().
"""
tmpl = config.KEY_PREFIX + config.KEY_TEMPLATE + config.PART_SUFFIX
if java_application:
tmpl = '"' + tmpl + '"'
keys = [tmpl % i
for i in
range(0, config.KEY_DISTANCE * config.KEY_MODULUS,
config.KEY_DISTANCE)]
results = memcache.get_multi(keys, namespace=config.KEY_NAMESPACE)
records = []
for rec in results.itervalues():
try:
pb = StatsProto(rec)
except Exception, err:
logging.warn('Bad record: %s', err)
else:
records.append(pb)
logging.info('Loaded %d raw summary records, %d valid',
len(results), len(records))
records.sort(key=lambda pb: -pb.start_timestamp_milliseconds())
return records