input: Write a function to optimize a Flask application for efficient performance. The function should use Redis for caching session and query performance, uWSGI for Python applications, gunicorn as WSGI server, Flask-Compress extension for compression, global variables where applicable, model methods instead of direct SQL queries, query parameters in URLs instead of plain parameters, and thread-local objects for performance. The function should return a Flask application instance with these optimizations applied.
  assert optimize_flask_app() is not None
  assert type(optimize_flask_app()) == Flask
output: def optimize_flask_app():
      from flask import Flask, g
      from flask_compress import Compress
      from redis import Redis
      from werkzeug.local import LocalProxy
      import threading
  
      app = Flask(__name__)
      Compress(app)
  
      app.config['SESSION_TYPE'] = 'redis'
      app.config['SESSION_REDIS'] = Redis(host='localhost', port=6379)
  
      @app.before_request
      def set_thread_local():
          g.thread_local = threading.local()
  
      @app.route('/query')
      def query():
          query_param = request.args.get('param')
          if hasattr(g.thread_local, 'cached_result'):
              return g.thread_local.cached_result
          result = perform_query(query_param)
          g.thread_local.cached_result = result
          return result
  
      def perform_query(param):
          # Example model method instead of direct SQL query
          return f"Query result for {param}"
  
      return app