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

The following are code examples for showing how to use google.appengine.api.memcache.set(). 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: gae-bbs   Author: ego008   File: view.py    Apache License 2.0 6 votes vote down vote up
def post(self):
        if self.cur_user and self.cur_user.flag==99:
            m_name = self.request.get('name')
            flag = self.request.get('flag')
            m_obj = None
            if m_name and flag:
                m_obj = Member.get_by_key_name(m_name)
                if m_obj:
                    m_obj.flag = int(flag)
                    m_obj.put()
                else:
                    self.redirect('/set-user-flag')
                    return
            
            self.redirect('/set-user-flag?name=%s'%str(m_name))
        else:
            self.error(403)
            self.write('403:forbidden') 
Example 2
Project: gae-bbs   Author: ego008   File: common.py    Apache License 2.0 6 votes vote down vote up
def memcached(key, cache_time, key_suffix_calc_func=None, namespace=None):
    def wrap(func):
        def cached_func(*args, **kw):
            key_with_suffix = key

            if key_suffix_calc_func:
                key_suffix = key_suffix_calc_func(*args, **kw)
                if key_suffix:
                    key_with_suffix = '%s:%s' % (key, key_suffix)

            value = memcache.get(key_with_suffix, namespace)
            if not value:
                value = func(*args, **kw)
                memcache.set(key_with_suffix, value, cache_time, namespace)
            return value
        return cached_func
    return wrap
    
### 
Example 3
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 6 votes vote down vote up
def __retrieve_data(self):
        """Sets the data associated with this session after retrieving it from
        memcache or the datastore.  Assumes self.sid is set.  Checks for session
        expiration after getting the data."""
        pdump = memcache.get(self.sid, namespace='')
        if pdump is None:
            # memcache lost it, go to the datastore
            if self.no_datastore:
                logging.info("can't find session data in memcache for sid=%s (using memcache only sessions)" % self.sid)
                self.terminate(False)  # we lost it; just kill the session
                return
            session_model_instance = db.get(self.db_key)
            if session_model_instance:
                pdump = session_model_instance.pdump
            else:
                logging.error("can't find session data in the datastore for sid=%s" % self.sid)
                self.terminate(False)  # we lost it; just kill the session
                return
        self.data = self.__decode_data(pdump) 
Example 4
Project: mlab-ns   Author: m-lab   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def locked_get(self):
    """Retrieve Credential from datastore.

    Returns:
      oauth2client.Credentials
    """
    if self._cache:
      json = self._cache.get(self._key_name)
      if json:
        return Credentials.new_from_json(json)

    credential = None
    entity = self._model.get_by_key_name(self._key_name)
    if entity is not None:
      credential = getattr(entity, self._property_name)
      if credential and hasattr(credential, 'set_store'):
        credential.set_store(self)
        if self._cache:
          self._cache.set(self._key_name, credentials.to_json())

    return credential 
Example 5
Project: mlab-ns   Author: m-lab   File: prometheus_config_wrapper.py    Apache License 2.0 6 votes vote down vote up
def get_prometheus_config():
    """Retrieves Prometheus config info. First checks memcache, then datastore.

    Returns:
        Prometheus model instance
    """
    prometheus = memcache.get(constants.DEFAULT_PROMETHEUS_ENTRY)
    if not prometheus:
        prometheus = model.Prometheus.get_by_key_name(
            constants.DEFAULT_PROMETHEUS_ENTRY)
        if prometheus:
            memcache.set(constants.DEFAULT_PROMETHEUS_ENTRY, prometheus)
        else:
            logging.error('Datastore does not have the Prometheus credentials.')

    return prometheus 
Example 6
Project: mlab-ns   Author: m-lab   File: test_sliver_tool_fetcher.py    Apache License 2.0 6 votes vote down vote up
def testRoundRobin(self):
        self.initToolSetForRoundRobin()
        tool_properties = sliver_tool_fetcher.ToolProperties(tool_id='rr_tool')
        rr_counter = [0, 0, 0]
        for i in range(1, 10000):
            tool = self.fetcher.fetch(tool_properties)
            self.assertEqual(1, len(tool))
            if tool[0].server_id == "mlab1":
                rr_counter[0] += 1
            if tool[0].server_id == "mlab2":
                rr_counter[1] += 1
            if tool[0].server_id == "mlab3":
                rr_counter[2] += 1
        # Ideally each server should have count around 3333.
        # The bar of the test was set for 10% to allow for variation
        # caused by randomness of site selection.
        self.assertGreater(rr_counter[0], 3000)
        self.assertGreater(rr_counter[1], 3000)
        self.assertGreater(rr_counter[2], 3000) 
Example 7
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 8
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 6 votes vote down vote up
def getConferencesToAttend(self, request):
        """Get list of conferences that user has registered for."""
        prof = self._getProfileFromUser() # get user Profile
        conf_keys = [ndb.Key(urlsafe=wsck) for wsck in prof.conferenceKeysToAttend]
        conferences = ndb.get_multi(conf_keys)

        # get organizers
        organisers = [ndb.Key(Profile, conf.organizerUserId) for conf in conferences]
        profiles = ndb.get_multi(organisers)

        # put display names in a dict for easier fetching
        names = {}
        for profile in profiles:
            names[profile.key.id()] = profile.displayName

        # return set of ConferenceForm objects per Conference
        return ConferenceForms(items=[self._copyConferenceToForm(conf, names[conf.organizerUserId])\
         for conf in conferences]
        ) 
Example 9
Project: isthislegit   Author: duo-labs   File: email.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def update(cls, domain, time=0):
        """
        Updates the memcached stats for a given domain

        This is used when a report is updated so that memcached
        has the current stats.

        Args:
            domain - str - The domain to use for the namespace
            time - int - The timeout for stored keys (default: 5 seconds)
        """
        namespace = "{}|{}".format('stats', domain)

        for status in VALID_STATUSES:
            count = EmailReport.query(EmailReport.reported_domain == domain,
                                      EmailReport.status == status).count()
            memcache.set(
                key=status, namespace=namespace, value=count, time=time) 
Example 10
Project: isthislegit   Author: duo-labs   File: email.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def update(cls, domain, time=0):
        """
        Updates the memcached stats for a given domain

        This is used when a report is updated so that memcached
        has the current stats.

        Args:
            domain - str - The domain to use for the namespace
            time - int - The timeout for stored keys (default: 5 seconds)
        """
        namespace = "{}|".format(domain)
        records = cls._get_from_datastore(domain, cls._memcache_result_count)
        memcache.set(
            key=cls._memcache_key,
            namespace=namespace,
            value=json.dumps(records),
            time=time) 
Example 11
Project: isthislegit   Author: duo-labs   File: timeline.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _update_memcached(cls, domain, time=3600 * 24, records=None):
        """
        Updates memcached with the latest data from the datastore
        and returns that data. By default stores entries to expire after
        24 hours.
        """
        namespace = "{}|".format(domain)
        if not records:
            records = cls._get_from_datastore(domain,
                                              cls._memcache_date_offset)
        memcache.set(
            key=cls._memcache_key,
            namespace=namespace,
            value=json.dumps(records),
            time=time)
        return records 
Example 12
Project: blockhooks   Author: EthereumWebhooks   File: sessions_ndb.py    Apache License 2.0 6 votes vote down vote up
def get_by_sid(cls, sid):
        """Returns a ``Session`` instance by session id.

        :param sid:
            A session id.
        :returns:
            An existing ``Session`` entity.
        """
        data = memcache.get(sid)
        if not data:
            session = model.Key(cls, sid).get()
            if session:
                data = session.data
                memcache.set(sid, data)

        return data 
Example 13
Project: Utmost_Bot   Author: jiachen247   File: utmost.py    GNU General Public License v3.0 5 votes vote down vote up
def __init(self):
        # date object storing date for
        self.date = None
        self.heading = None
        self.verse_reference = None
        self.bible_in_a_year = None
        self.verse_concise = None
        self.verse_full = None
        self.post = None

        # write get and set

        # link to get full verse
        self.link_to_full_verse_bgw = None
        self.link_to_full_verse_yv = None 
Example 14
Project: hackernewsbot   Author: phil-r   File: database.py    MIT License 5 votes vote down vote up
def add_memcache(self):
    memcache.set(self.key.id(), self.url) 
Example 15
Project: loaner   Author: google   File: template_model.py    Apache License 2.0 5 votes vote down vote up
def _cache_template(self, template):
    """Caches the title and body of a Template separately in memcache."""
    memcache.set(_CACHED_TITLE_NAME % template.name, template.title)
    memcache.set(_CACHED_BODY_NAME % template.name, template.body) 
Example 16
Project: loaner   Author: google   File: config_model_test.py    Apache License 2.0 5 votes vote down vote up
def test_get_from_memcache(self):
    config = 'string_config'
    config_value = 'this should be read.'
    memcache.set(config, config_value)
    reference_datastore_config = config_model.Config.get_by_id(config)
    config_memcache = config_model.Config.get(config)

    self.assertEqual(config_memcache, config_value)
    self.assertEqual(reference_datastore_config.string_value, 'config value 1') 
Example 17
Project: loaner   Author: google   File: config_model_test.py    Apache License 2.0 5 votes vote down vote up
def test_get_identifier_with_use_asset(self):
    config_model.Config.set('use_asset_tags', True)
    config_datastore = config_model.Config.get('device_identifier_mode')
    self.assertEqual(config_datastore,
                     config_model.DeviceIdentifierMode.BOTH_REQUIRED) 
Example 18
Project: loaner   Author: google   File: config_model_test.py    Apache License 2.0 5 votes vote down vote up
def test_set(self, test_config):
    config_model.Config.set(test_config[0], test_config[1])
    memcache_config = memcache.get(test_config[0])
    config = config_model.Config.get(test_config[0])

    self.assertEqual(memcache_config, test_config[1])
    self.assertEqual(config, test_config[1]) 
Example 19
Project: loaner   Author: google   File: config_model_test.py    Apache License 2.0 5 votes vote down vote up
def test_set_nonexistent(self):
    with self.assertRaisesRegexp(KeyError,
                                 config_model._CONFIG_NOT_FOUND_MSG % 'fake'):
      config_model.Config.set('fake', 'does_not_exist') 
Example 20
Project: loaner   Author: google   File: config_model.py    Apache License 2.0 5 votes vote down vote up
def set(cls, name, value, validate=True):
    """Stores values for a config name in memcache and datastore.

    Args:
      name: str, name of the config setting.
      value: str, int, bool, list value to set or change config setting.
      validate: bool, checks keys against config_defaults if enabled.
    Raises:
      KeyError: Error raised when name does not exist in config.py file.
    """
    if validate:
      config_defaults = utils.load_config_from_yaml()
      if name not in config_defaults:
        raise KeyError(_CONFIG_NOT_FOUND_MSG % name)

    if isinstance(value, basestring):
      stored_config = cls.get_or_insert(name)
      stored_config.string_value = value
      stored_config.put()
    if isinstance(value, bool) and isinstance(value, int):
      stored_config = cls.get_or_insert(name)
      stored_config.bool_value = value
      stored_config.put()
    if isinstance(value, int) and not isinstance(value, bool):
      stored_config = cls.get_or_insert(name)
      stored_config.integer_value = value
      stored_config.put()
    if isinstance(value, list):
      stored_config = cls.get_or_insert(name)
      stored_config.list_value = value
      stored_config.put()

    memcache.set(name, value) 
Example 21
Project: analyzer-website   Author: santiagolizardo   File: index.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get( self ):
		html = memcache.get( 'page-index' )
		if html is None:
			html = self.generate_html()
			memcache.set( 'page-index', html, time = 3600 )

		self.writeResponse( html ) 
Example 22
Project: analyzer-website   Author: santiagolizardo   File: staticReport.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get( self, domainUrl ):
		cache_key = 'page_' + domainUrl
		pageHtml = memcache.get( cache_key )
		if pageHtml is None:
			pageHtml = self.generate_static_report( domainUrl )
			memcache.set( key = cache_key, value = pageHtml, time = 86400 )

		self.writeResponse( pageHtml ) 
Example 23
Project: bbq   Author: ocadotechnology   File: cached.py    Apache License 2.0 5 votes vote down vote up
def cached(time=1200):
    """
    Decorator that caches the result of a method for the specified time in
    seconds.

    IMPORTANT: If function returns None value, result will not be cached.
    In that case decorated function will be called each time,
    unless return something else than None.

    WARNING: this uses name of the function and passed parameters as a cache
    key.
    'self' param and another ones depends on given __str__ implementations.
    Be aware if you annotate functions used in different context,
    ensure that both of them generate different key.


    Usage:
      @cached(time=1200)
      def functionToCache(arguments):
        ...

    """

    def decorator(function):
        @functools.wraps(function)
        def wrapper(*args, **kwargs):
            key = '%s%s%s' % (function.__name__, str(args), str(kwargs))
            value = memcache.get(key)
            logging.debug('Memcache: key %s %s found', key,
                          'not' if value is None else '')
            if value is None:
                value = function(*args, **kwargs)
                memcache.set(key, value, time=time)
            return value

        return wrapper

    return decorator 
Example 24
Project: pyaedj   Author: google   File: cache.py    Apache License 2.0 5 votes vote down vote up
def memcache_set(key, value, ttl=DEFAULT_CACHE_TTL_SECS):
  value = copy.deepcopy(value)
  size = sys.getsizeof(value)
  if size < MEMCACHE_MAX:
    memcache.set(key, value, ttl, namespace=config.ENV.namespace) 
Example 25
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 26
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def get(self):
		posts = memcache.get('all')
		if posts is None:
			logging.error("DB QUERY")
			posts = db.GqlQuery("SELECT * FROM Post ORDER BY created DESC")
			memcache.set('all', posts)
			memcache.set('last_queried', time())
		if self.request.url.endswith('.json'):
			self.response.headers['Content-Type'] = 'application/json'
			self.write(json.dumps([{'subject':post.title, 'content':post.body, 'created':strftime('%A, %B %c',post.created.timetuple())}
				for post in posts], {'Queried %s seconds ago.' % age}))
		else:
			self.render_html("front_template.html", posts = posts) 
Example 27
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def get_post(id):
	post = memcache.get(id)
	if post is None:
		logging.error("DB QUERY")
		key = db.Key.from_path('Post', int(id))
		post = db.get(key) 
		memcache.set(id, post)
		memcache.set('last_queried', time())
	return post
			

#id is passed from the RE that recognizes the url 
Example 28
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def get(self, id):
		post = get_post(id)
		comments = memcache.get(id + '_comments')
		if post:
			if comments is None:
				logging.error('COMMENT QUERY')
				comments = db.GqlQuery('select * from Comment where ancestor is :1 order by created asc', post)
				memcache.set(id + '_comments', comments)
			if self.request.url.endswith('.json'):
				self.response.headers['Content-Type'] = 'application/json'
				self.write(json.dumps({'subject':post.title, 'content':post.body, 'created':strftime('%A, %B %c',post.created.timetuple())}))
			else:
				self.render_html("permalink_template.html", post = post, comments = comments)
		else:
			self.render_html('404.html') 
Example 29
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 30
Project: ithz   Author: ergoithz   File: utils.py    MIT License 5 votes vote down vote up
def __memput__(self):
        memcache.set(self.id,self.__vars) 
Example 31
Project: ithz   Author: ergoithz   File: utils.py    MIT License 5 votes vote down vote up
def set_page(self, rel, section, level, page):
        memcache.set(self.__id(rel, section, level), page) 
Example 32
Project: ithz   Author: ergoithz   File: data.py    MIT License 5 votes vote down vote up
def __getitem__(self,y):  
        ''' Gets a counter with given id. Counter[ str(id) ] '''
        v = memcache.get(y,namespace=self.ns)
        if v is None:
            qu = self.__gfm(y)
            if qu:
                v = qu.count
            else:
                v = 0
            memcache.set(y,v,namespace=self.ns) 
        return v 
Example 33
Project: ithz   Author: ergoithz   File: data.py    MIT License 5 votes vote down vote up
def __setitem__(self,i,j):
        ''' Set a counter with given id. Counter[ str(id) ] = int(j) '''     
        if type(j) in (int,long):
            qu = self.__gfm(i)
            if qu:
                qu.count = j
            else:
                qu = self.__com(i,j)
            qu.put()
            memcache.set(i,j,namespace=self.ns) 
Example 34
Project: ithz   Author: ergoithz   File: fetchrss.py    MIT License 5 votes vote down vote up
def getRSS(url):
    id = "getrss_cache/%s" % url
    a = memcache.get(id)
    if not a:
        #b = queryRSS(url)
        b = False
        if b:
            a = b.content
        else:
            a = parsefeed(url)
            if not a:
                return None
        memcache.set(id, a, 43200) # 43200 seconds is 12 hours
    return a 
Example 35
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def set(self, cachepath, template):
        """set template object and save template attributes into cache file."""
        self.items[cachepath] = template
        dct = self._save_data_of(template)
        return self._store(cachepath, dct) 
Example 36
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def set(self, key, value, *options):
        raise NotImplementedError("%s.set(): not implemented yet." % self.__class__.__name__) 
Example 37
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def set(self, key, value, lifetime=0):
        created_at = _time()
        expires_at = lifetime and created_at + lifetime or 0
        self.values[key] = (value, created_at, expires_at)
        return True 
Example 38
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def set(self, key, value, lifetime=0):
        fpath = self.filepath(key)
        dirname = os.path.dirname(fpath)
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        now = _time()
        if isinstance(value, _unicode):
            value = value.encode(self.encoding or 'utf-8')
        _write_binary_file(fpath, value)
        expires_at = now + (lifetime or self.lifetime)  # timestamp
        os.utime(fpath, (expires_at, expires_at))
        return True 
Example 39
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def cache_as(self, cache_key, lifetime=None):
        key = self.prefix and self.prefix + cache_key or cache_key
        _buf = sys._getframe(1).f_locals['_buf']
        value = self.store.get(key)
        if value:
            if logger: logger.debug('[tenjin.cache_as] %r: cache found.' % (cache_key, ))
            _buf.append(value)
        else:
            if logger: logger.debug('[tenjin.cache_as] %r: expired or not cached yet.' % (cache_key, ))
            _buf_len = len(_buf)
            yield None
            value = ''.join(_buf[_buf_len:])
            self.store.set(key, value, lifetime)

## you can change default store by 'tenjin.helpers.fragment_cache.store = ...' 
Example 40
Project: gae-bbs   Author: ego008   File: common.py    Apache License 2.0 5 votes vote down vote up
def call_member(text):
    members = _re_call.findall(text)
    if len(members) > 0:
        return set([member for member in members])
    else:
        return None

# autolink 
Example 41
Project: gae-bbs   Author: ego008   File: common.py    Apache License 2.0 5 votes vote down vote up
def findall_mentions(text, filter_name=None):
    if '@' in text:
        ns = set([yk for yk in _re_mentions.findall(text.lower())])
        if filter_name:
            ns.discard(filter_name)
        if len(ns)<=NOTIFY_MEMBER_NUM:
            return ns
        else:
            return None
    else:
        return None 
Example 42
Project: bitrise-steps-google-drive-upload   Author: GetintheLoop   File: appengine_memcache.py    MIT License 5 votes vote down vote up
def set(self, url, content):
    try:
      memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE)
    except Exception as e:
      logging.warning(e, exc_info=True) 
Example 43
Project: kay-template   Author: yosukesuzuki   File: decorators.py    MIT License 5 votes vote down vote up
def __init__(self, decorator, func):
    update_wrapper(self, func)
    # NB: update the __dict__ first, *then* set
    # our own .func and .decorator, in case 'func' is actually
    # another MethodDecoratorAdaptor object, which has its
    # 'func' and 'decorator' attributes in its own __dict__
    self.decorator = decorator
    self.func = func 
Example 44
Project: kay-template   Author: yosukesuzuki   File: decorators.py    MIT License 5 votes vote down vote up
def cron_only(func):
  from werkzeug.exceptions import Forbidden
  def inner(request, *args, **kwargs):
    from kay.utils import is_dev_server
    from kay.conf import settings

    # Only allow access in the following cases
    # 1. We are using the dev server in DEBUG mode
    # 2. The X-AppEngine-Cron request header is set to true
    if (not (is_dev_server() and settings.DEBUG) and
        not request.headers.get("X-AppEngine-Cron") == "true"):
      raise Forbidden("This URL is cron only")
    return func(request, *args, **kwargs)
  return inner 
Example 45
Project: kay-template   Author: yosukesuzuki   File: decorators.py    MIT License 5 votes vote down vote up
def __get__(self, obj, type=None):
    if obj is None:
      return self
    value = obj.__dict__.get(self.__name__, _missing)
    if value is _missing:
      memcached_key = self._key_f(obj)
      if memcached_key is not None:
          value = memcache.get(memcached_key)
      if value is _missing or not value:
        value = self.func(obj)
        obj.__dict__[self.__name__] = value
        if memcached_key is not None:
            memcache.set(memcached_key, value, self._expire)
    return value 
Example 46
Project: kay-template   Author: yosukesuzuki   File: sessionstore.py    MIT License 5 votes vote down vote up
def save(self, session):
    key_name = self.get_key_name(session.sid)
    gae_session = self.save_to_db(key_name, session)
    memcache.set(key_name, gae_session, settings.SESSION_MEMCACHE_AGE) 
Example 47
Project: kay-template   Author: yosukesuzuki   File: middleware.py    MIT License 5 votes vote down vote up
def process_response(self, request, response):
    if not hasattr(request, '_cache_update') or not request._cache_update:
      return response
    if not hasattr(response, 'status_code') or not response.status_code == 200:
      return response
    key = get_key(request.url, request.lang)
    timeout = response.cache_control.max_age
    if timeout is None:
      timeout = self.cache_timeout
    if memcache.set(key, response, timeout, namespace=self.namespace):
      logging.debug("CacheMiddleware cache set. key: '%s', timeout: %d" %
                    (key, timeout))
    return response 
Example 48
Project: kay-template   Author: yosukesuzuki   File: __init__.py    MIT License 5 votes vote down vote up
def set(self, key, value, expire=_DEFAULT_TTL, namespace=None):
    from kay.ext.live_settings.models import KayLiveSetting
    
    old_namespace = namespace_manager.get_namespace()
    try:
      if namespace is not None:
        namespace_manager.set_namespace(namespace)

      new_setting = KayLiveSetting(
          key_name=key,
          ttl=expire,
          value=value,
      )
      new_setting.put()

      # Set the memcached key to never expire. It only expires
      # if it is evicted from memory. TTLs are handled by the 
      # in-memory cache.
      memcache.set("kay:live:%s" % key, (value, expire))
       
      self._set_local_cache(key, value, ttl=expire, namespace=namespace)
    finally:
      if namespace is not None:
        namespace_manager.set_namespace(old_namespace)

    return new_setting 
Example 49
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def is_ssl_only(self):
        """Returns True if cookies set by this session will include the "Secure"
        attribute so that the client will only send them over a secure channel
        like SSL)."""
        return self.sid is not None and self.sid[-33] == 'S' 
Example 50
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def __set_sid(self, sid, make_cookie=True):
        """Sets the session ID, deleting the old session if one existed.  The
        session's data will remain intact (only the session ID changes)."""
        if self.sid:
            self.__clear_data()
        self.sid = sid
        self.db_key = db.Key.from_path(SessionModel.kind(), sid, namespace='')

        # set the cookie if requested
        if make_cookie:
            self.cookie_data = ''  # trigger the cookie to be sent 
Example 51
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 5 votes vote down vote up
def has_key(self, key):
        """Returns True if key is set."""
        self.ensure_data_loaded()
        return key in self.data 
Example 52
Project: mlab-ns   Author: m-lab   File: appengine.py    Apache License 2.0 5 votes vote down vote up
def locked_put(self, credentials):
    """Write a Credentials to the datastore.

    Args:
      credentials: Credentials, the credentials to store.
    """
    entity = self._model.get_or_insert(self._key_name)
    setattr(entity, self._property_name, credentials)
    entity.put()
    if self._cache:
      self._cache.set(self._key_name, credentials.to_json()) 
Example 53
Project: mlab-ns   Author: m-lab   File: appengine.py    Apache License 2.0 5 votes vote down vote up
def authorize_url(self):
    """Returns the URL to start the OAuth dance.

    Must only be called from with a webapp.RequestHandler subclassed method
    that had been decorated with either @oauth_required or @oauth_aware.
    """
    callback = self._request_handler.request.relative_url('/oauth2callback')
    url = self.flow.step1_get_authorize_url(callback)
    user = users.get_current_user()
    memcache.set(user.user_id(), pickle.dumps(self.flow),
                 namespace=OAUTH2CLIENT_NAMESPACE)
    return url 
Example 54
Project: mlab-ns   Author: m-lab   File: nagios_config_wrapper.py    Apache License 2.0 5 votes vote down vote up
def get_nagios_config():
    """Retrieves nagios config info. First checks memcache, then datastore.

    Returns:
        Nagios model instance
    """
    nagios = memcache.get(constants.DEFAULT_NAGIOS_ENTRY)
    if not nagios:
        nagios = model.Nagios.get_by_key_name(constants.DEFAULT_NAGIOS_ENTRY)
        if nagios:
            memcache.set(constants.DEFAULT_NAGIOS_ENTRY, nagios)
        else:
            logging.error('Datastore does not have the Nagios credentials.')

    return nagios 
Example 55
Project: mlab-ns   Author: m-lab   File: test_sliver_tool_fetcher.py    Apache License 2.0 5 votes vote down vote up
def insertCreatedTools(self):
        tools_by_id = collections.defaultdict(lambda: [])
        for tool in self.created_tools:
            tools_by_id[tool.tool_id].append(tool)
        for tool_id, tool_list in tools_by_id.iteritems():
            memcache.set(tool_id,
                         tool_list,
                         namespace=constants.MEMCACHE_NAMESPACE_TOOLS) 
Example 56
Project: mlab-ns   Author: m-lab   File: test_nagios_config_wrapper.py    Apache License 2.0 5 votes vote down vote up
def test_get_nagios_config_returns_successfully_from_memcache(self):
        nagios_model = model.Nagios(url='foo')
        memcache.set(constants.DEFAULT_NAGIOS_ENTRY, nagios_model)
        retrieved = nagios_config_wrapper.get_nagios_config()
        self.assertEqual(retrieved.url, 'foo') 
Example 57
Project: mlab-ns   Author: m-lab   File: test_nagios_config_wrapper.py    Apache License 2.0 5 votes vote down vote up
def test_get_nagios_config_returns_from_memcache_over_datastore(self):
        memcache_nagios_model = model.Nagios(url='memcache_nagios')
        memcache.set(constants.DEFAULT_NAGIOS_ENTRY, memcache_nagios_model)

        datastore_nagios_model = model.Nagios(
            key_name=constants.DEFAULT_NAGIOS_ENTRY,
            url='datastore_nagios')
        datastore_nagios_model.put()

        retrieved = nagios_config_wrapper.get_nagios_config()
        self.assertEqual(retrieved.url, 'memcache_nagios') 
Example 58
Project: mlab-ns   Author: m-lab   File: reverse_proxy.py    Apache License 2.0 5 votes vote down vote up
def get_reverse_proxy(experiment):
    """Reads the ReverseProxyProbability record for an experiment.

    If the entity is not cached, it also refreshes the cache.
    """
    reverse_proxy = memcache.get(
        experiment,
        namespace=constants.MEMCACHE_NAMESPACE_REVERSE_PROXY)

    if reverse_proxy is None:
        # Update ReverseProxyProbability for all the experiments.
        for prob in model.ReverseProxyProbability.all().run():
            if experiment == prob.name:
                reverse_proxy = prob

            if not memcache.set(
                    prob.name,
                    prob,
                    time=1800,
                    namespace=constants.MEMCACHE_NAMESPACE_REVERSE_PROXY):
                logging.error(
                    'Failed to update ReverseProxyProbability in memcache ' +
                    'for experiment %s', prob.name)

        if reverse_proxy is None:
            logging.info('No reverse proxy probability found; using default')
            reverse_proxy = default_reverse_proxy.with_name(experiment)

    return reverse_proxy 
Example 59
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def getConferencesCreated(self, request):
        """Return conferences created by user."""
        # make sure user is authed
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        # create ancestor query for all key matches for this user
        confs = Conference.query(ancestor=ndb.Key(Profile, user_id))
        prof = ndb.Key(Profile, user_id).get()
        # return set of ConferenceForm objects per Conference
        return ConferenceForms(
            items=[self._copyConferenceToForm(conf, getattr(prof, 'displayName')) for conf in confs]
        ) 
Example 60
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def _cacheFeaturedSpeaker(websafeConferenceKey, speaker_name):
        """Create a string containing featured speaker / session names & assign to memcache;
        """
        #use websafeConferenceKey + speaker_name as key
        MEMCACHE_FEATURED_SPEAKER_KEY = websafeConferenceKey

        ss = Session.query(ancestor=ndb.Key(urlsafe=websafeConferenceKey))
        ss = ss.filter(Session.speaker == speaker_name)

        if ss and ss.count() > 1:
            message ="Featuring: "+ speaker_name + " Sessions: "
            for s in ss:
                message += s.name + ", "
            memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, message) 
Example 61
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def getSessionsCreated(self, request):
        """Return sessions for conference"""
        # create ancestor query for all key matches for this user

        ss = Session.query(ancestor=ndb.Key(urlsafe=request.websafeConferenceKey))

        # return set of ConferenceForm objects per Conference
        return SessionForms(
            sessions=[self._copySessionToForm(s) for s in ss]
        ) 
Example 62
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def getSessionsByType(self, request):
        """Return sessions for conference with requested type"""
        
        if request.sessionType not in SessionType.to_dict():
            raise endpoints.BadRequestException('Invalid session type')
        # create ancestor query for all key matches for this conference
        ss = Session.query(ancestor=ndb.Key(urlsafe=request.websafeConferenceKey)).filter(
            Session.typeOfSession==str(request.sessionType))

        # return set of SessionForm objects per session
        return SessionForms(
            sessions=[self._copySessionToForm(s) for s in ss]
        ) 
Example 63
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def getSessionsBySpeaker(self, request):
        """Return sessions for conference with requested type"""
        
        # get all sessions
        ss = Session.query().filter(Session.speaker==request.speaker) #filter by speaker

        # return set of SessionForm objects per session
        return SessionForms(
            sessions=[self._copySessionToForm(s) for s in ss]
        ) 
Example 64
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 5 votes vote down vote up
def getPopularConferences(self, request):
        """Return x number of most popular conferences."""
        # make sure user is authed
        if request.num < 1:
            raise endpoints.BadRequestException('Please choose a number > 0.')

        confs = Conference.query().order(Conference.ratioAvailable) #ascending, less available = more popular

        confs = confs.fetch(request.num)
        # return set of ConferenceForm objects per Conference
        return ConferenceForms(
            items=[self._copyConferenceToForm(conf) for conf in confs]
        ) 
Example 65
Project: isthislegit   Author: duo-labs   File: memcache.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set(key=None, value=None, time=TIMEOUT):
    parts = _split_value(json.dumps(value))
    memcache.set(key=key + '_parts', value=len(parts), time=time)
    for i, part in enumerate(parts):
        logging.debug("Setting %s%d" % (key, i))
        memcache.set(key='%s%d' % (key, i), value=part, time=time) 
Example 66
Project: dev-random-as-a-service   Author: phoeagon   File: random_dev.py    Apache License 2.0 5 votes vote down vote up
def get(self):
        val = memcache.get(self.KEY)
        if val is not None:
            return val
        else:
            stat = Stats.query(Stats.key==self.KEY).get()
            if stat is not None:
                memcache.set(key=self.KEY, value=int(stat.val))
                return int(stat.val)
        return DEFAULT_COUNT 
Example 67
Project: dev-random-as-a-service   Author: phoeagon   File: random_dev.py    Apache License 2.0 5 votes vote down vote up
def set(self, val):
        memcache.set(key=self.KEY, value=str(val)) 
Example 68
Project: dev-random-as-a-service   Author: phoeagon   File: random_dev.py    Apache License 2.0 5 votes vote down vote up
def delta(self, diff):
        val = memcache.get(self.KEY)
        if val is None:
            stat = Stats.query(Stats.key==self.KEY).get()
            if stat:
                val = int(stat.val)
        if val is None:
            val = DEFAULT_COUNT
        val += diff
        # Enforce non-zero
        if val < 0:
            val = 0
        memcache.set(key=self.KEY, value=val) 
Example 69
Project: dev-random-as-a-service   Author: phoeagon   File: random_dev.py    Apache License 2.0 5 votes vote down vote up
def set(self, val):
        self._persistent().set(val) 
Example 70
Project: syndicate-core   Author: syndicate-storage   File: shard.py    Apache License 2.0 5 votes vote down vote up
def set_modtime_notrans(config, name, mtime_sec, mtime_nsec, sync=True):
    """Transactional 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.
    """
    num_shards = config.num_shards
    
    index = random.randint(0, num_shards - 1)

    shard_key_string = SHARD_KEY_TEMPLATE.format(name, index)
    
    mtime = Shard.get_by_id(shard_key_string)
    if mtime is None:
       mtime = Shard(id=shard_key_string)

    mtime.mtime_sec = mtime_sec
    mtime.mtime_nsec = mtime_nsec

    memcache.set( name, mtime )

    fut = None
    if sync:
       mtime.put()
    else:
       fut = mtime.put_async()

    return fut 
Example 71
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 5 votes vote down vote up
def _get_count_async(name, key_gen, use_memcache=True):
    """Retrieve the value for a given sharded counter.

    Args:
        name: The name of the counter.
        num_shards: the number of shards
    Returns:
        Integer; the cumulative count of all sharded counters for the given
            counter name.
    """
    
    # only cache if it exists at all 
    do_cache = False
    cached = False

    total = None 
    
    if use_memcache:
      total = memcache.get(name)
      cached = True
      
    if total is None:
        total = 0
        all_keys = key_gen()
        counters = yield ndb.get_multi_async( all_keys, use_cache=False, use_memcache=False )
        for counter in counters:
            if counter is not None:
                total += counter.count
                do_cache = True 
                
        if do_cache and use_memcache:
            memcache.set(name, total)
           
    logging.info("Value of {} is {} (cached: {})".format(name, total, cached))
    raise ndb.Return( total ) 
Example 72
Project: syndicate-core   Author: syndicate-storage   File: shardcounter.py    Apache License 2.0 5 votes vote down vote up
def count_from_futures( name, futs, do_cache=True ):
   """
   Get the value of a counter from a list of futures returned from get_count_async 
   """
   
   count = 0
   for f in futs:
      count += f.get_result()
   
   if do_cache:
      memcache.set(name, count)
   
   return count 
Example 73
Project: blockhooks   Author: EthereumWebhooks   File: sessions_ndb.py    Apache License 2.0 5 votes vote down vote up
def _put(self):
        """Saves the session and updates the memcache entry."""
        memcache.set(self._key.id(), self.data)
        super(Session, self).put() 
Example 74
Project: blockhooks   Author: EthereumWebhooks   File: sessions_memcache.py    Apache License 2.0 5 votes vote down vote up
def save_session(self, response):
        if self.session is None or not self.session.modified:
            return

        memcache.set(self.sid, dict(self.session))
        self.session_store.save_secure_cookie(
            response, self.name, {'_sid': self.sid}, **self.session_args) 
Example 75
Project: appengine-python-flask-travis-ci   Author: russomi   File: test_datastore.py    Apache License 2.0 5 votes vote down vote up
def GetEntityViaMemcache(entity_key):
  """Get entity from memcache if available, from datastore if not."""
  entity = memcache.get(entity_key)
  if entity is not None:
    return entity
  key = ndb.Key(urlsafe=entity_key)
  entity = key.get()
  if entity is not None:
    memcache.set(entity_key, entity)
  return entity
# [END datastore_example_1]


# [START datastore_example_test] 
Example 76
Project: loaner   Author: google   File: config_model.py    Apache License 2.0 4 votes vote down vote up
def get(cls, name):
    """Checks memcache for name, if not available, check datastore.

    Args:
      name: str, name of config name.

    Returns:
      The config value from memcache, datastore, or config file.

    Raises:
      KeyError: An error occurred when name does not exist.
    """
    memcache_config = memcache.get(name)
    cached_config = None
    if memcache_config:
      return memcache_config

    stored_config = cls.get_by_id(name, use_memcache=False)
    if stored_config:
      if stored_config.string_value:
        cached_config = stored_config.string_value
      elif stored_config.integer_value:
        cached_config = stored_config.integer_value
      elif stored_config.bool_value is not None:
        cached_config = stored_config.bool_value
      elif stored_config.list_value:
        cached_config = stored_config.list_value
    # Conversion from use_asset_tags to device_identifier_mode.
    if name == 'device_identifier_mode' and not cached_config:
      if cls.get('use_asset_tags'):
        cached_config = DeviceIdentifierMode.BOTH_REQUIRED
        cls.set(name, cached_config)
        memcache.set(name, cached_config)
    if cached_config is not None:
      memcache.set(name, cached_config)
      return cached_config
    config_defaults = utils.load_config_from_yaml()
    if name in config_defaults:
      value = config_defaults[name]
      cls.set(name, value)
      return value

    raise KeyError(_CONFIG_NOT_FOUND_MSG, name) 
Example 77
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 4 votes vote down vote up
def get_template(self, template_name, _context=None, _globals=None):
        """Return template object.
           If template object has not registered, template engine creates
           and registers template object automatically.
        """
        #: accept template_name such as ':index'.
        filename = self.to_filename(template_name)
        #: if template object is added by add_template(), return it.
        if filename in self._added_templates:
            return self._added_templates[filename]
        #: get filepath and fullpath of template
        pair = self._filepaths.get(filename)
        if pair:
            filepath, fullpath = pair
        else:
            #: if template file is not found then raise TemplateNotFoundError.
            filepath = self.loader.find(filename, self.path)
            if not filepath:
                raise TemplateNotFoundError('%s: filename not found (path=%r).' % (filename, self.path))
            #
            fullpath = self.loader.abspath(filepath)
            self._filepaths[filename] = (filepath, fullpath)
        #: use full path as base of cache file path
        cachepath = self.cachename(fullpath)
        #: get template object from cache
        cache = self.cache
        template = cache and self._get_template_from_cache(cachepath, filepath) or None
        #: if template object is not found in cache or is expired...
        if not template:
            ret = self.loader.load(filepath)
            if not ret:
                raise TemplateNotFoundError("%r: template not found." % filepath)
            input, timestamp = ret
            if self.preprocess:   ## required for preprocessing
                if _context is None: _context = {}
                if _globals is None: _globals = sys._getframe(1).f_globals
                input = self._preprocess(input, filepath, _context, _globals)
            #: create template object.
            template = self._create_template(input, filepath, _context, _globals)
            #: set timestamp and filename of template object.
            template.timestamp = timestamp
            template._last_checked_at = _time()
            #: save template object into cache.
            if cache:
                if not template.bytecode: template.compile()
                cache.set(cachepath, template)
        #else:
        #    template.compile()
        #:
        template.filename = filepath
        return template 
Example 78
Project: kay-template   Author: yosukesuzuki   File: __init__.py    MIT License 4 votes vote down vote up
def get(self, key, default=None, 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)

      set_dictcache = False
      set_memcache = False
  
      value = self._get_local_cache(key, namespace=namespace)
      expire = _missing
      ttl = _missing
  
      if value is _missing:
        set_dictcache = True
        value = memcache.get("kay:live:%s" % key)
        if value:
          value,ttl = value
        else:
          value,ttl = (_missing, _missing)
      if value is _missing:
        set_memcache = True
        entity = KayLiveSetting.get_by_key_name(key)
        if entity:
          value = entity.value or _missing
          ttl = entity.ttl or _missing
  
      if value is _missing:
        return default
      else:
        if ttl is None or ttl is _missing:
          ttl = _DEFAULT_TTL 
        if set_dictcache:
          self._set_local_cache(key, value, ttl=ttl, namespace=namespace)
        if set_memcache:
          memcache.set("kay:live:%s" % key, (value, ttl))
  
        return value
    finally:
      if namespace is not None:
        namespace_manager.set_namespace(old_namespace) 
Example 79
Project: tra-tracking-on-gae   Author: elliot79313   File: __init__.py    MIT License 4 votes vote down vote up
def save(self, persist_even_if_using_cookie=False):
        """Saves the data associated with this session IF any changes have been
        made (specifically, if any mutator methods like __setitem__ or the like
        is called).

        If the data is small enough it will be sent back to the user in a cookie
        instead of using memcache and the datastore.  If `persist_even_if_using_cookie`
        evaluates to True, memcache and the datastore will also be used.  If the
        no_datastore option is set, then the datastore will never be used.

        Normally this method does not need to be called directly - a session is
        automatically saved at the end of the request if any changes were made.
        """
        if not self.sid:
            return  # no session is active
        if not self.dirty:
            return  # nothing has changed
        dirty = self.dirty
        self.dirty = False  # saving, so it won't be dirty anymore

        # do the pickling ourselves b/c we need it for the datastore anyway
        pdump = self.__encode_data(self.data)

        # persist via cookies if it is reasonably small
        if len(pdump) * 4 / 3 <= self.cookie_only_thresh:  # 4/3 b/c base64 is ~33% bigger
            self.cookie_data = pdump
            if not persist_even_if_using_cookie:
                return
        elif self.cookie_keys:
            # latest data will only be in the backend, so expire data cookies we set
            self.cookie_data = ''

        memcache.set(self.sid, pdump, namespace='', time=self.get_expiration())  # may fail if memcache is down

        # persist the session to the datastore
        if dirty is Session.DIRTY_BUT_DONT_PERSIST_TO_DB or self.no_datastore:
            return
        try:
            SessionModel(key_name=self.sid, pdump=pdump).put()
        except Exception, e:
            logging.warning("unable to persist session to datastore for sid=%s (%s)" % (self.sid, e))

    # Users may interact with the session through a dictionary-like interface. 
Example 80
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _createConferenceObject(self, request):
        """Create or update Conference object, returning ConferenceForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException("Conference 'name' field required")

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        del data['websafeKey']
        del data['organizerDisplayName']

        # add default values for those missing (both data model & outbound Message)
        for df in DEFAULTS:
            if data[df] in (None, []):
                data[df] = DEFAULTS[df]
                setattr(request, df, DEFAULTS[df])

        # if data['maxAttendees'] > 0:
        #     data['ratioAvailable'] = float(data['seatsAvailable'])/float(data['maxAttendees'])
        #     request.ratioAvailable = data['ratioAvailable']

        # convert dates from strings to Date objects; set month based on start_date
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10], "%Y-%m-%d").date()
            data['month'] = data['startDate'].month
        else:
            data['month'] = 0
        if data['endDate']:
            data['endDate'] = datetime.strptime(data['endDate'][:10], "%Y-%m-%d").date()

        # set seatsAvailable to be same as maxAttendees on creation
        if data["maxAttendees"] > 0:
            data["seatsAvailable"] = data["maxAttendees"]
        # generate Profile Key based on user ID and Conference
        # ID based on Profile key get Conference key from ID
        p_key = ndb.Key(Profile, user_id)
        c_id = Conference.allocate_ids(size=1, parent=p_key)[0]
        c_key = ndb.Key(Conference, c_id, parent=p_key)
        data['key'] = c_key
        data['organizerUserId'] = request.organizerUserId = user_id



        # create Conference, send email to organizer confirming
        # creation of Conference & return (modified) ConferenceForm
        Conference(**data).put()
        taskqueue.add(params={'email': user.email(),
            'conferenceInfo': repr(request)},
            url='/tasks/send_confirmation_email'
        )
        return request