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

The following are code examples for showing how to use google.appengine.api.memcache.delete(). 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: analyzer-website   Author: santiagolizardo   File: site.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def post( self ):
		domain = self.request.get( 'domain' )

		title = self.request.get( 'title' )
		strengths = self.request.get_all( 'strengths[]' )
		weaknesses = self.request.get_all( 'weaknesses[]' )

		siteReview = SiteReview( domain = domain )
		siteReview.title = title
		siteReview.strengths = strengths
		siteReview.weaknesses = weaknesses
		siteReview.save()

		cache_key = 'page_' + domain
		memcache.delete( cache_key )

		self.response.set_status( 200 )
		self.response.headers['Access-Control-Allow-Origin'] = self.request.headers['Origin']
		self.response.headers['Content-Type'] = 'application/json'
		self.response.write( '"true"' ) 
Example 2
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 6 votes vote down vote up
def get(self, key, original_timestamp=None):
        fpath = self.filepath(key)
        #if not _isfile(fpath): return None
        stat = _ignore_not_found_error(lambda: os.stat(fpath), None)
        if stat is None:
            return None
        created_at = stat.st_ctime
        expires_at = stat.st_mtime
        if original_timestamp is not None and created_at < original_timestamp:
            self.delete(key)
            return None
        if expires_at < _time():
            self.delete(key)
            return None
        if self.encoding:
            f = lambda: _read_text_file(fpath, self.encoding)
        else:
            f = lambda: _read_binary_file(fpath)
        return _ignore_not_found_error(f, None) 
Example 3
Project: kay-template   Author: yosukesuzuki   File: __init__.py    MIT License 6 votes vote down vote up
def delete(self, key, 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)

      setting = KayLiveSetting.get_by_key_name(key)
      if setting:
          setting.delete()
      memcache.delete("kay:live:%s" % key)
      self._del_local_cache(key, namespace=namespace)
    finally:
      if namespace is not None:
        namespace_manager.set_namespace(old_namespace) 
Example 4
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 6 votes vote down vote up
def _cacheAnnouncement():
        """Create Announcement & assign to memcache; used by
        memcache cron job & putAnnouncement().
        """
        confs = Conference.query(ndb.AND(
            Conference.seatsAvailable <= 5,
            Conference.seatsAvailable > 0)
        ).fetch(projection=[Conference.name])

        if confs:
            # If there are almost sold out conferences,
            # format announcement and set it in memcache
            announcement = ANNOUNCEMENT_TPL % (
                ', '.join(conf.name for conf in confs))
            memcache.set(MEMCACHE_ANNOUNCEMENTS_KEY, announcement)
        else:
            # If there are no sold out conferences,
            # delete the memcache announcements entry
            announcement = ""
            memcache.delete(MEMCACHE_ANNOUNCEMENTS_KEY)

        return announcement 
Example 5
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 6 votes vote down vote up
def delete_async(name, num_shards):
   """
   Asynchronously delete the named counter and all of its shards.

   Args:
       name: The name of the counter.
       num_shards: the number of shards in the counter
       
   Return:
       A list of Futures for each entity making up the counter.
   """
   
   all_keys = _all_keys(name, num_shards)
   delete_futs = ndb.delete_multi_async( all_keys )
   
   memcache.delete( name )
   
   return delete_futs 
Example 6
Project: mymovie   Author: manish-pocketgems   File: main.py    MIT License 6 votes vote down vote up
def post(self):
        title = self.request.get('title')
        overview = self.request.get('overview')
        poster = self.request.get('poster')
        trailer_url = self.request.get('trailer_url')

        movie = Movie.query().filter(Movie.title == title).get()
        if not movie:
            movie = Movie(title=title,
                          overview=overview,
                          trailer=trailer_url,
                          poster=poster)
            movie.put()
            if memcache.delete(MEMCACHE_ALL_MOVIES_KEY) == memcache.DELETE_NETWORK_FAILURE:
                logging.info('deleting memcache all movies key failed')
            logging.info('deleting memcache all movies key')
        self.redirect('/') 
Example 7
Project: roger-api   Author: rogertalk   File: admin.py    MIT License 6 votes vote down vote up
def post_comments_json():
    content_id = int(request.args['content_id'])
    key = ndb.Key('Content', content_id,
                  'ContentComment', request.args['comment_id'])
    key.delete()
    futures = []
    v = 51
    c = ndb.get_context()
    for x in xrange(3):
        futures += [
            c.memcache_delete('content_comments_%s_%d_created' % (v - x, content_id)),
            c.memcache_delete('content_comments_%s_%d_offset' % (v - x, content_id)),
            c.memcache_delete('content_comments_%s_%d_threaded' % (v - x, content_id)),
        ]
    ndb.Future.wait_all(futures)
    return convert.to_json({'success': True}) 
Example 8
Project: Conference-Organization-APP-UDACITY   Author: kongling893   File: conference.py    Apache License 2.0 6 votes vote down vote up
def _cacheAnnouncement():
        """Create Announcement & assign to memcache; used by
        memcache cron job & putAnnouncement().
        """
        confs = Conference.query(ndb.AND(
            Conference.seatsAvailable <= 5,
            Conference.seatsAvailable > 0)
        ).fetch(projection=[Conference.name])

        if confs:
            # If there are almost sold out conferences,
            # format announcement and set it in memcache
            announcement = '%s %s' % (
                'Last chance to attend! The following conferences '
                'are nearly sold out:',
                ', '.join(conf.name for conf in confs))
            memcache.set(MEMCACHE_ANNOUNCEMENTS_KEY, announcement)
        else:
            # If there are no sold out conferences,
            # delete the memcache announcements entry
            announcement = ""
            memcache.delete(MEMCACHE_ANNOUNCEMENTS_KEY)

        return announcement 
Example 9
Project: server   Author: viur-framework   File: db.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def PutAsync( entities, **kwargs ):
	"""
		Asynchronously store one or more entities in the data store.

		This function is identical to :func:`server.db.Put`, except that it
		returns an asynchronous object. Call ``get_result()`` on the return value to
		block on the call and get the results.
	"""
	if isinstance( entities, Entity ):
		entities._fixUnindexedProperties()
	elif isinstance( entities, list ):
		for entity in entities:
			assert isinstance( entity, Entity )
			entity._fixUnindexedProperties()
	if conf["viur.db.caching" ]>0:
		if isinstance( entities, Entity ): #Just one:
			if entities.is_saved(): #Its an update
				memcache.delete( str( entities.key() ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
		elif isinstance( entities, list ):
			for entity in entities:
				assert isinstance( entity, Entity )
				if entity.is_saved(): #Its an update
					memcache.delete( str( entity.key() ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
	return( datastore.PutAsync( entities, **kwargs ) ) 
Example 10
Project: server   Author: viur-framework   File: db.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def DeleteAsync(keys, **kwargs):
	"""
		Asynchronously deletes one or more entities from the data store.

		This function is identical to :func:`server.db.Delete`, except that it
		returns an asynchronous object. Call ``get_result()`` on the return value to
		block on the call and get the results.
	"""
	if conf["viur.db.caching" ]>0:
		if isinstance( keys, datastore_types.Key ): #Just one:
			memcache.delete( str( keys ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
		elif isinstance( keys, list ):
			for key in keys:
				assert isinstance( key, datastore_types.Key ) or isinstance( key, basestring )
				memcache.delete( str( key ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
	return( datastore.DeleteAsync( keys, **kwargs ) ) 
Example 11
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    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 12
Project: deb-python-oauth2client   Author: openstack   File: test_appengine.py    Apache License 2.0 6 votes vote down vote up
def test_delete_ndb(self):
        # Start empty
        storage = appengine.StorageByKeyName(
            appengine.CredentialsNDBModel, 'foo', 'credentials')
        self.assertEqual(None, storage.get())

        # Add credentials to model with storage, and check equivalent
        # w/o storage
        storage.put(self.credentials)
        credmodel = appengine.CredentialsNDBModel.get_by_id('foo')
        self.assertEqual(credmodel.credentials.to_json(),
                         self.credentials.to_json())

        # Delete and make sure empty
        storage.delete()
        self.assertEqual(None, storage.get()) 
Example 13
Project: deb-python-oauth2client   Author: openstack   File: test_appengine.py    Apache License 2.0 6 votes vote down vote up
def test_delete_db_ndb_mixed(self):
        # Start empty
        storage_ndb = appengine.StorageByKeyName(
            appengine.CredentialsNDBModel, 'foo', 'credentials')
        storage = appengine.StorageByKeyName(
            appengine.CredentialsModel, 'foo', 'credentials')

        # First DB, then NDB
        self.assertEqual(None, storage.get())
        storage.put(self.credentials)
        self.assertNotEqual(None, storage.get())

        storage_ndb.delete()
        self.assertEqual(None, storage.get())

        # First NDB, then DB
        self.assertEqual(None, storage_ndb.get())
        storage_ndb.put(self.credentials)

        storage.delete()
        self.assertNotEqual(None, storage_ndb.get())
        # NDB uses memcache and an instance cache (Context)
        ndb.get_context().clear_cache()
        memcache.flush_all()
        self.assertEqual(None, storage_ndb.get()) 
Example 14
Project: deb-python-oauth2client   Author: openstack   File: test_appengine.py    Apache License 2.0 6 votes vote down vote up
def test_build_and_parse_state(self):
        secret = appengine.xsrf_secret_key()

        # Secret shouldn't change from call to call.
        secret2 = appengine.xsrf_secret_key()
        self.assertEqual(secret, secret2)

        # Secret shouldn't change if memcache goes away.
        memcache.delete(appengine.XSRF_MEMCACHE_ID,
                        namespace=appengine.OAUTH2CLIENT_NAMESPACE)
        secret3 = appengine.xsrf_secret_key()
        self.assertEqual(secret2, secret3)

        # Secret should change if both memcache and the model goes away.
        memcache.delete(appengine.XSRF_MEMCACHE_ID,
                        namespace=appengine.OAUTH2CLIENT_NAMESPACE)
        model = appengine.SiteXsrfSecretKey.get_or_insert('site')
        model.delete()

        secret4 = appengine.xsrf_secret_key()
        self.assertNotEqual(secret3, secret4) 
Example 15
Project: Conference_Central   Author: ananthashankar   File: conference.py    Apache License 2.0 6 votes vote down vote up
def _cacheSpeaker(speaker):
        """ Add Featured Speaker """
        
        # Set featured speaker with session count

        if not speaker:
            featuredSpeaker = "Empty"
            memcache.delete(MEMCACHE_FEATURED_SPEAKER_KEY)
        else:
            # delete if no speaker
            featuredSpeaker = {}
            sessions = Session.query(Session.speaker == speaker).fetch()
            featuredSpeaker[speaker] = sessions
            memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, value=str(featuredSpeaker))

        return featuredSpeaker 
Example 16
Project: Conference_Central   Author: ananthashankar   File: conference.py    Apache License 2.0 6 votes vote down vote up
def _cacheAnnouncement():
        """Create Announcement & assign to memcache; used by
        memcache cron job & putAnnouncement().
        """
        confs = Conference.query(ndb.AND(
            Conference.seatsAvailable <= 5,
            Conference.seatsAvailable > 0)
        ).fetch(projection=[Conference.name])

        if confs:
            # If there are almost sold out conferences,
            # format announcement and set it in memcache
            announcement = '%s %s' % (
                'Last chance to attend! The following conferences '
                'are nearly sold out:',
                ', '.join(conf.name for conf in confs))
            memcache.set(MEMCACHE_ANNOUNCEMENTS_KEY, announcement)
        else:
            # If there are no sold out conferences,
            # delete the memcache announcements entry
            announcement = ""
            memcache.delete(MEMCACHE_ANNOUNCEMENTS_KEY)

        return announcement 
Example 17
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    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 18
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: __init__.py    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 19
Project: python-for-android   Author: kuri65536   File: cron.py    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: python-for-android   Author: kuri65536   File: sessions.py    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 21
Project: python-for-android   Author: kuri65536   File: sessions.py    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 22
Project: python-for-android   Author: kuri65536   File: sessions.py    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 23
Project: python-for-android   Author: kuri65536   File: sessions.py    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 24
Project: python-for-android   Author: kuri65536   File: sessions.py    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 25
Project: analyzer-website   Author: santiagolizardo   File: site.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def post( self ):
		domain = self.request.get( 'domain' )

		content = self.request.get( 'content' )
		usability = self.request.get( 'usability' )
		presentation = self.request.get( 'presentation' )

		report = SiteRating.all().filter( 'domain =', domain ).get()
		if report is None:
			report = SiteRating( domain = domain, rating_overall = 0.0 )
		report.num_raters += 1
		report.rating_content = report.rating_content + int( content )
		report.rating_usability = report.rating_usability + int( usability )
		report.rating_presentation = report.rating_presentation + int( presentation )

		overall = ( ( report.rating_content + report.rating_usability + report.rating_presentation ) / 3.0 ) / report.num_raters
		report.rating_overall = overall
		report.save()

		cache_key = 'page_' + domain
		memcache.delete( cache_key )

		self.response.set_status( 200 )
		self.response.headers['Access-Control-Allow-Origin'] = self.request.headers['Origin']
		self.response.headers['Content-Type'] = 'application/json'
		self.response.write( '"true"' ) 
Example 26
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def add_comment(self, author, body):
		self.comments += 1
		id = self.key().id()
		new = Comment(parent = self, author = author, body = body, post_id = id, number = self.comments)
		new.put()
		self.put()
		memcache.set(str(id), self)
		memcache.delete(str(id)+'_comments') 
Example 27
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def post(self):
		subject = self.request.get("subject")
		content = self.request.get("content")
		if subject and content:
			p = Post(author = self.user.username, title = subject, body = content, comments = 0)
			p.put()										
			id = p.key().id()
			memcache.set(str(id), p)
			memcache.delete('all')
			self.redirect('/blog/' + str(id))
		else:
			error = "you monster"
			self.render_form(subject, content, error) 
Example 28
Project: ithz   Author: ergoithz   File: fetchrss.py    MIT License 5 votes vote down vote up
def refreshRSS():
    for i in queryRss():
        html = parsefeed(i.url)
        if html is not None:
            memcache.delete("getrss_cache/%s" % i.url)
            i.content = html
            i.put() 
Example 29
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def delete(self, key, *options):
        raise NotImplementedError("%s.del(): not implemented yet." % self.__class__.__name__) 
Example 30
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def get(self, key, original_timestamp=None):
        tupl = self.values.get(key)
        if not tupl:
            return None
        value, created_at, expires_at = tupl
        if original_timestamp is not None and created_at < original_timestamp:
            self.delete(key)
            return None
        if expires_at < _time():
            self.delete(key)
            return None
        return value 
Example 31
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def delete(self, key):
        try:
            del self.values[key]
            return True
        except KeyError:
            return False 
Example 32
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def has(self, key):
        pair = self.values.get(key)
        if not pair:
            return False
        value, created_at, expires_at = pair
        if expires_at and expires_at < _time():
            self.delete(key)
            return False
        return True


##
## file base data cache
## 
Example 33
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def has(self, key):
        fpath = self.filepath(key)
        if not _isfile(fpath):
            return False
        if _getmtime(fpath) < _time():
            self.delete(key)
            return False
        return True



##
## html fragment cache helper class
## 
Example 34
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def regenerate_id(self, expiration_ts=None):
        """Assigns the session a new session ID (data carries over).  This
        should be called whenever a user authenticates to prevent session
        fixation attacks.

        ``expiration_ts`` - The UNIX timestamp the session will expire at. If
        omitted, the session expiration time will not be changed.
        """
        if self.sid or expiration_ts is not None:
            self.ensure_data_loaded()  # ensure we have the data before we delete it
            if expiration_ts is None:
                expiration_ts = self.get_expiration()
            self.__set_sid(self.__make_sid(expiration_ts, self.is_ssl_only()))
            self.dirty = True  # ensure the data is written to the new session 
Example 35
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def __clear_data(self):
        """Deletes this session from memcache and the datastore."""
        if self.sid:
            memcache.delete(self.sid, namespace='')  # not really needed; it'll go away on its own
            try:
                db.delete(self.db_key)
            except:
                pass  # either it wasn't in the db (maybe cookie/memcache-only) or db is down => cron will expire it 
Example 36
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def delete_expired_sessions():
    """Deletes expired sessions from the datastore.
    If there are more than 500 expired sessions, only 500 will be removed.
    Returns True if all expired sessions have been removed.
    """
    now_str = unicode(int(time.time()))
    q = db.Query(SessionModel, keys_only=True, namespace='')
    key = db.Key.from_path('SessionModel', now_str + u'\ufffd', namespace='')
    q.filter('__key__ < ', key)
    results = q.fetch(500)
    db.delete(results)
    logging.info('gae-sessions: deleted %d expired sessions from the datastore' % len(results))
    return len(results) < 500 
Example 37
Project: isthislegit   Author: duo-labs   File: memcache.py    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 38
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 5 votes vote down vote up
def flush_cache(name):
   """
   Flush the cache for this counter 
   """
   memcache.delete(name) 
Example 39
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 5 votes vote down vote up
def delete(name, num_shards):
   """
   Delete the named counter and all of its shards.

   Args:
       name: The name of the counter.
       num_shards: the number of shards in the counter
   """
   
   all_keys = _all_keys(name, num_shards)
   ndb.delete_multi( all_keys )
   
   memcache.delete( name )
   
   return 
Example 40
Project: cp100-appengine-memcache-python   Author: GoogleCloudPlatformTraining   File: guestbook.py    Apache License 2.0 5 votes vote down vote up
def post(self):
        if not memcache.delete('entries'):
            logging.error("Memcache failed to delete entries")
        self.redirect('/') 
Example 41
Project: mymovie   Author: manish-pocketgems   File: main.py    MIT License 5 votes vote down vote up
def _post_put_hook(self, future):
        """Hook that runs after put()"""
        mc_key = MEMCACHE_KEY_PREFIX + str(self.movie_id)
        memcache.delete(mc_key) 
Example 42
Project: roger-api   Author: rogertalk   File: api.py    MIT License 5 votes vote down vote up
def delete_device_token(session, token):
    """
    Remove a user's device token.

    Request:
        DELETE /v1/device/abcdef
        Authorization: Bearer 5ZVhC41gMAqjpno3ELKc5pLtcAbQAPO1bWJf6hCMFW8T20s

    Response:
        {"success": true}

    """
    models.Device.delete(session.account, token)
    return {'success': True} 
Example 43
Project: roger-api   Author: rogertalk   File: admin.py    MIT License 5 votes vote down vote up
def post_clear_cache_json():
    memcache.delete(request.args.get('cache_key'))
    return '{}' 
Example 44
Project: roger-api   Author: rogertalk   File: admin.py    MIT License 5 votes vote down vote up
def post_search_evict():
    content_id = flask_extras.get_parameter('id')
    query = flask_extras.get_parameter('query')
    # TODO: Consider clearing memcache for search query.
    search.Index('original2').delete(content_id)
    return '{}' 
Example 45
Project: beibq   Author: chaijunit   File: main.py    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 46
Project: beibq   Author: chaijunit   File: main.py    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 47
Project: server   Author: viur-framework   File: db.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def Put( entities, **kwargs ):
	"""
		Store one or more entities in the data store.

		The entities may be new or previously existing. For new entities,
		``Put()`` will fill in the app id and key assigned by the data store.

		:param entities: Entity or list of entities to be stored.
		:type entities: :class:`server.db.Entity` | list of :class:`server.db.Entity`

		:param config: Optional configuration to use for this request. This must be specified\
		as a keyword argument.
		:type config: dict

		:returns: If the argument ``entities`` is a single :class:`server.db.Entity`, \
		a single Key is returned. If the argument is a list of :class:`server.db.Entity`, \
		a list of Keys will be returned.
		:rtype: Key | list of keys

		:raises: :exc:`TransactionFailedError`, if the action could not be committed.
	"""
	if isinstance( entities, Entity ):
		entities._fixUnindexedProperties()
	elif isinstance( entities, list ):
		for entity in entities:
			assert isinstance( entity, Entity )
			entity._fixUnindexedProperties()
	if conf["viur.db.caching" ]>0:
		if isinstance( entities, Entity ): #Just one:
			if entities.is_saved(): #Its an update
				memcache.delete( str( entities.key() ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
		elif isinstance( entities, list ):
			for entity in entities:
				assert isinstance( entity, Entity )
				if entity.is_saved(): #Its an update
					memcache.delete( str( entity.key() ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
	return( datastore.Put( entities, **kwargs ) ) 
Example 48
Project: server   Author: viur-framework   File: db.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def Delete(keys, **kwargs):
	"""
		Deletes one or more entities from the data store.

		:warning: Permanently deletes entities, use with care!

		Deletes the given entity or entities from the data store. You can only delete
		entities from your app. If there is an error, the function raises a
		subclass of :exc:`datastore_errors.Error`.

		:param keys: Key, str or list of keys or strings to be deleted.
		:type keys: Key | str | list of Key | list of str

		:param config: Optional configuration to use for this request. This must be specified\
		as a keyword argument.
		:type config: dict

		:raises: :exc:`TransactionFailedError`, if the deletion could not be committed.
	"""
	if conf["viur.db.caching" ]>0:
		if isinstance( keys, datastore_types.Key ) or isinstance( keys, basestring ): #Just one:
			memcache.delete( str( keys ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
		elif isinstance( keys, list ):
			for key in keys:
				assert isinstance( key, datastore_types.Key ) or isinstance( key, basestring )
				memcache.delete( str( key ), namespace=__CacheKeyPrefix__, seconds=__cacheLockTime__  )
	return( datastore.Delete( keys, **kwargs ) ) 
Example 49
Project: oscars2016   Author: 0x0ece   File: query_cache.py    Apache License 2.0 5 votes vote down vote up
def purge_keys(keys):
    for k in keys:
        memcache.delete(MC_GUARD%k)
        memcache.delete(MC_KEY%k) 
Example 50
Project: pledgeservice   Author: Lessig2016   File: cache.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def ClearShardedCounterTotal(name):
  memcache.delete(_COUNTER_TOTAL.format(name)) 
Example 51
Project: python-docs-samples   Author: GoogleCloudPlatform   File: failure.py    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 52
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets_test.py    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 53
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets_test.py    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 54
Project: deb-python-oauth2client   Author: openstack   File: test_appengine.py    Apache License 2.0 5 votes vote down vote up
def test_get_and_put_cached(self):
        storage = appengine.StorageByKeyName(
            appengine.CredentialsModel, 'foo', 'credentials', cache=memcache)

        self.assertEqual(None, storage.get())
        self.credentials.set_store(storage)

        http = http_mock.HttpMock(data=BASIC_RESP)
        self.credentials._refresh(http)
        credmodel = appengine.CredentialsModel.get_by_key_name('foo')
        self.assertEqual(BASIC_TOKEN, credmodel.credentials.access_token)

        # Now remove the item from the cache.
        memcache.delete('foo')

        # Check that getting refreshes the cache.
        credentials = storage.get()
        self.assertEqual(BASIC_TOKEN, credentials.access_token)
        self.assertNotEqual(None, memcache.get('foo'))

        # Deleting should clear the cache.
        storage.delete()
        credentials = storage.get()
        self.assertEqual(None, credentials)
        self.assertEqual(None, memcache.get('foo'))

        # Verify mock.
        self._verify_basic_refresh(http) 
Example 55
Project: deb-python-oauth2client   Author: openstack   File: test_appengine.py    Apache License 2.0 5 votes vote down vote up
def test_storage_delete(self, new_http):
        new_http.return_value = http_mock.HttpMock(data=DEFAULT_RESP)
        # An initial request to an oauth_required decorated path should be a
        # redirect to start the OAuth dance.
        response = self.app.get('/foo_path')
        self.assertTrue(response.status.startswith('302'))

        with mock.patch.object(appengine, '_parse_state_value',
                               return_value='foo_path',
                               autospec=True) as parse_state_value:
            # Now simulate the callback to /oauth2callback.
            response = self.app.get('/oauth2callback', {
                'code': 'foo_access_code',
                'state': 'foo_path:xsrfkey123',
            })
            self.assertEqual('http://localhost/foo_path',
                             response.headers['Location'])
            self.assertEqual(None, self.decorator.credentials)

            # Now requesting the decorated path should work.
            response = self.app.get('/foo_path')

            self.assertTrue(self.had_credentials)

            # Credentials should be cleared after each call.
            self.assertEqual(None, self.decorator.credentials)

            # Invalidate the stored Credentials.
            self.found_credentials.store.delete()

            # Invalid Credentials should start the OAuth dance again.
            response = self.app.get('/foo_path')
            self.assertTrue(response.status.startswith('302'))

            parse_state_value.assert_called_once_with(
                'foo_path:xsrfkey123', self.current_user)

        # Check the mocks were called.
        new_http.assert_called_once_with() 
Example 56
Project: andris-projeto   Author: andris210296   File: commands.py    MIT License 5 votes vote down vote up
def do_business(self):
        super(SaveOrUpdateFacebookApp, self).do_business()
        if not self.result:
            self.result = FacebookApp()
        else:
            memcache.delete(self._cache_key())
        if self.id:
            self.result.app_id = self.id
        if self.token:
            self.result.token = self.token 
Example 57
Project: andris-projeto   Author: andris210296   File: commands.py    MIT License 5 votes vote down vote up
def do_business(self):
        super(SaveOrUpdateApp, self).do_business()
        if not self.result:
            self.result = PasswordlessApp()
        else:
            memcache.delete(self._cache_key())
        if self.id:
            self.result.app_id = self.id
        if self.token:
            self.result.token = self.token 
Example 58
Project: andris-projeto   Author: andris210296   File: commands.py    MIT License 5 votes vote down vote up
def do_business(self):
        super(SaveOrUpdateFacebookApp, self).do_business()
        if not self.result:
            self.result = FacebookApp()
        else:
            memcache.delete(self._cache_key())
        if self.id:
            self.result.app_id = self.id
        if self.token:
            self.result.token = self.token 
Example 59
Project: andris-projeto   Author: andris210296   File: commands.py    MIT License 5 votes vote down vote up
def do_business(self):
        super(SaveOrUpdateApp, self).do_business()
        if not self.result:
            self.result = PasswordlessApp()
        else:
            memcache.delete(self._cache_key())
        if self.id:
            self.result.app_id = self.id
        if self.token:
            self.result.token = self.token 
Example 60
Project: luci-py   Author: luci   File: task_to_run.py    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 61
Project: ifttt-line   Author: MypaceEngine   File: userinfo_utility.py    Apache License 2.0 5 votes vote down vote up
def deleteUserData(id):
    # memcache.delete(key="MakerSecret-"+id)
    # memcache.delete(key="USER-"+id)
    # dashid=memcache.get(key="User-dash-"+id)
    # memcache.delete(key = "User-dash-"+id)
    # memcache.delete(key = "Dash-user-"+str(dashid))

    try:
        key = Key.from_path('UserData', id)
        entity = datastore.Delete(key)
    except:
        logging.debug(id + u"は登録されていません。")

### Private methods 
Example 62
Project: ifttt-line   Author: MypaceEngine   File: userinfo_utility.py    Apache License 2.0 5 votes vote down vote up
def _clearCurrentUser():
    return memcache.delete(key="CURRENTUSR")


### Tests 
Example 63
Project: html5-to-dfp   Author: googleads   File: dfp_utils.py    Apache License 2.0 5 votes vote down vote up
def purge(self, id):
    return memcache.delete('wsdl_%s' % id) 
Example 64
Project: vishnu   Author: anomaly   File: google_app_engine_memcache.py    Apache License 2.0 5 votes vote down vote up
def clear(self):
        super(Client, self).clear()
        if self._sid:
            memcache.delete(self._sid, namespace=NAMESPACE) 
Example 65
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: memcache_stub.py    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 66
Project: python-for-android   Author: kuri65536   File: app_engine.py    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 67
Project: python-for-android   Author: kuri65536   File: app_engine.py    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 68
Project: python-for-android   Author: kuri65536   File: app_engine.py    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 69
Project: python-for-android   Author: kuri65536   File: app_engine.py    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 70
Project: python-for-android   Author: kuri65536   File: app_engine.py    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 71
Project: python-for-android   Author: kuri65536   File: sessions.py    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 72
Project: python-for-android   Author: kuri65536   File: sessions.py    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() 
Example 73
Project: python-for-android   Author: kuri65536   File: sessions.py    Apache License 2.0 5 votes vote down vote up
def _delete_session(self):
        """
        Delete the session and all session data.
        """
        if hasattr(self, "session"):
            self.session.delete()
        self.cookie_vals = {}
        self.cache = {}
        self.output_cookie[self.cookie_name + '_data'] = \
            simplejson.dumps(self.cookie_vals)
        print self.output_cookie.output()
        """
        OLD
        if hasattr(self, "session"):
            sessiondata = self._get()
            # delete from datastore
            if sessiondata is not None:
                for sd in sessiondata:
                    sd.delete()
            # delete from memcache
            memcache.delete('sid-'+str(self.session.key()))
            # delete the session now that all items that reference it are deleted.
            self.session.delete()
        # unset any cookie values that may exist
        self.cookie_vals = {}
        self.cache = {}
        self.output_cookie[self.cookie_name + '_data'] = \
            simplejson.dumps(self.cookie_vals)
        print self.output_cookie.output()
        """
        # if the event class has been loaded, fire off the sessionDeleted event
        if 'AEU_Events' in __main__.__dict__:
            __main__.AEU_Events.fire_event('sessionDelete') 
Example 74
Project: python-for-android   Author: kuri65536   File: sessions.py    Apache License 2.0 5 votes vote down vote up
def delete(self):
        """
        Delete the current session and start a new one.

        This is useful for when you need to get rid of all data tied to a
        current session, such as when you are logging out a user.
        """
        self._delete_session() 
Example 75
Project: analyzer-website   Author: santiagolizardo   File: scores.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def get( self ):
		domainUrl = self.request.get( 'domainUrl' )
		channelId = self.request.cookies.get( 'channelId' )
		
		tasks = library.task.manager.findAll()
	
		data = {
			'pageTitle': None,
			'pageDescription': None,
		}
		actions = []

		for task in tasks:
			report = task.getSavedReport( domainUrl )
			task.suggest_actions( actions, report, domainUrl )

			html_node = task.generate_html_node( report )
			if html_node is not None:
				data[ task.getName() ] = html_node 

		score = 100
		totalVariables = len( actions ) # eg 12
		scorePerVariable = 100 / totalVariables # eg: 8.33
		
		for action in actions:
			if action['status'] == 'good':
				pass
			elif action['status'] == 'regular':
				score -= scorePerVariable/2
			elif action['status'] == 'bad':
				score -= scorePerVariable
		
		siteReport = SiteReport()
		siteReport.score = score
		siteReport.name = data['pageTitle']
		siteReport.description = data['pageDescription']
		siteReport.url = domainUrl
		siteReport.put()
		
		message = {
			'name': 'score',
			'content': str( score ) + '/100',
			'actions': [],
		}
		messageEncoded = json.dumps( message )
				
		memcache.delete( 'page-index' )

		send_message( channelId, messageEncoded )

                if not debug_active:
 		    send_twitter_update(self.current_instance['domain'], domainUrl) 
Example 76
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 4 votes vote down vote up
def _change_async(name, num_shards, value, do_transaction=True, use_memcache=True):
    """
    Asynchronous transaction helper to increment the value for a given sharded counter.

    Also takes a number of shards to determine which shard will be used.

    Args:
        name: The name of the counter.
        num_shards: How many shards to use.
        
    """
    
    logging.info("Shard: change {} by {}".format(name, value))

    @ndb.tasklet
    def txn():
      
      index = random.randint(0, num_shards - 1)
      
      shard_key_string = SHARD_KEY_TEMPLATE.format(name, index)
      
      counter = yield GeneralCounterShard.get_by_id_async(shard_key_string)
      if counter is None:
         counter = GeneralCounterShard(id=shard_key_string)
         
      counter.count += value
      yield counter.put_async()
      
      if use_memcache:
         if value > 0:
            memcache.incr( name, delta=value )
         elif value < 0:
            memcache.decr( name, delta=-value )

      else:
         memcache.delete( name )
      
      raise ndb.Return( True )
   
    if do_transaction:
      return ndb.transaction_async( txn )
   
    else:
      return txn() 
Example 77
Project: roger-api   Author: rogertalk   File: admin.py    MIT License 4 votes vote down vote up
def get_update_content(content_id=None):
    content = models.ExportedContent.get_by_id(content_id) if content_id else None
    if not content:
        return render_template('admin_update_content.html', content=None)
    stream, chunk = ndb.get_multi([content.chunk.parent(), content.chunk])
    entities = {}
    def mark_chunk_for_update():
        entities[chunk.key] = chunk
        entities[content.key] = content
        for i, c in enumerate(stream.chunks):
            if c.chunk_id == chunk.key.id():
                stream.chunks[i] = models.ChunkInStream.from_chunk(chunk)
                entities[stream.key] = stream
                break
    delete_attachment_url = flask_extras.get_parameter('delete-attachment')
    if delete_attachment_url:
        without_deleted_attachment = lambda a: a.url != delete_attachment_url
        chunk.attachments = filter(without_deleted_attachment, chunk.attachments)
        content.attachments = chunk.attachments
        mark_chunk_for_update()
    file = request.files.get('attachment-file')
    url = flask_extras.get_parameter('attachment-url')
    if file or url:
        title = flask_extras.get_parameter('attachment-title')
        if file:
            if not title:
                title = file.filename
            path = files.upload(file.filename, file.stream, persist=True)
            url = files.storage_url(path)
        attachment = models.ChunkAttachment(title=title or url, url=url)
        # Add attachment to Chunk and ExportedContent instances.
        chunk.attachments.append(attachment)
        content.attachments = chunk.attachments
        mark_chunk_for_update()
    texts = flask_extras.get_parameter_list('sub-text')
    if texts:
        starts = flask_extras.get_parameter_list('sub-start')
        durations = flask_extras.get_parameter_list('sub-duration')
        segments = [{'text': t, 'start': int(s), 'duration': int(d)}
                    for t, s, d in zip(texts, starts, durations)
                    if t]
        if segments != content.properties['text']:
            content.properties['text'] = segments
            chunk.text_segments = [models.TextSegment(**d) for d in segments]
            mark_chunk_for_update()
    title = flask_extras.get_parameter('title')
    if title and title != content.properties.get('title'):
        content.properties['title'] = title
        entities[content.key] = content
    if entities:
        ndb.put_multi(entities.values())
    return render_template('admin_update_content.html', content=content,
                           stream=streams.Stream(stream), chunk=chunk) 
Example 78
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: memcache_viewer.py    Apache License 2.0 4 votes vote down vote up
def post(self):
    """Handle modifying actions and/or redirect to GET page."""
    super(MemcacheViewerRequestHandler, self).post()
    next_param = {}

    if self.request.get('action:flush'):
      if memcache.flush_all():
        next_param['message'] = 'Cache flushed, all keys dropped.'
      else:
        next_param['message'] = 'Flushing the cache failed.  Please try again.'

    elif self.request.get('action:display'):
      next_param['key'] = self.request.get('key')

    elif self.request.get('action:edit'):
      next_param['edit'] = self.request.get('key')

    elif self.request.get('action:delete'):
      key = self.request.get('key')
      result = memcache.delete(key)
      if result == memcache.DELETE_NETWORK_FAILURE:
        next_param['message'] = ('ERROR: Network failure, key "%s" not deleted.'
                                 % key)
      elif result == memcache.DELETE_ITEM_MISSING:
        next_param['message'] = 'Key "%s" not in cache.' % key
      elif result == memcache.DELETE_SUCCESSFUL:
        next_param['message'] = 'Key "%s" deleted.' % key
      else:
        next_param['message'] = ('Unknown return value.  Key "%s" might still '
                                 'exist.' % key)

    elif self.request.get('action:save'):
      key = self.request.get('key')
      value = self.request.get('value')
      type_ = self.request.get('type')
      next_param['key'] = key

      converter = self.FRIENDLY_TYPE_NAME_TO_CONVERTER[type_]
      try:
        memcache_value = converter.to_cache(value)
      except ValueError as e:
        next_param['message'] = 'ERROR: Failed to save key "%s": %s.' % (key, e)
      else:
        if self._set_memcache_value(key,
                                    memcache_value,
                                    converter.memcache_type):
          next_param['message'] = 'Key "%s" saved.' % key
        else:
          next_param['message'] = 'ERROR: Failed to save key "%s".' % key
    elif self.request.get('action:cancel'):
      next_param['key'] = self.request.get('key')
    else:
      next_param['message'] = 'Unknown action.'

    next = self.request.path_url
    if next_param:
      next = '%s?%s' % (next, self._urlencode(next_param))
    self.redirect(next) 
Example 79
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: __init__.py    Apache License 2.0 4 votes vote down vote up
def post(self):
    """Handle modifying actions and/or redirect to GET page."""
    next_param = {}

    if self.request.get('action:flush'):
      if memcache.flush_all():
        next_param['message'] = 'Cache flushed, all keys dropped.'
      else:
        next_param['message'] = 'Flushing the cache failed.  Please try again.'

    elif self.request.get('action:display'):
      next_param['key'] = self.request.get('key')

    elif self.request.get('action:edit'):
      next_param['edit'] = self.request.get('key')

    elif self.request.get('action:delete'):
      key = self.request.get('key')
      result = memcache.delete(key)
      if result == memcache.DELETE_NETWORK_FAILURE:
        next_param['message'] = ('ERROR: Network failure, key "%s" not deleted.'
                                 % key)
      elif result == memcache.DELETE_ITEM_MISSING:
        next_param['message'] = 'Key "%s" not in cache.' % key
      elif result == memcache.DELETE_SUCCESSFUL:
        next_param['message'] = 'Key "%s" deleted.' % key
      else:
        next_param['message'] = ('Unknown return value.  Key "%s" might still '
                                 'exist.' % key)

    elif self.request.get('action:save'):
      key = self.request.get('key')
      value = self.request.get('value')
      type_ = self.request.get('type')
      next_param['key'] = key
      try:
        if self._SetValue(key, type_, value):
          next_param['message'] = 'Key "%s" saved.' % key
        else:
          next_param['message'] = 'ERROR: Failed to save key "%s".' % key
      except ValueError, e:
        next_param['message'] = 'ERROR: Unable to encode value: %s' % e 
Example 80
Project: python-for-android   Author: kuri65536   File: sessions.py    Apache License 2.0 4 votes vote down vote up
def get_session(cls, session_obj=None):
        """
        Uses the passed sid to get a session object from memcache, or datastore
        if a valid one exists.
        """
        if session_obj.sid == None:
            return None
        session_key = session_obj.sid.split('_')[0]
        session = memcache.get("_AppEngineUtilities_Session_" + str(session_key))
        if session:
            if session.deleted == True:
                session.delete()
                return None
            if session.dirty == True and session.working != False:
                # the working bit is used to make sure multiple requests, which can happen
                # with ajax oriented sites, don't try to put at the same time
                session.working = True
                memcache.set("_AppEngineUtilities_Session_" + str(session_key), session)
                session.put()
            if session_obj.sid in session.sid:
                logging.info('grabbed session from memcache')
                sessionAge = datetime.datetime.now() - session.last_activity
                if sessionAge.seconds > session_obj.session_expire_time:
                    session.delete()
                    return None
                return session
            else:
                return None
 
        # Not in memcache, check datastore
        query = _AppEngineUtilities_Session.all()
        query.filter("sid = ", session_obj.sid)
        results = query.fetch(1)
        if len(results) > 0:
            sessionAge = datetime.datetime.now() - results[0].last_activity
            if sessionAge.seconds > self.session_expire_time:
                results[0].delete()
                return None
            memcache.set("_AppEngineUtilities_Session_" + str(session_key), results[0])
            memcache.set("_AppEngineUtilities_SessionData_" + str(session_key), results[0].get_items_ds())
            logging.info('grabbed session from datastore')
            return results[0]
        else:
            return None