Ejemplo n.º 1
0
    def isfile(self, afilename):
        """Checks file existence by looking up the datastore row."""
        filename = self._logical_to_physical(afilename)

        # Check cache.
        result = MemcacheManager.get(self.make_key(filename),
                                     namespace=self._ns)
        if result:
            return True
        if NO_OBJECT == result:
            return False

        # Check datastore.
        metadata = FileMetadataEntity.get_by_key_name(filename)
        if metadata:
            return True

        result = False

        # Check with parent fs.
        if self._inherits_from and self._can_inherit(filename):
            result = self._inherits_from.isfile(afilename)

        # Put NO_OBJECT marker into memcache to avoid repeated lookups.
        if not result:
            MemcacheManager.set(self.make_key(filename),
                                NO_OBJECT,
                                namespace=self._ns)

        return result
Ejemplo n.º 2
0
    def isfile(self, afilename):
        """Checks file existence by looking up the datastore row."""
        filename = self._logical_to_physical(afilename)

        # Check cache.
        result = MemcacheManager.get(
            self.make_key(filename), namespace=self._ns)
        if result:
            return True
        if NO_OBJECT == result:
            return False

        # Check datastore.
        metadata = FileMetadataEntity.get_by_key_name(filename)
        if metadata:
            return True

        result = False

        # Check with parent fs.
        if self._inherits_from and self._can_inherit(filename):
            result = self._inherits_from.isfile(afilename)

        # Put NO_OBJECT marker into memcache to avoid repeated lookups.
        if not result:
            MemcacheManager.set(
                self.make_key(filename), NO_OBJECT, namespace=self._ns)

        return result
Ejemplo n.º 3
0
    def _load_from_memcache(self):
        """Loads course representation from memcache."""
        try:
            envelope = MemcacheManager.get(self.memcache_key)
            if envelope:
                self._units = envelope.units
                self._lessons = envelope.lessons
                self._reindex()

                self._loaded = True
        except Exception as e:  # pylint: disable-msg=broad-except
            logging.error(
                'Failed to load course \'%s\' from memcache. %s',
                self.memcache_key, e)
Ejemplo n.º 4
0
    def _load_from_memcache(self):
        """Loads course representation from memcache."""
        try:
            envelope = MemcacheManager.get(self.memcache_key)
            if envelope:
                self._units = envelope.units
                self._lessons = envelope.lessons
                self._references = envelope.references
                self._reindex()

                self._loaded = True
        except Exception as e:  # pylint: disable-msg=broad-except
            logging.error('Failed to load course \'%s\' from memcache. %s',
                          self.memcache_key, e)
Ejemplo n.º 5
0
 def load(cls, app_context):
     """Loads instance from memcache; does not fail on errors."""
     try:
         binary_data = MemcacheManager.get(
             cls._make_key(),
             namespace=app_context.get_namespace_name())
         if binary_data:
             memento = cls.new_memento()
             memento.deserialize(binary_data)
             return cls.instance_from_memento(app_context, memento)
     except Exception as e:  # pylint: disable-msg=broad-except
         logging.error(
             'Failed to load object \'%s\' from memcache. %s',
             cls._make_key(), e)
         return None
Ejemplo n.º 6
0
 def load(cls, app_context):
     """Loads instance from memcache; does not fail on errors."""
     try:
         binary_data = MemcacheManager.get(
             cls._make_key(),
             namespace=app_context.get_namespace_name())
         if binary_data:
             memento = cls.new_memento()
             memento.deserialize(binary_data)
             return cls.instance_from_memento(app_context, memento)
     except Exception as e:  # pylint: disable-msg=broad-except
         logging.error(
             'Failed to load object \'%s\' from memcache. %s',
             cls._make_key(), e)
         return None
Ejemplo n.º 7
0
    def get(self, afilename):
        """Gets a file from a datastore. Raw bytes stream, no encodings."""
        filename = self._logical_to_physical(afilename)

        # Load from cache.
        result = MemcacheManager.get(self.make_key(filename),
                                     namespace=self._ns)
        if result:
            return result
        if NO_OBJECT == result:
            return None

        # Load from a datastore.
        metadata = FileMetadataEntity.get_by_key_name(filename)
        if metadata:
            data = FileDataEntity.get_by_key_name(filename)
            if data:
                result = FileStreamWrapped(metadata, data.data)
                MemcacheManager.set(self.make_key(filename),
                                    result,
                                    namespace=self._ns)
                return result

        result = None
        metadata = None

        # Load from parent fs.
        if self._inherits_from and self._can_inherit(filename):
            result = self._inherits_from.get(afilename)

        # Cache result.
        if result:
            result = FileStreamWrapped(metadata, result.read())
            MemcacheManager.set(self.make_key(filename),
                                result,
                                namespace=self._ns)
        else:
            MemcacheManager.set(self.make_key(filename),
                                NO_OBJECT,
                                namespace=self._ns)

        return result
Ejemplo n.º 8
0
    def get(self, afilename):
        """Gets a file from a datastore. Raw bytes stream, no encodings."""
        filename = self._logical_to_physical(afilename)

        # Load from cache.
        result = MemcacheManager.get(
            self.make_key(filename), namespace=self._ns)
        if result:
            return result
        if NO_OBJECT == result:
            return None

        # Load from a datastore.
        metadata = FileMetadataEntity.get_by_key_name(filename)
        if metadata:
            data = FileDataEntity.get_by_key_name(filename)
            if data:
                result = FileStreamWrapped(metadata, data.data)
                MemcacheManager.set(
                    self.make_key(filename), result, namespace=self._ns)
                return result

        result = None
        metadata = None

        # Load from parent fs.
        if self._inherits_from and self._can_inherit(filename):
            result = self._inherits_from.get(afilename)

        # Cache result.
        if result:
            result = FileStreamWrapped(metadata, result.read())
            MemcacheManager.set(
                self.make_key(filename), result, namespace=self._ns)
        else:
            MemcacheManager.set(
                self.make_key(filename), NO_OBJECT, namespace=self._ns)

        return result
Ejemplo n.º 9
0
 def _load_permissions_map(cls):
     """Loads the permissions map from Memcache or creates it if needed."""
     permissions_map = MemcacheManager.get(cls.memcache_key)
     if permissions_map is None:  # As opposed to {}, which is valid.
         permissions_map = cls.update_permissions_map()
     return permissions_map
Ejemplo n.º 10
0
 def _load_permissions_map(cls):
     """Loads the permissions map from Memcache or creates it if needed."""
     permissions_map = MemcacheManager.get(cls.memcache_key)
     if not permissions_map:
         permissions_map = cls.update_permissions_map()
     return permissions_map
Ejemplo n.º 11
0
 def _load_permissions_map(cls):
     """Loads the permissions map from Memcache or creates it if needed."""
     permissions_map = MemcacheManager.get(cls.memcache_key)
     if permissions_map is None:  # As opposed to {}, which is valid.
         permissions_map = cls.update_permissions_map()
     return permissions_map