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

The following are 30 code examples for showing how to use google.appengine.api.memcache.delete(). 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: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def ClearDatastore(request):
    """Clears data in the datastore, many at a time (for admins only)."""
    clear = (None, 'None')
    atatime = 10

    msg = ''
    query = db.Query(clear[0])
    rows = query.fetch(atatime)
    length = len(rows)
    if length is 0:
        msg += 'No more rows to delete<br>'
    else:
        msg += 'Deleting %s %s<br>' % (length, clear[1])
        db.delete(rows)
        query = db.Query(clear[0])
        more = query.fetch(1)
        if len(more) is not 0:
            msg += 'Now do it again!'
            msg += '<script>window.location.href="/reflows/clear_datastore";</script>'
    return http.HttpResponse(msg) 
Example 2
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
        # We set the same parent key on the 'Greeting' to ensure each greeting
        # is in the same entity group. Queries across the single entity group
        # are strongly consistent. However, the write rate to a single entity
        # group is limited to ~1/second.
        guestbook_name = self.request.get('guestbook_name')
        greeting = Greeting(parent=guestbook_key(guestbook_name))

        if users.get_current_user():
            greeting.author = users.get_current_user().nickname()

        greeting.content = self.request.get('content')
        greeting.put()
        memcache.delete('{}:greetings'.format(guestbook_name))
        self.redirect('/?' +
                      urllib.urlencode({'guestbook_name': guestbook_name})) 
Example 3
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    License: Apache License 2.0 6 votes vote down vote up
def end_recording(status, firepython_set_extension_data=None):
  """Stop recording RPC traces and save all traces to memcache.

  This clears the recorder set for this request in 'recorder_proxy'.

  Args:
    status: HTTP Status, a 3-digit integer.
  """
  if firepython_set_extension_data is not None:
    warnings.warn('Firepython is no longer supported')
  rec = recorder_proxy.get_for_current_request()
  recorder_proxy.clear_for_current_request()
  if config.DEBUG:
    logging.debug('Cleared recorder')
  if rec is not None:
    try:
      rec.record_http_status(status)
      rec.save()
    finally:
      memcache.delete(lock_key(), namespace=config.KEY_NAMESPACE) 
Example 4
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
    """Handle POST."""
    index_name = self.request.get('index')
    namespace = self.request.get('namespace')

    docs = []
    index = 0
    num_docs = int(self.request.get('numdocs'))
    for i in xrange(1, num_docs+1):
      key = self.request.get('doc%d' % i)
      if key:
        docs.append(key)

    index = search.Index(name=index_name, namespace=namespace)
    index.delete(docs)
    self.redirect(self.request.get('next')) 
Example 5
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 6
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 6 votes vote down vote up
def get_items(self):
        """
        Returns all the items stored in a session
        """
        items = memcache.get("_AppEngineUtilities_SessionData_" + str(self.session_key))
        if items:
            for item in items:
                if item.deleted == True:
                    item.delete()
                    items.remove(item)
            return items

        query = _AppEngineUtilities_SessionData.all()
        query.filter('session_key', self.session_key)
        results = query.fetch(1000)
        return results 
Example 7
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 6 votes vote down vote up
def get_item(self, keyname = None):
        """
        Returns a single item from the memcache or datastore
        """
        mc = memcache.get("_AppEngineUtilities_SessionData_" + str(self.session_key))
        if mc:
            for item in mc:
                if item.keyname == keyname:
                    if item.deleted == True:
                        item.delete()
                        return None
                    return item
        query = _AppEngineUtilities_SessionData.all()
        query.filter("session_key = ", self.session_key)
        query.filter("keyname = ", keyname)
        results = query.fetch(1)
        if len(results) > 0:
            memcache.set("_AppEngineUtilities_SessionData_" + str(self.session_key), self.get_items_ds())
            return results[0]
        return None 
Example 8
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 6 votes vote down vote up
def delete_all_sessions(cls):
        """
        Deletes all sessions and session data from the data store and memcache:

        NOTE: This is not fully developed. It also will not delete any cookie
        data as this does not work for each incoming request. Keep this in mind
        if you are using the cookie writer.
        """
        all_sessions_deleted = False
        all_data_deleted = False

        while not all_sessions_deleted:
            query = _AppEngineUtilities_Session.all()
            results = query.fetch(75)
            if len(results) is 0:
                all_sessions_deleted = True
            else:
                for result in results:
                    result.delete() 
Example 9
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 6 votes vote down vote up
def _clean_old_sessions(self):
        """
        Delete expired sessions from the datastore.

        This is only called for CLEAN_CHECK_PERCENT percent of requests because
        it could be rather intensive.
        """
        duration = datetime.timedelta(seconds=self.session_expire_time)
        session_age = datetime.datetime.now() - duration
        query = _AppEngineUtilities_Session.all()
        query.filter('last_activity <', session_age)
        results = query.fetch(50)
        for result in results:
            """
            OLD
            data_query = _AppEngineUtilities_SessionData.all()
            data_query.filter('session', result)
            data_results = data_query.fetch(1000)
            for data_result in data_results:
                data_result.delete()
            memcache.delete('sid-'+str(result.key()))
            """
            result.delete()

    # Implement Python container methods 
Example 10
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 6 votes vote down vote up
def __delitem__(self, keyname):
        """
        Delete item from session data.

        Args:
            keyname: The keyname of the object to delete.
        """
        bad_key = False
        sessdata = self._get(keyname = keyname)
        if sessdata is None:
            bad_key = True
        else:
            sessdata.delete()
        if keyname in self.cookie_vals:
            del self.cookie_vals[keyname]
            bad_key = False
            self.output_cookie[self.cookie_name + '_data'] = \
                simplejson.dumps(self.cookie_vals)
            print self.output_cookie.output()
        if bad_key:
            raise KeyError(str(keyname))
        if keyname in self.cache:
            del self.cache[keyname] 
Example 11
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 12
Project: browserscope   Author: elsigh   File: user_test.py    License: Apache License 2.0 5 votes vote down vote up
def delete_memcache(self):
    memcache.delete(self.get_memcache_keyname()) 
Example 13
Project: browserscope   Author: elsigh   File: result_stats.py    License: Apache License 2.0 5 votes vote down vote up
def DeleteMemcacheValue(cls, category, version_level):
        key_name = cls.KeyName(category, version_level)
        memcache.delete(key_name, namespace=cls.MEMCACHE_NAMESPACE) 
Example 14
Project: pledgeservice   Author: MayOneUS   File: cache.py    License: Apache License 2.0 5 votes vote down vote up
def ClearShardedCounterTotal(name):
  memcache.delete(_COUNTER_TOTAL.format(name)) 
Example 15
Project: isthislegit   Author: duo-labs   File: memcache.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def delete(key=None):
    parts_count = memcache.get(key='%s_parts' % key)
    if parts_count:
        for i in xrange(parts_count):
            memcache.delete(key='%s%d' % (key, i))

    memcache.delete(key=key)
    memcache.delete(key='%s_parts' % key) 
Example 16
Project: cloud-pubsub-samples-python   Author: GoogleCloudPlatform   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def post(self):
        if constants.SUBSCRIPTION_UNIQUE_TOKEN != self.request.get('token'):
            self.response.status = 404
            return

        # Store the message in the datastore.
        logging.debug('Post body: {}'.format(self.request.body))
        message = json.loads(urllib.unquote(self.request.body).rstrip('='))
        message_body = base64.b64decode(str(message['message']['data']))
        pubsub_message = PubSubMessage(message=message_body)
        pubsub_message.put()

        # Invalidate the cache
        memcache.delete(MESSAGE_CACHE_KEY)
        self.response.status = 200 
Example 17
Project: beibq   Author: chaijunit   File: main.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def post(self):
        if (self.request.get('_method') == 'DELETE'):
            return self.delete()
        result = {'files': self.handle_upload()}
        s = self.json_stringify(result)
        redirect = self.request.get('redirect')
        if self.validate_redirect(redirect):
            return self.redirect(str(
                redirect.replace('%s', urllib.quote(s, ''), 1)
            ))
        if 'application/json' in self.request.headers.get('Accept'):
            self.response.headers['Content-Type'] = 'application/json'
        self.response.write(s) 
Example 18
Project: beibq   Author: chaijunit   File: main.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def delete(self, content_type, data_hash, file_name):
        content_type = self.normalize(content_type)
        file_name = self.normalize(file_name)
        key = content_type + '/' + data_hash + '/' + file_name
        result = {key: memcache.delete(key)}
        content_type = urllib.unquote(content_type)
        if IMAGE_TYPES.match(content_type):
            thumbnail_key = key + THUMB_SUFFIX
            result[thumbnail_key] = memcache.delete(thumbnail_key)
        if 'application/json' in self.request.headers.get('Accept'):
            self.response.headers['Content-Type'] = 'application/json'
        s = self.json_stringify(result)
        self.response.write(s) 
Example 19
Project: python-docs-samples   Author: GoogleCloudPlatform   File: failure.py    License: Apache License 2.0 5 votes vote down vote up
def get(self):
        key = "some key"
        seconds = 5
        memcache.set(key, "some value")
        # [START memcache-delete]
        memcache.delete(key, seconds)  # clears cache
        # write to persistent datastore
        # Do not attempt to put new value in cache, first reader will do that
        # [END memcache-delete]
        self.response.content_type = 'text/html'
        self.response.write('done') 
Example 20
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_data_not_present(query_fn, testbed):
    data = snippets.get_data()
    query_fn.assert_called_once_with()
    assert data == 'data'
    memcache.delete('key') 
Example 21
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_data_present(query_fn, testbed):
    memcache.add('key', 'data', 9000)
    data = snippets.get_data()
    query_fn.assert_not_called()
    assert data == 'data'
    memcache.delete('key') 
Example 22
Project: luci-py   Author: luci   File: task_to_run.py    License: Apache License 2.0 5 votes vote down vote up
def set_lookup_cache(to_run_key, is_available_to_schedule):
  """Updates the quick lookup cache to mark an item as available or not.

  This cache is a blacklist of items that are about to be reaped or are already
  reaped, so it is not worth trying to reap it with a DB transaction. This saves
  on DB contention when a high number (>1000) of concurrent bots with similar
  dimension are reaping tasks simultaneously. In this case, there is a high
  likelihood that multiple concurrent HTTP handlers are trying to reap the exact
  same task simultaneously. This blacklist helps reduce the contention by
  telling the other bots to back off.

  Another reason for this negative cache is that the DB index takes some seconds
  to be updated, which means it can return stale items (e.g. already reaped).

  It can be viewed as a lock, except that the 'lock' is never released, it is
  impliclity released after 15 seconds.

  Returns:
    True if the key was updated, False if was trying to reap and the entry was
    already set.
  """
  # Set the expiration time for items in the negative cache as 15 seconds. This
  # copes with significant index inconsistency but do not clog the memcache
  # server with unneeded keys.
  cache_lifetime = 15

  key = _memcache_to_run_key(to_run_key)
  if is_available_to_schedule:
    # The item is now available, so remove it from memcache.
    memcache.delete(key, namespace='task_to_run')
    return True

  # add() returns True if the entry was added, False otherwise. That's perfect.
  return memcache.add(key, True, time=cache_lifetime, namespace='task_to_run') 
Example 23
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: memcache_stub.py    License: Apache License 2.0 5 votes vote down vote up
def ExpireAndLock(self, timeout):
    """Marks this entry as deleted and locks it for the expiration time.

    Used to implement memcache's delete timeout behavior.

    Args:
      timeout: Parameter originally passed to memcache.delete or
        memcache.delete_multi to control deletion timeout.
    """
    self.will_expire = True
    self.locked = True
    self._SetExpiration(timeout) 
Example 24
Project: python-for-android   Author: kuri65536   File: app_engine.py    License: Apache License 2.0 5 votes vote down vote up
def set_token(unique_key, token_str):
  """Saves the serialized auth token in the datastore.

  The token is also stored in memcache to speed up retrieval on a cache hit.

  Args:
    unique_key: The unique name for this token as a string. It is up to your
        code to ensure that this token value is unique in your application.
        Previous values will be silently overwitten.
    token_str: A serialized auth token as a string. I expect that this string
        will be generated by gdata.gauth.token_to_blob.

  Returns:
    True if the token was stored sucessfully, False if the token could not be
    safely cached (if an old value could not be cleared). If the token was
    set in memcache, but not in the datastore, this function will return None.
    However, in that situation an exception will likely be raised.

  Raises:
    Datastore exceptions may be raised from the App Engine SDK in the event of
    failure.
  """
  # First try to save in memcache.
  result = memcache.set(unique_key, token_str)
  # If memcache fails to save the value, clear the cached value.
  if not result:
    result = memcache.delete(unique_key)
    # If we could not clear the cached value for this token, refuse to save.
    if result == 0:
      return False
  # Save to the datastore.
  if Token(key_name=unique_key, t=token_str).put():
    return True
  return None 
Example 25
Project: python-for-android   Author: kuri65536   File: app_engine.py    License: Apache License 2.0 5 votes vote down vote up
def delete_token(unique_key):
  # Clear from memcache.
  memcache.delete(unique_key)
  # Clear from the datastore.
  Token(key_name=unique_key).delete() 
Example 26
Project: python-for-android   Author: kuri65536   File: app_engine.py    License: Apache License 2.0 5 votes vote down vote up
def set_token(unique_key, token_str):
  """Saves the serialized auth token in the datastore.
  
  The token is also stored in memcache to speed up retrieval on a cache hit.

  Args:
    unique_key: The unique name for this token as a string. It is up to your
        code to ensure that this token value is unique in your application.
        Previous values will be silently overwitten.
    token_str: A serialized auth token as a string. I expect that this string
        will be generated by gdata.gauth.token_to_blob.

  Returns:
    True if the token was stored sucessfully, False if the token could not be
    safely cached (if an old value could not be cleared). If the token was
    set in memcache, but not in the datastore, this function will return None.
    However, in that situation an exception will likely be raised.
    
  Raises:
    Datastore exceptions may be raised from the App Engine SDK in the event of
    failure.
  """
  # First try to save in memcache.
  result = memcache.set(unique_key, token_str)
  # If memcache fails to save the value, clear the cached value.
  if not result:
    result = memcache.delete(unique_key)
    # If we could not clear the cached value for this token, refuse to save.
    if result == 0:
      return False
  # Save to the datastore.
  if Token(key_name=unique_key, t=token_str).put():
    return True
  return None 
Example 27
Project: python-for-android   Author: kuri65536   File: app_engine.py    License: Apache License 2.0 5 votes vote down vote up
def delete_token(unique_key):
  # Clear from memcache.
  memcache.delete(unique_key)
  # Clear from the datastore.
  Token(key_name=unique_key).delete() 
Example 28
Project: python-for-android   Author: kuri65536   File: app_engine.py    License: Apache License 2.0 5 votes vote down vote up
def set_token(unique_key, token_str):
  """Saves the serialized auth token in the datastore.
  
  The token is also stored in memcache to speed up retrieval on a cache hit.

  Args:
    unique_key: The unique name for this token as a string. It is up to your
        code to ensure that this token value is unique in your application.
        Previous values will be silently overwitten.
    token_str: A serialized auth token as a string. I expect that this string
        will be generated by gdata.gauth.token_to_blob.

  Returns:
    True if the token was stored sucessfully, False if the token could not be
    safely cached (if an old value could not be cleared). If the token was
    set in memcache, but not in the datastore, this function will return None.
    However, in that situation an exception will likely be raised.
    
  Raises:
    Datastore exceptions may be raised from the App Engine SDK in the event of
    failure.
  """
  # First try to save in memcache.
  result = memcache.set(unique_key, token_str)
  # If memcache fails to save the value, clear the cached value.
  if not result:
    result = memcache.delete(unique_key)
    # If we could not clear the cached value for this token, refuse to save.
    if result == 0:
      return False
  # Save to the datastore.
  if Token(key_name=unique_key, t=token_str).put():
    return True
  return None 
Example 29
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 5 votes vote down vote up
def delete(self):
        try:
            query = _AppEngineUtilities_SessionData.all()
            query.filter("session_key = ", self.session_key)
            results = query.fetch(1000)
            db.delete(results)
            db.delete(self)
            memcache.delete_multi(["_AppEngineUtilities_Session_" + str(self.session_key), "_AppEngineUtilities_SessionData_" + str(self.session_key)])
        except:
            mc = memcache.get("_AppEngineUtilities_Session_" + str(self.session_key))
            mc.deleted = True
            memcache.set("_AppEngineUtilities_Session_" + str(self.session_key), mc) 
Example 30
Project: python-for-android   Author: kuri65536   File: sessions.py    License: Apache License 2.0 5 votes vote down vote up
def put(self, keyname, value, session):
        """
        Insert a keyname/value pair into the datastore for the session.

        Args:
            keyname: The keyname of the mapping.
            value: The value of the mapping.
        """
        keyname = session._validate_key(keyname)
        if value is None:
            raise ValueError('You must pass a value to put.')

        # datestore write trumps cookie. If there is a cookie value
        # with this keyname, delete it so we don't have conflicting
        # entries.
        if session.cookie_vals.has_key(keyname):
            del(session.cookie_vals[keyname])
            session.output_cookie[session.cookie_name + '_data'] = \
                simplejson.dumps(session.cookie_vals)
            print session.output_cookie.output()

        sessdata = session._get(keyname=keyname)
        if sessdata is None:
            sessdata = _AppEngineUtilities_SessionData()
            sessdata.session_key = session.session.session_key
            sessdata.keyname = keyname
        sessdata.content = pickle.dumps(value)
        # UNPICKLING CACHE session.cache[keyname] = pickle.dumps(value)
        session.cache[keyname] = value
        sessdata.put()
        # todo _set_memcache() should be going away when this is done
        # session._set_memcache()