When to set "cache_ok" to True?

2,787 views
Skip to first unread message

niuji...@gmail.com

unread,
May 26, 2021, 3:07:07 AM5/26/21
to sqlalchemy
I have consistently receiving the warning: 
will not produce a cache key because the ``cache_ok`` flag is not set to True.  Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning.


After reading the documentation, I learned that the "cache_ok" class-level attribute can be set to either True or False. But the documentation is very abstract on when to use it? What is cache key? Is it for loading and caching a set of objects from the database? Or caching the query itself?

If my TypeDecorator class doesn't even have a __init__method, just "process_bind_param" and "process_result_value" two methods, do I even need to bother with this "cache_ok" setting at all?

Mike Bayer

unread,
May 26, 2021, 8:53:59 AM5/26/21
to noreply-spamdigest via sqlalchemy


On Wed, May 26, 2021, at 3:07 AM, niuji...@gmail.com wrote:
I have consistently receiving the warning: 
will not produce a cache key because the ``cache_ok`` flag is not set to True.  Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning.


After reading the documentation, I learned that the "cache_ok" class-level attribute can be set to either True or False. But the documentation is very abstract on when to use it? What is cache key? Is it for loading and caching a set of objects from the database? Or caching the query itself?

the caching system is used to cache the generated SQL and related parameter and result-handling structures for a statement object and is described at:


it is always safe to set cache_ok=False on your TypeDecorator class,  you will however lose the benefit of SQL statement caching for statements which include use of this datatype.



If my TypeDecorator class doesn't even have a __init__method, just "process_bind_param" and "process_result_value" two methods, do I even need to bother with this "cache_ok" setting at all?

If your TypeDecorator is stateless, then it's fine to set cache_ok=True.



--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Jinghui Niu

unread,
May 26, 2021, 5:39:07 PM5/26/21
to sqlal...@googlegroups.com
Thank you. Could you please also give some hint on a case where we must set this attribute to `False`?

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/zCPZKTxM6b0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/f3640072-8d16-4f59-bc67-f468c78878cb%40www.fastmail.com.

Mike Bayer

unread,
May 26, 2021, 6:04:37 PM5/26/21
to noreply-spamdigest via sqlalchemy

class TZDateTime(TypeDecorator):
    impl = DateTime
    cache_ok = True

    def process_bind_param(self, value, dialect):
        if value is not None:
            if not value.tzinfo:
                raise TypeError("tzinfo is required")
            value = value.astimezone(datetime.timezone.utc).replace(
                tzinfo=None
            )
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = value.replace(tzinfo=datetime.timezone.utc)
        return value

Reply all
Reply to author
Forward
0 new messages