touch()
The touch() method marks one or more memoized objects as modified, even if their underlying data hasn't changed. This is a write operation that will result in a new transaction upon the next commit().
It's useful for scenarios where you need to signal that an object is "dirty" for external reasons, such as triggering cache invalidation or downstream processing that depends on dbzero.select_modified().
Parameters
| Name | Type | Description |
|---|---|---|
*objects | dbzero.memoized object | The memoized object or objects to be touched. |
Returns
This method does not return a value (None).
Side Effects
- Marks the provided objects as modified, making them part of the next transaction.
- Because it's a write operation, calling
dbzero.commit()aftertouch()will increment the database state number. - Touched objects will be detected by
dbzero.select_modified().
Example
Here's how to use touch() to mark an object as modified and see how it affects the database state.
import dbzero as db0
@db0.memo()
class Task:
def __init__(self, description):
self.description = description
# Create and commit an initial object
task = Task("Initial task")
db0.commit()
snap_1 = db0.snapshot()
# Touching the object doesn't change its attributes,
# but it flags it as modified.
db0.touch(task)
db0.commit()
snap_2 = db0.snapshot()
# The touch operation created a new transaction,
# so the object appears in select_modified()
modified_tasks = db0.select_modified(db0.find(Task), snap_1, snap_2)
assert len(modified_tasks) == 1
assert modified_tasks[0].description == "Initial task"You can also touch multiple objects in a single call:
task_1 = Task("First task")
task_2 = Task("Second task")
# Mark both objects as modified in one go
db0.touch(task_1, task_2)
db0.commit()