code :
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
engine = create_engine('mysql://root:password@localhost/db', echo=True)
Base = declarative_base(engine)
class Accounts(Base):
__tablename__ = 'accounts'
__table_args__ = {'autoload': True}
I am trying to store sqlalchemy record object into memcache
from pymemcache.client.base import Client
client = Client(('localhost', 11211))
client.set('testkey', session.query(Users).get(1))
It is storing string object instead of User object
output : '<__main__.Users object at 0x105b69b10>'
Any help ?
Thanks advance
2
Answers
The issue isn’t so much about how to store a
SQLAlchemy
object but rather how to store any Object instance.This is from docstring of the pymemcache
Client
class that you’ve imported:You haven’t included a definition of the
Users
class that you are querying your database with so I can only assume you haven’t overridden__str__
. Therefore, whenpymemcache
tries to convert your object into a byte string, it is calling python’s default__str__
implementation and storing that.As @SuperShoot mentions in their answer,
pymemcache
expects to store astr
representation. Since your SQLAlchemy model instance (record) is not natively astr
representation,pymemcache
tries to call its default__str__()
method, which produces an undesired result.What you need is an interim step that will serialize your SQLAlchemy model instance to a
str
of one structure or another. This makes your logical round-trippable flow:str
of one structure or another.str
usingpymemcache
.str
frompymemcache
, de-serialize it into a SQLAlchemy model instance to continue working easily with it in your Python code.This can be a bit complicated with SQLAlchemy models, which is why I recommend using the SQLAthanor library (full disclosure: I’m the author). It lets you serialize your SQLAlchemy model to CSV, JSON, or YAML – which you can then persist to memcache. And it also lets you easily de-serialize a CSV, JSON, or YAML string into a SQLAlchemy model instance as well, letting you easily maintain the whole flow described above.
There’s a lot more functionality to the library which you can read about on the docs page: https://sqlathanor.readthedocs.io/en/latest/
The important bit to remember is that when using SQLAthanor, you’ll need to decide what format you want to store your data in (I recommend JSON or YAML), and then explicitly configure the columns/attributes you want to have serialized to that format (this is a security feature in the library). Because your code snippet shows that you’re using Declarative Reflection, you’ll probably want to look at the following sections of the documentation for how to configure SQLAthanor:
Hope this helps!