sqlalchemy object caching issue
See original GitHub issueGreetings! I ran into sqlalchemy object caching issues. From time to time I get errors like this:
" File \"/home/boot/stellar/backend/./app/v1/security/auth.py\", line 127, in get_current_user\n permissions_v2 = await service_permissions.get_permissions_by_role_cache(\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/wrapper.py\", line 272, in _call\n return await decorator(*args, **kwargs)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/decorators/cache/simple.py\", line 43, in _wrap\n await backend.set(_cache_key, result, expire=ttl)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/validation.py\", line 62, in _invalidate_middleware\n return await call(*args, key=key, **kwargs)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/wrapper.py\", line 34, in _auto_init\n return await call(*args, **kwargs)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/disable_control.py\", line 12, in _is_disable_middleware\n return await call(*args, **kwargs)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/backends/client_side.py\", line 132, in set\n return await super().set(self._prefix + key, value, *args, **kwargs)\n",
" File \"/home/boot/stellar/backend/.venv/lib/python3.10/site-packages/cashews/serialize.py\", line 107, in set\n value = pickle.dumps(value, protocol=pickle.HIGHEST_PROTOCOL, fix_imports=False)\n",
"_pickle.PicklingError: Can't pickle <function __init__ at 0x7fc386fbb7f0>: it's not the same object as sqlalchemy.orm.instrumentation.__init__\n"
I found the answer that such objects should be serialized and deserialized differently: https://docs.sqlalchemy.org/en/14/core/serializer.html
Tell me how good / bad idea it is to cache alchemy objects, and not the endpoint itself.
If this is not bad, what can you say about adding functionality that could fix this error?
Issue Analytics
- State:
- Created a year ago
- Comments:13 (8 by maintainers)
Top Results From Across the Web
Does SQLAlchemy support caching? - Stack Overflow
Caching the query object so that Python interpreter doesn't have to manually re-assemble the query string every time. These queries are called baked...
Read more >Performance - SQLAlchemy 1.4 Documentation
The caching system allows SQLAlchemy 1.4 and above to be more performant than SQLAlchemy 1.3 with regards to the time spent converting SQL...
Read more >SQLAlchemy ORM Object caching with relationships and ...
I'm using the ORM and I have a SQLAlchemy object that I load and occasionally store in a second level cache. I have...
Read more >Flask-SQLAlchemy-Caching - PyPI
A CachingQuery implementation to Flask using Flask-SQLAlchemy and Flask-Caching. To start using caching queries, you just have to replace Flask-SQLAlchemy Model ...
Read more >How to use st.cache with sqlalchemy.orm objects - Streamlit
I need some advice on how to use caching with sqlalchemy.orm . I am writing an app for data exploration. The app uses...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello,
I think objects like sqlalchemy orm is a good use case for caching, if it possible to pickle somehow. So Yep, cashews should support it ( thanks for the link), I am going to fix this issue ASAP.
released with 4.7.1