Python google.appengine.api.memcache.get_multi() Examples

The following are code examples for showing how to use google.appengine.api.memcache.get_multi(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: ithz   Author: ergoithz   File: data.py    MIT License 6 votes vote down vote up
def getMulti(self,l):
        ''' Get a dict of counters from given id list. '''                
        tr = dict(memcache.get_multi(l,namespace=self.ns))
        gk = tr.keys()
        if len(l) > len(gk):
            l = l[:]
            for i in gk:
                l.remove(i)
            d = {}
            r = AuxCounterModel.gql("WHERE id in :1",l)
            for i in r:
                l.remove(i.id)
                tr[i.id] = i.count
                d[i.id] = i.count
            memcache.set_multi(d,namespace=self.ns)
            for i in l:
                tr[i] = 0

        return tr 
Example 2
Project: server   Author: viur-framework   File: ratelimit.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def isQuotaAvailable(self):
		"""
		Checks if there's currently quota available for the current user/ip
		:return: True if there's quota available, False otherwise
		:rtype: bool
		"""
		endPoint = self._getEndpointKey()
		dateTime = datetime.now()
		secSinceMidnight = (dateTime - dateTime.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
		currentStep = int(secSinceMidnight/self.secondsPerStep)
		keyBase = dateTime.strftime("%Y-%m-%d-%%s")
		memcacheKeys = []
		for x in range(0, self.steps):
			memcacheKeys.append("%s-%s-%s" % (self.resource, endPoint, keyBase % (currentStep - x)))
		tmpRes = memcache.get_multi(memcacheKeys)
		return sum(tmpRes.values()) <= self.maxRate 
Example 3
Project: oppia   Author: google-code-export   File: gae_memcache_services.py    Apache License 2.0 6 votes vote down vote up
def get_multi(keys):
    """Looks up a list of keys in memcache.

    Args:
      - keys: a list of keys (strings) to look up.

    Returns:
      A dict of key-value pairs for the keys/values that were present in
      memcache.
    """
    assert isinstance(keys, list)
    result = memcache.get_multi(keys)

    if result is not None:
        counters.MEMCACHE_HIT.inc()
    else:
        counters.MEMCACHE_MISS.inc()

    return result 
Example 4
Project: iris   Author: doitintl   File: main.py    MIT License 5 votes vote down vote up
def 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])
    try:
        return pickle.loads(serialized)
    except EOFError as e:
        logging.info(e)
        return None 
Example 5
Project: hackernewsbot   Author: phil-r   File: main.py    MIT License 5 votes vote down vote up
def task(stories):
  def check_story(rpc):
    try:
      result = rpc.get_result()
      story = json.loads(result.content)
      if story and story.get('score') >= 100:
        StoryPost.add(story)
      elif story and story.get('score'):
        # api returned a comment once (issue with id 21447853)
        logging.info('STOP: {id} has low score ({score})'.format(**story))
      elif story:
        logging.info('STOP: {id} has no score'.format(**story))
      else:
        logging.info("STOP: story was probably deleted/flagged")
    except urlfetch.DownloadError as ex:
      logging.exception(ex)
    except ValueError as ex:
      logging.info(result.content)
      logging.exception(ex)


  # stringify ids for use in memcache and convert to set for later
  ids = set(str(story_id) for story_id in stories)
  logging.info('checking stories: {}'.format(ids))
  # get stories that we already posted to reduce the number of requests
  cached_stories = set(memcache.get_multi(ids).keys())
  logging.info('cached stories: {}'.format(cached_stories))
  # remove stories we know about from stories that we need to check
  stories_to_check = ids.difference(cached_stories)
  rpcs = map(lambda id: item_async(id, check_story), stories_to_check)
  for rpc in rpcs:
    rpc.wait() 
Example 6
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def test_get_multi(keys):
    """Check get_multi is identical to ndb get_multi"""
    entities = get_multi(keys)
    ndb_entities = ndb.get_multi(keys)
    returned_entity_keys = set([e.key.id() for e in entities])
    ndb_returned_entity_keys = set([e.key.id() for e in ndb_entities])
    assert(returned_entity_keys == ndb_returned_entity_keys) 
Example 7
Project: electioncharts   Author: mickeykedia   File: main.py    Apache License 2.0 5 votes vote down vote up
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) 
Example 8
Project: python-docs-samples   Author: GoogleCloudPlatform   File: batch.py    Apache License 2.0 5 votes vote down vote up
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] 
Example 9
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    Apache License 2.0 5 votes vote down vote up
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)