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

The following are 30 code examples for showing how to use google.appengine.api.memcache.add(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module google.appengine.api.memcache , or try the search function .

Example 1
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 2
Project: splunk-ref-pas-code   Author: splunk   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 3
Project: sndlatr   Author: Schibum   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 4
Project: billing-export-python   Author: googlearchive   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 5
Project: RSSNewsGAE   Author: liantian-cn   File: gae.py    License: Apache License 2.0 6 votes vote down vote up
def open_resource(self, name):
        """Opens a resource from the zoneinfo subdir for reading."""
        # Import nested here so we can run setup.py without GAE.
        from google.appengine.api import memcache
        from pytz import OLSON_VERSION

        name_parts = name.lstrip('/').split('/')
        if os.path.pardir in name_parts:
            raise ValueError('Bad path segment: %r' % os.path.pardir)

        cache_key = 'pytz.zoneinfo.%s.%s' % (OLSON_VERSION, name)
        zonedata = memcache.get(cache_key)
        if zonedata is None:
            zonedata = get_zoneinfo().read('zoneinfo/' + '/'.join(name_parts))
            memcache.add(cache_key, zonedata)
            log.info('Added timezone to memcache: %s' % cache_key)
        else:
            log.info('Loaded timezone from memcache: %s' % cache_key)

        return StringIO(zonedata) 
Example 6
Project: aqua-monitor   Author: Deltares   File: appengine.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 7
Project: googleapps-message-recall   Author: google   File: sharded_counter.py    License: Apache License 2.0 6 votes vote down vote up
def GetCounterCount(name):
  """Sums a cumulative value from all the shard counts for the given name.

  Args:
    name: The name of the counter.

  Returns:
    Integer; the cumulative count of all sharded counters for the given
    counter name.
  """
  total = memcache.get(key=name)
  if total is not None:
    return total

  total = 0
  all_keys = CounterShardConfig.AllKeys(name)
  for counter in ndb.get_multi(all_keys):
    if counter is not None:
      total += counter.count
  if memcache.add(key=name, value=total,
                  time=_COUNTER_MEMCACHE_EXPIRATION_S):
    return total
  raise recall_errors.MessageRecallCounterError(
      'Unexpected problem adding to memcache: %s.' % name) 
Example 8
Project: googleapps-message-recall   Author: google   File: frontend_views.py    License: Apache License 2.0 6 votes vote down vote up
def _CacheUserEmailBillingEnabled(user_email):
  """Cache the user_email to avoid billing-check rountrips.

  Wrapped in a separate method to aid error handling.

  Args:
    user_email: String email address of the form user@domain.com.

  Raises:
    MessageRecallMemcacheError: If the add fails so cache issues can be noticed.
  """
  if not memcache.add(user_email, True, time=_USER_BILLING_CACHE_TIMEOUT_S,
                      namespace=_APPLICATION_BILLING_CACHE_NAMESPACE):
    raise recall_errors.MessageRecallMemcacheError(
        'Unexpectedly unable to add application billing information to '
        'memcache. Please try again.') 
Example 9
Project: googleapps-message-recall   Author: google   File: frontend_views.py    License: Apache License 2.0 6 votes vote down vote up
def _CacheUserEmailAsAdmin(user_email):
  """Cache the admin user_email to avoid rountrips.

  Wrapped in a separate method to aid error handling.

  Args:
    user_email: String email address of the form user@domain.com.

  Raises:
    MessageRecallMemcacheError: If the add fails so cache issues can be noticed.
  """
  if not memcache.add(user_email, True, time=_USER_ADMIN_CACHE_TIMEOUT_S,
                      namespace=_USER_ADMIN_CACHE_NAMESPACE):
    raise recall_errors.MessageRecallMemcacheError(
        'Unexpectedly unable to add admin user information to memcache. '
        'Please try again.') 
Example 10
Project: googleapps-message-recall   Author: google   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 11
Project: alfred-gmail   Author: fniephaus   File: appengine.py    License: MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 12
Project: luci-py   Author: luci   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 13
Project: luci-py   Author: luci   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 14
Project: luci-py   Author: luci   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 15
Project: luci-py   Author: luci   File: handlers_endpoints.py    License: Apache License 2.0 6 votes vote down vote up
def count(self, request):
    """Counts number of tasks in a given state."""
    logging.debug('%s', request)

    # Check permission.
    # If the caller has global permission, it can access all tasks.
    # Otherwise, it requires pool dimension to check ACL.
    realms.check_tasks_list_acl(request.tags)

    if not request.start:
      raise endpoints.BadRequestException('start (as epoch) is required')
    now = utils.utcnow()
    mem_key = self._memcache_key(request, now)
    count = memcache.get(mem_key, namespace='tasks_count')
    if count is not None:
      return swarming_rpcs.TasksCount(count=count, now=now)

    try:
      count = self._query_from_request(request, 'created_ts').count()
      memcache.add(mem_key, count, 24*60*60, namespace='tasks_count')
    except ValueError as e:
      raise endpoints.BadRequestException(
          'Inappropriate filter for tasks/count: %s' % e)
    return swarming_rpcs.TasksCount(count=count, now=now) 
Example 16
Project: luci-py   Author: luci   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 17
Project: twitter-for-bigquery   Author: twitterdev   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 18
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    License: Apache License 2.0 6 votes vote down vote up
def start_recording(env=None):
  """Start recording RPC traces.

  This creates a Recorder instance and sets it for the current request
  in the global RequestLocalRecorderProxy 'recorder_proxy'.

  Args:
    env: Optional WSGI environment; defaults to os.environ.
  """
  recorder_proxy.clear_for_current_request()
  if env is None:
    env = os.environ
  if not config.should_record(env):
    return

  if memcache.add(lock_key(), 0,
                  time=config.LOCK_TIMEOUT, namespace=config.KEY_NAMESPACE):
    recorder_proxy.set_for_current_request(Recorder(env))
    if config.DEBUG:
      logging.debug('Set recorder') 
Example 19
Project: python-for-android   Author: kuri65536   File: cron.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self):
        # Check if any tasks need to be run
        query = _AppEngineUtilities_Cron.all()
        query.filter('next_run <= ', datetime.datetime.now())
        results = query.fetch(1000)
        if len(results) > 0:
            one_second = datetime.timedelta(seconds = 1)
            before  = datetime.datetime.now()
            for r in results:
                if re.search(':' + APPLICATION_PORT, r.url):
                    r.url = re.sub(':' + APPLICATION_PORT, ':' + CRON_PORT, r.url)
                #result = urlfetch.fetch(r.url)
                diff = datetime.datetime.now() - before
                if int(diff.seconds) < 1:
                    if memcache.add(str(r.key), "running"):
                        result = urlfetch.fetch(r.url)
                        r.next_run = self._get_next_run(pickle.loads(r.cron_compiled))
                        r.put()
                        memcache.delete(str(r.key))
                else:
                    break 
Example 20
Project: jarvis   Author: haynieresearch   File: appengine.py    License: GNU General Public License v2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 21
Project: data   Author: jmankoff   File: appengine.py    License: GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 22
Project: data   Author: jmankoff   File: appengine.py    License: GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 23
Project: data   Author: jmankoff   File: appengine.py    License: GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 24
Project: data   Author: jmankoff   File: appengine.py    License: GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 25
Project: data   Author: jmankoff   File: appengine.py    License: GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 26
Project: browserscope   Author: elsigh   File: shardedcounter.py    License: Apache License 2.0 5 votes vote down vote up
def get_count(name):
  """Retrieve the value for a given sharded counter.

  Parameters:
    name - The name of the counter
  """
  total = memcache.get(name, namespace=NAMESPACE)
  if total is None:
    total = 0
    for counter in GeneralCounterShard.all().filter('name = ', name):
      total += counter.count
    memcache.add(name, str(total), 60, namespace=NAMESPACE)
  return total 
Example 27
Project: browserscope   Author: elsigh   File: user_test.py    License: Apache License 2.0 5 votes vote down vote up
def add_memcache(self, test):
    memcache_keyname = self.get_memcache_keyname(test)
    memcache.delete(memcache_keyname)
    memcache.add(memcache_keyname, self,
                 settings.STATS_USERTEST_TIMEOUT) 
Example 28
Project: browserscope   Author: elsigh   File: user_test.py    License: Apache License 2.0 5 votes vote down vote up
def add_memcache(self):
    self.delete_memcache()
    memcache.add(self.get_memcache_keyname(), self,
                 settings.STATS_USERTEST_TIMEOUT) 
Example 29
Project: Sony-PMCA-RE   Author: ma1co   File: main.py    License: MIT License 5 votes vote down vote up
def cached(key, func, time=15*60):
 # TODO values > 1MB
 res = memcache.get(key)
 if not res:
  res = func()
  try:
   memcache.add(key, res, time)
  except:
   pass
 return res 
Example 30
Project: pledgeservice   Author: MayOneUS   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def getTeamTotal(self, team):
    self.response.headers['Content-Type'] = 'application/javascript'
    key = "TEAM-TOTAL-%s" % team
    res = memcache.get(key)
    if not res:
      total_pledges, total_amount = 0, 0
      for pledge in model.Pledge.all().filter("team =", team):
        total_pledges += 1
        total_amount += pledge.amountCents
      # doh, we should probably return a json object here instead of just an
      # some ints, but we'd like to be backwards compatible with the previous
      # (non-team) api. so for now, let's make use javascript varargs
      res = '(%d, %d)' % (total_amount, total_pledges)
      memcache.add(key, res, 60)
    self.response.write("%s%s" % (self.request.get('callback'), res))