title: flask-cache TypeError: can't pickle CompiledFFI objects
body: <p>i am using <a href="https://github.com/IdentityPython/pysaml2" rel="nofollow noreferrer">pysaml2</a> library in python. which has a <a href="https://github.com/IdentityPython/pysaml2/blob/master/example/idp2/idp_uwsgi.py#L335" rel="nofollow noreferrer">method defined as below</a>:</p>
  
  <pre><code>def _store_request(self, saml_msg):
      key = sha1(saml_msg["SAMLRequest"]).hexdigest()
      IDP.ticket[key] = saml_msg
      return key
  </code></pre>
  
  <p>where, <code>IDP.ticket = {}</code> is an application wide simple key value mapping(in a <a href="https://github.com/IdentityPython/pysaml2/blob/master/example/idp2/idp_uwsgi.py" rel="nofollow noreferrer">raw WSGI application</a>).here <code>IDP.ticket</code> persists between multiple requests . some context below.</p>
  
  <p>The route that is called is <a href="https://github.com/IdentityPython/pysaml2/blob/b224a021960a65d6495580f2b94734a530d62d90/example/idp2/idp_uwsgi.py#L342" rel="nofollow noreferrer">SSO().redirect</a>, </p>
  
  <ul>
  <li>Initially <code>self.user</code> is <code>False</code> so key is being set by 
  <a href="https://github.com/IdentityPython/pysaml2/blob/b224a021960a65d6495580f2b94734a530d62d90/example/idp2/idp_uwsgi.py#L386" rel="nofollow noreferrer">self._store_request(saml_msg)</a></li>
  <li>Then user is returned with a form with the key set in form so that the request can be identified. On form submit <a href="https://github.com/IdentityPython/pysaml2/blob/b224a021960a65d6495580f2b94734a530d62d90/example/idp2/idp_uwsgi.py#L342" rel="nofollow noreferrer">SSO().redirect</a> is called again &amp; value set in <code>IDP.ticket</code> during previous request was obtained.   </li>
  </ul>
  
  <p>i want to port this method to a flask application but when using flask with <a href="https://pythonhosted.org/Flask-Caching/" rel="nofollow noreferrer">Flask-Cache</a> when i do <code>cache.set(key,saml_msg)</code> i get error:</p>
  
  <pre><code>TypeError: can't pickle CompiledFFI objects
  </code></pre>
  
  <p>I gues its because <code>saml_msg</code> contains a key </p>
  
  <pre><code>req_info': &lt;saml2.request.AuthnRequest object at 0x1072302d0&gt;
  </code></pre>
  
  <p>How can i store state between multiple requests in flask other than using FLask-Cache?</p>
  
  <p><strong>EDIT:</strong>. </p>
  
  <p>this question is similar to <a href="https://stackoverflow.com/questions/19277280/preserving-global-state-in-a-flask-application">this question</a>  but I have already tried that solution .I need to save an <code>object</code> not a simple data type so when i save it using Flask-Cache i get  `can't pickle CompiledFFI objects(maybe this is something with <a href="https://github.com/IdentityPython/pysaml2/blob/b224a021960a65d6495580f2b94734a530d62d90/src/saml2/request.py#L157" rel="nofollow noreferrer">AuthnRequest</a> object ,it can't be pickled)</p>
  
  <p>Using <code>flask.session</code> to store key between requests, it gives <code>TypeError: Object of type AuthnRequest is not JSON serializable</code>.</p>
  
  <p>full stack trace.</p>
  
  <pre class="lang-py prettyprint-override"><code>Traceback (most recent call last):
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
      return self.wsgi_app(environ, start_response)
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
      response = self.handle_exception(e)
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
      raise value
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
      response = self.full_dispatch_request()
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
      return self.finalize_request(rv)
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1969, in finalize_request
      response = self.process_response(response)
    File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2268, in process_response
      self.session_interface.save_session(self, ctx.session, response)
    File "/usr/local/lib/python3.7/site-packages/flask/sessions.py", line 378, in save_session
      val = self.get_signing_serializer(app).dumps(dict(session))
    File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 166, in dumps
      payload = want_bytes(self.dump_payload(obj))
    File "/usr/local/lib/python3.7/site-packages/itsdangerous/url_safe.py", line 42, in dump_payload
      json = super(URLSafeSerializerMixin, self).dump_payload(obj)
    File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 133, in dump_payload
      return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
    File "/usr/local/lib/python3.7/site-packages/flask/json/tag.py", line 305, in dumps
      return dumps(self.tag(value), separators=(",", ":"))
    File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 211, in dumps
      rv = _json.dumps(obj, **kwargs)
    File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 238, in dumps
      **kw).encode(obj)
    File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 199, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 257, in iterencode
      return _iterencode(o, 0)
    File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 100, in default
      return _json.JSONEncoder.default(self, o)
    File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
      raise TypeError(f'Object of type {o.__class__.__name__} '
  TypeError: Object of type AuthnRequest is not JSON serializable
  </code></pre>