thumbnail with a memory cache

def get_thumbnail(self, width, height):
        """
        thumbnail with a memory cache
        """
        # get from the file
        thumb_path = self._get_thumb_path()
        try:
            doc_file_path = self.get_doc_file_path()

            if (self.fs.exists(thumb_path) and
                    self.fs.getmtime(doc_file_path) <
                    self.fs.getmtime(thumb_path)):
                with self.fs.open(thumb_path, 'rb') as fd:
                    thumbnail = PIL.Image.open(fd)
                    thumbnail.load()
                if thumbnail.size[0] == width or thumbnail.size[1] == height:
                    # fills the specified area
                    return thumbnail
                logger.warning(
                    "[%s] Unexpected thumbnail size: %s instead of %s ;"
                    " Updating thumbnail ...",
                    str(self.doc.docid), str(thumbnail.size),
                    str((width, height))
                )
        except Exception as exc:
            logger.warning(
                "[%s] Failed to check doc and thumbnail mdate. Forcing update"
                " of the thumbnail", str(self.doc.docid), exc_info=exc
            )

        logger.info("[%s] Updating thumbnail ...", str(self.doc.docid))
        thumbnail = self.__make_thumbnail(width, height)
        with self.fs.open(thumb_path, 'wb') as fd:
            thumbnail.save(fd, format="JPEG")

        return thumbnail