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

The following are code examples for showing how to use google.appengine.api.memcache.set_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 setMulti(self,d):
        ''' Set counters with given ids as dict. Counter.setMulti({dictionary}) '''     
        for i in d.values():
            if not type(i) in (int,long):
                return None
        memcache.set_multi(d,namespace=self.ns)
        nid = d.keys()
        r = AuxCounterModel.gql("WHERE id in :1",nid)
        for i in r:
            nid.remove(i.id)
            i.count = int(d[i.id])
            i.put()
        if nid:
            tc = []
            for i in nid:
                tc.append(self.__com(i,d[i]))
            db.put(tc) 
Example 2
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 3
Project: kay-template   Author: yosukesuzuki   File: __init__.py    MIT License 6 votes vote down vote up
def set_multi(self, data, expire=_DEFAULT_TTL, namespace=None):
    from kay.ext.live_settings.models import KayLiveSetting

    old_namespace = namespace_manager.get_namespace()
    try:
      if namespace is not None:
        namespace_manager.set_namespace(namespace)

      data_items = data.items()
      db.put(map(lambda x: KayLiveSetting(key_name=x[0], ttl=expire, value=x[1]),
          data_items))
      memcache.set_multi(dict(map(lambda x: (
          "kay:live:%s" % x[0], (x[1],expire)
      ), data_items)))
  
      for key, value in data_items:
        self._set_local_cache(key, value, ttl=expire, namespace=namespace)
    finally:
      if namespace is not None:
        namespace_manager.set_namespace(old_namespace) 
Example 4
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets.py    Apache License 2.0 6 votes vote down vote up
def add_values():
    # [START add_values]
    # Add a value if it doesn't exist in the cache
    # with a cache expiration of 1 hour.
    memcache.add(key="weather_USA_98105", value="raining", time=3600)

    # Set several values, overwriting any existing values for these keys.
    memcache.set_multi(
        {"USA_98115": "cloudy", "USA_94105": "foggy", "USA_94043": "sunny"},
        key_prefix="weather_",
        time=3600
    )

    # Atomically increment an integer value.
    memcache.set(key="counter", value=0)
    memcache.incr("counter")
    memcache.incr("counter")
    memcache.incr("counter")
    # [END add_values] 
Example 5
Project: oppia   Author: google-code-export   File: gae_memcache_services.py    Apache License 2.0 6 votes vote down vote up
def set_multi(key_value_mapping):
    """Sets multiple keys' values at once.

    Args:
      - key_value_mapping: a dict of {key: value} pairs. The key is a string
          and the value is anything that is serializable using the Python
          pickle module. The combined size of each key and value must be
          < 1 MB. The total size of key_value_mapping should be at most 32 MB.

    Returns:
      A list of the keys whose values were NOT set.
    """
    assert isinstance(key_value_mapping, dict)
    unset_keys = memcache.set_multi(key_value_mapping)

    if unset_keys:
        counters.MEMCACHE_SET_FAILURE.inc()
    else:
        counters.MEMCACHE_SET_SUCCESS.inc()

    return unset_keys 
Example 6
Project: panwdbl   Author: jtschichold   File: panwdbl.py    ISC License 6 votes vote down vote up
def __get_iplist_info(self, tag):
        ipltime = memcache.get("t"+tag)
        iplist = memcache.get("l"+tag)
        if (ipltime is not None) and (iplist is not None):
            iplist = len(iplist)
            return ipltime, iplist
        else:
            q = BlockList.all()
            q.filter("tag =", tag)
            q.order("-time")
            iplist = q.get()
            if iplist is None:
                return '--', '--'
            else:
                ipltime = iplist.time.strftime('%d %b %Y %H:%M %Z')
                memcache.set_multi({"t"+tag: ipltime, "l"+tag: iplist.iplist}, 60*60*24)
                return ipltime, len(iplist.iplist) 
Example 7
Project: iris   Author: doitintl   File: main.py    MIT License 5 votes vote down vote up
def store(key, value, chunksize=950000):
    serialized = cloudpickle.dumps(value, 2)
    values = {}
    for i in xrange(0, len(serialized), chunksize):
        values['%s.%s' % (key, i // chunksize)] = serialized[i:i + chunksize]
    return memcache.set_multi(values) 
Example 8
Project: electioncharts   Author: mickeykedia   File: main.py    Apache License 2.0 5 votes vote down vote up
def cache_store(key, value, time=30, chunksize=950000):
    serialized = pickle.dumps(value, 2)
    values = {}
    for i in xrange(0, len(serialized), chunksize):
        values['%s.%s' % (key, i//chunksize)] = serialized[i : i+chunksize]
    memcache.set_multi(values,time) 
Example 9
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 10
Project: luci-py   Author: luci   File: task_queues.py    Apache License 2.0 5 votes vote down vote up
def get_queues(bot_root_key):
  """Returns the known task queues as integers.

  This function is called to get the task queues to poll, as the bot is trying
  to reap a task, any task.

  It is also called while the bot is running a task, to refresh the task queues.

  Arguments:
    bot_root_key: ndb.Key to bot_management.BotRoot
  """
  bot_id = bot_root_key.string_id()
  data = memcache.get(bot_id, namespace='task_queues')
  if data is not None:
    # Note: This may return stale queues. We may want to change the format to
    # include the expiration.
    logging.debug(
        'get_queues(%s): can run from %d queues (memcache)\n%s',
        bot_id, len(data), data)
    # Refresh all the keys.
    memcache.set_multi(
        {str(d): True for d in data}, time=61, namespace='task_queues_tasks')
    return data

  # Retrieve all the dimensions_hash that this bot could run that have
  # actually been triggered in the past. Since this is under a root entity, this
  # should be fast.
  now = utils.utcnow()
  data = sorted(
      obj.key.integer_id() for obj in
      BotTaskDimensions.query(ancestor=bot_root_key)
      if obj.valid_until_ts >= now)
  memcache.set(bot_id, data, namespace='task_queues')
  logging.info(
      'get_queues(%s): Query in %.3fs: can run from %d queues\n%s',
      bot_id, (utils.utcnow()-now).total_seconds(), len(data), data)
  memcache.set_multi(
      {str(d): True for d in data}, time=61, namespace='task_queues_tasks')
  return data 
Example 11
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    Apache License 2.0 5 votes vote down vote up
def _save(self):
    """Internal function to save the recorded data to memcache.

    Returns:
      A tuple (key, summary_size, full_size).
    """
    part, full = self.get_both_protos_encoded()
    key = make_key(self.start_timestamp)
    errors = memcache.set_multi({config.PART_SUFFIX: part,
                                 config.FULL_SUFFIX: full},
                                time=36*3600, key_prefix=key,
                                namespace=config.KEY_NAMESPACE)
    if errors:
      logging.warn('Memcache set_multi() error: %s', errors)
    return key, len(part), len(full)