SQLAlchemy 中的 cache_ok 标志是什么?
What's the cache_ok flag in SQLAlchemy?
我有 SQLAlchemy 的下一个自定义类型装饰器:
class TimeStamp(TypeDecorator):
impl = DateTime
cache_ok = True
LOCAL_TIMEZONE = datetime.utcnow().astimezone().tzinfo
def process_literal_param(self, value, dialect):
return self.imple.process_literal_param(value, dialect)
@property
def python_type(self):
return self.impl.python_type
def process_bind_param(self, value: datetime, dialect):
if value is None:
return None
if value.tzinfo is None:
value = value.astimezone(self.LOCAL_TIMEZONE)
return value.astimezone(timezone.utc)
def process_result_value(self, value, dialect):
if value is None:
return None
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
问题是 SQLAlchemy 要求我设置 cache_ok
标志...我试图在 docs 中阅读有关此标志的内容,但是我还不清楚。我不明白 SQLAlchemy 如何使用它。
它缓存了什么?我的类型装饰器适合缓存吗?
运行 最近发现了你的问题,没有答案 :( .. 进一步查看,查看了文档 ..
The TypeDecorator.cache_ok class-level flag indicates if this custom TypeDecorator is safe to be used as part of a cache key. This flag defaults to None which will initially generate a warning when the SQL compiler attempts to generate a cache key for a statement that uses this type. If the TypeDecorator is not guaranteed to produce the same bind/result behavior and SQL generation every time, this flag should be set to False; otherwise if the class produces the same behavior each time, it may be set to True. See TypeDecorator.cache_ok for further notes on how this works.
https://docs.sqlalchemy.org/en/14/core/custom_types.html#sqlalchemy.types.TypeDecorator
.. 因此,如果您确信您的 TypeDecorator 始终为给定值生成相同的 bind/result,那么继续并通过在您的 TypeDecorator [=] 中设置值 cache_ok = True
来发出该语句20=].
我有 SQLAlchemy 的下一个自定义类型装饰器:
class TimeStamp(TypeDecorator):
impl = DateTime
cache_ok = True
LOCAL_TIMEZONE = datetime.utcnow().astimezone().tzinfo
def process_literal_param(self, value, dialect):
return self.imple.process_literal_param(value, dialect)
@property
def python_type(self):
return self.impl.python_type
def process_bind_param(self, value: datetime, dialect):
if value is None:
return None
if value.tzinfo is None:
value = value.astimezone(self.LOCAL_TIMEZONE)
return value.astimezone(timezone.utc)
def process_result_value(self, value, dialect):
if value is None:
return None
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc)
问题是 SQLAlchemy 要求我设置 cache_ok
标志...我试图在 docs 中阅读有关此标志的内容,但是我还不清楚。我不明白 SQLAlchemy 如何使用它。
它缓存了什么?我的类型装饰器适合缓存吗?
运行 最近发现了你的问题,没有答案 :( .. 进一步查看,查看了文档 ..
The TypeDecorator.cache_ok class-level flag indicates if this custom TypeDecorator is safe to be used as part of a cache key. This flag defaults to None which will initially generate a warning when the SQL compiler attempts to generate a cache key for a statement that uses this type. If the TypeDecorator is not guaranteed to produce the same bind/result behavior and SQL generation every time, this flag should be set to False; otherwise if the class produces the same behavior each time, it may be set to True. See TypeDecorator.cache_ok for further notes on how this works.
https://docs.sqlalchemy.org/en/14/core/custom_types.html#sqlalchemy.types.TypeDecorator
.. 因此,如果您确信您的 TypeDecorator 始终为给定值生成相同的 bind/result,那么继续并通过在您的 TypeDecorator [=] 中设置值 cache_ok = True
来发出该语句20=].