Esempio n. 1
0
 def __init__(self, keys):
     self._keys = list(keys)
     self._data_valid = False
     self.__data = None
     self._cache = memcache.Client()
     self._cache_rpc = memcache.create_rpc()
     self._cache_prefix = self.__class__.__name__
     self._cache.get_multi_async(self._keys, key_prefix=self._cache_prefix,
                                 rpc=self._cache_rpc)
Esempio n. 2
0
 def __init__(self, keys):
     self._keys = list(keys)
     self._data_valid = False
     self.__data = None
     self._cache = memcache.Client()
     self._cache_rpc = memcache.create_rpc()
     self._cache_prefix = self.__class__.__name__
     self._cache.get_multi_async(self._keys,
                                 key_prefix=self._cache_prefix,
                                 rpc=self._cache_rpc)
    def POST(self, world):
        cur_world = world_exists(world)
        self.redirect_if_not_create(cur_world)

        i = web.input(long=[], lat=[], prop_name=[], prop_val=[])
        coord = []
        new = []
        access = cur_world.get_access_string(self.page_user)
        form = self.text_form()

        if not form.validates():
            world_menu = self.get_or_put_menu(cur_world, access)
            return self.reset_text_page(cur_world, form, access, world_menu)
        else:

            if cur_world.is_mod(self.page_user):
                visible = True
                """ Flush menus for everyone so they get reset"""
                mrpc = memcache.create_rpc()
                m = flush_caches("", cur_world, mrpc)
                try:
                    m.get_result()
                except AssertionError: # There were no caches to flush - is this even the problem?
                    pass
            else:
                visible = False

            new_text = Room(world = cur_world.key,
                            name=form.d.name,
                            short_desc=form.d.short_desc,
                            long_desc=form.d.long_desc,
                            added_by=self.page_user.key,
                            latitudes=i.lat,
                            longitudes=i.long,
                            visible=visible,
                            rejected=False
                        )
            new_text.put()

            for index, name in enumerate(i.prop_name):
                p = Property.get_by_id(int(name))
                new.append(PropertyValue())
                new[index].value = i.prop_val[index]
                new[index].of_property = p.key
                new[index].room = new_text.key
                new[index].added_by = self.page_user.key
                new[index].visible = visible

            valftrs = ndb.put_multi_async(new)
            self.set_menu(cur_world, access)

            for ftr in valftrs:
                ftr.get_result()

            return web.seeother('/%s/text/%s' % (cur_world.key.id(), new_text.key.id()))
Esempio n. 4
0
    def POST(self, world):
        cur_world = world_exists(world)
        self.redirect_if_not_create(cur_world)

        i = web.input(long=[], lat=[], prop_name=[], prop_val=[])
        coord = []
        new = []
        access = cur_world.get_access_string(self.page_user)
        form = self.text_form()

        if not form.validates():
            world_menu = self.get_or_put_menu(cur_world, access)
            return self.reset_text_page(cur_world, form, access, world_menu)
        else:

            if cur_world.is_mod(self.page_user):
                visible = True
                """ Flush menus for everyone so they get reset"""
                mrpc = memcache.create_rpc()
                m = flush_caches("", cur_world, mrpc)
                try:
                    m.get_result()
                except AssertionError:  # There were no caches to flush - is this even the problem?
                    pass
            else:
                visible = False

            new_text = Room(world=cur_world.key,
                            name=form.d.name,
                            short_desc=form.d.short_desc,
                            long_desc=form.d.long_desc,
                            added_by=self.page_user.key,
                            latitudes=i.lat,
                            longitudes=i.long,
                            visible=visible,
                            rejected=False)
            new_text.put()

            for index, name in enumerate(i.prop_name):
                p = Property.get_by_id(int(name))
                new.append(PropertyValue())
                new[index].value = i.prop_val[index]
                new[index].of_property = p.key
                new[index].room = new_text.key
                new[index].added_by = self.page_user.key
                new[index].visible = visible

            valftrs = ndb.put_multi_async(new)
            self.set_menu(cur_world, access)

            for ftr in valftrs:
                ftr.get_result()

            return web.seeother('/%s/text/%s' %
                                (cur_world.key.id(), new_text.key.id()))
Esempio n. 5
0
 def _memcache_off_tasklet(self, todo, options):
     if not todo:
         raise RuntimeError('Nothing to do.')
     initial_value, namespace, deadline = options
     mapping = {}
     for unused_fut, (key, delta) in todo:
         mapping[key] = delta
     rpc = memcache.create_rpc(deadline=deadline)
     results = yield self._memcache.offset_multi_async(
         mapping, initial_value=initial_value, namespace=namespace, rpc=rpc)
     for fut, (key, unused_delta) in todo:
         fut.set_result(results.get(key))
Esempio n. 6
0
 def _memcache_get_tasklet(self, todo, options):
     if not todo:
         raise RuntimeError('Nothing to do.')
     for_cas, namespace, deadline = options
     keys = set()
     for unused_fut, key in todo:
         keys.add(key)
     rpc = memcache.create_rpc(deadline=deadline)
     results = yield self._memcache.get_multi_async(keys,
                                                    for_cas=for_cas,
                                                    namespace=namespace,
                                                    rpc=rpc)
     for fut, key in todo:
         fut.set_result(results.get(key))
Esempio n. 7
0
def memcache_util_get_multi_async_with_deadline(
        keys, key_prefix='', namespace=None,
        deadline=DEFAULT_MEMCACHE_GET_DEADLINE):
    """Like get_multi_async(), but fails if it takes longer than deadline.

    Asynchronously looks up multiple keys from memcache in one
    operation.  Deadline is in seconds and is defaulted to a
    reasonable value unless set explicitly.

    See memcache.Client().get_multi_async documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    return memcache.Client().get_multi_async(keys, key_prefix=key_prefix,
                                             namespace=namespace, rpc=rpc)
Esempio n. 8
0
def memcache_util_add_multi_async_with_deadline(
        mapping, time=0, key_prefix='', min_compress_len=0, namespace=None,
        deadline=DEFAULT_MEMCACHE_SET_DEADLINE):
    """Like add_multi_async(), but fails if it takes longer than deadline.

    Asynchronously adds multiple keys' values at once.  Deadline is in
    seconds and is defaulted to a reasonable value unless set
    explicitly.

    See memcache.Client().add_multi_async documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    return memcache.Client().add_multi_async(mapping, time=time,
                                             key_prefix=key_prefix,
                                             min_compress_len=min_compress_len,
                                             namespace=namespace, rpc=rpc)
Esempio n. 9
0
def memcache_util_get_multi_async_with_deadline(
        keys,
        key_prefix='',
        namespace=None,
        deadline=DEFAULT_MEMCACHE_GET_DEADLINE):
    """Like get_multi_async(), but fails if it takes longer than deadline.

    Asynchronously looks up multiple keys from memcache in one
    operation.  Deadline is in seconds and is defaulted to a
    reasonable value unless set explicitly.

    See memcache.Client().get_multi_async documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    return memcache.Client().get_multi_async(keys,
                                             key_prefix=key_prefix,
                                             namespace=namespace,
                                             rpc=rpc)
Esempio n. 10
0
    def get(self):
        count = int(self.request.get('count'))
        if count < 1:
            self.response.write('Count must be at least 1\n')
            return

        rpcs = []
        c = memcache.Client()

        for x in xrange(count):
            rpc = memcache.create_rpc(deadline=30)
            c.get_multi_async(["roger"], rpc=rpc)
            rpcs.append(rpc)

        for rpc in rpcs:
            rpc.wait()

        self.response.write('OK\n')
Esempio n. 11
0
def memcache_util_delete_with_deadline(
        key, seconds=0, namespace=None,
        deadline=DEFAULT_MEMCACHE_SET_DEADLINE):
    """Like delete(), but fails if it takes longer than deadline seconds.

    Deletes a key.  Deadline is in seconds and is defaulted to a
    reasonable value unless delete explicitly.

    If the deadline is reached before memcache responds,  False is returned.

    See memcache.delete documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    async_response = memcache.Client().delete_multi_async(
        [key], seconds=seconds, key_prefix='',
        namespace=namespace, rpc=rpc)
    results = async_response.get_result()
    if not results:
        return memcache.DELETE_NETWORK_FAILURE
    return results[0]
Esempio n. 12
0
 def _memcache_del_tasklet(self, todo, options):
     if not todo:
         raise RuntimeError('Nothing to do.')
     seconds, namespace, deadline = options
     keys = set()
     for unused_fut, key in todo:
         keys.add(key)
     rpc = memcache.create_rpc(deadline=deadline)
     statuses = yield self._memcache.delete_multi_async(keys,
                                                        seconds=seconds,
                                                        namespace=namespace,
                                                        rpc=rpc)
     status_key_mapping = {}
     if statuses:
         for key, status in zip(keys, statuses):
             status_key_mapping[key] = status
     for fut, key in todo:
         status = status_key_mapping.get(key,
                                         memcache.DELETE_NETWORK_FAILURE)
         fut.set_result(status)
Esempio n. 13
0
 def _memcache_set_tasklet(self, todo, options):
     if not todo:
         raise RuntimeError('Nothing to do.')
     opname, time, namespace, deadline = options
     methodname = opname + '_multi_async'
     method = getattr(self._memcache, methodname)
     mapping = {}
     for unused_fut, (key, value) in todo:
         mapping[key] = value
     rpc = memcache.create_rpc(deadline=deadline)
     results = yield method(mapping,
                            time=time,
                            namespace=namespace,
                            rpc=rpc)
     for fut, (key, unused_value) in todo:
         if results is None:
             status = memcache.MemcacheSetResponse.ERROR
         else:
             status = results.get(key)
         fut.set_result(status == memcache.MemcacheSetResponse.STORED)
Esempio n. 14
0
def memcache_util_add_multi_async_with_deadline(
        mapping,
        time=0,
        key_prefix='',
        min_compress_len=0,
        namespace=None,
        deadline=DEFAULT_MEMCACHE_SET_DEADLINE):
    """Like add_multi_async(), but fails if it takes longer than deadline.

    Asynchronously adds multiple keys' values at once.  Deadline is in
    seconds and is defaulted to a reasonable value unless set
    explicitly.

    See memcache.Client().add_multi_async documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    return memcache.Client().add_multi_async(mapping,
                                             time=time,
                                             key_prefix=key_prefix,
                                             min_compress_len=min_compress_len,
                                             namespace=namespace,
                                             rpc=rpc)
Esempio n. 15
0
def memcache_util_delete_with_deadline(key,
                                       seconds=0,
                                       namespace=None,
                                       deadline=DEFAULT_MEMCACHE_SET_DEADLINE):
    """Like delete(), but fails if it takes longer than deadline seconds.

    Deletes a key.  Deadline is in seconds and is defaulted to a
    reasonable value unless delete explicitly.

    If the deadline is reached before memcache responds,  False is returned.

    See memcache.delete documentation for details.
    """
    rpc = memcache.create_rpc(deadline=deadline)
    async_response = memcache.Client().delete_multi_async([key],
                                                          seconds=seconds,
                                                          key_prefix='',
                                                          namespace=namespace,
                                                          rpc=rpc)
    results = async_response.get_result()
    if not results:
        return memcache.DELETE_NETWORK_FAILURE
    return results[0]
 def test_raise_rpc_error(self):
     rpc = memcache.create_rpc()
     with self.assertRaises(Exception):
         memcachetag.get_multi_async_with_tags(['test'], rpc=rpc)
Esempio n. 17
0
    def POST(self, world, text):
        cur_world = world_exists(world)
        cur_text = text_exists(text)
        self.redirect_if_not_edit(cur_world, cur_text)

        a = cur_world.get_access_string(self.page_user)

        post = web.input()
        if hasattr(post, 'delete'):
            
            PropertyValue.delete_all_for_text(cur_text)
            cur_text.key.delete()

            """ Flush menus for everyone so they get reset"""
            # TODO: Fix cache flushing for everyone else
            self.replace_or_put_menu(cur_world, a)
            if a == "admin":
                memcache.delete("%s-closed" % cur_world.key.id())
            elif a == "closed":
                memcache.delete("%s-admin" % cur_world.key.id())

            raise web.seeother("/%s/text" % cur_world.key.id())

        else:

            i = web.input(long=[], lat=[], prop_name=[], prop_val=[], del_property=[], action_val=[], mod_id=[])
            coord = []
            new = []

            form = self.text_form()
            if not form.validates():
                world_menu = self.get_or_put_menu(cur_world, a)
                return self.reset_text_page(cur_world, form, a, world_menu, cur_text)
        
            else:

                cur_text.world = cur_world.key
                cur_text.name = form.d.name
                cur_text.short_desc = form.d.short_desc
                cur_text.long_desc = form.d.long_desc
                cur_text.last_modified_by = self.page_user.key
                cur_text.latitudes = i.lat
                cur_text.longitudes = i.long

                text_ftr = cur_text.put_async()

                if cur_world.is_mod(self.page_user):
                    visible = True
                    """ Flush menus for everyone so they get reset"""
                    mrpc = memcache.create_rpc()
                    m = flush_caches("", cur_world, mrpc)
                    try:
                        m.get_result()
                    except AssertionError: # There were no caches to flush - this doesn't work
                        pass
                else:
                    visible = False

                for index, name in enumerate(i.prop_name):
                    p = ndb.Key('Property', int(name))
                    new.append(PropertyValue())
                    new[index].value = i.prop_val[index]
                    new[index].of_property = p
                    new[index].room = cur_text.key
                    new[index].added_by = self.page_user.key
                    new[index].visible = visible

                new_values_ftrs = ndb.put_multi_async(new)

                value_keys = []
                for index, p_id in enumerate(i.del_property):
                    value_keys.append(ndb.Key('PropertyValue', int(p_id)))
                ndb.delete_multi(value_keys)

                for ftr in new_values_ftrs:
                    ftr.get_result()

                text_ftr.get_result()

                self.set_menu(cur_world, a)

                return web.seeother("/%s/text/%s" % (cur_world.key.id(), cur_text.key.id()))