Skip to content

Commit d9de578

Browse files
Jo-Byrjourdain
authored andcommitted
fix(cache): clean cache on object deletion
Remove cached properties of deleted objects to reduce cache size and prevent cache corruption
1 parent e3169d8 commit d9de578

3 files changed

Lines changed: 54 additions & 36 deletions

File tree

src/trame_vtk/modules/common/serve/trame-vtk.js

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/trame_vtk/modules/vtk/serializers/cache.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
PROP_CACHE = {}
22

33

4+
def remove_from_cache(obj_id):
5+
if obj_id in PROP_CACHE:
6+
del PROP_CACHE[obj_id]
7+
8+
49
def get_cached_property(obj_id, prop):
510
return PROP_CACHE.get(obj_id, {}).get(prop, None)
611

src/trame_vtk/modules/vtk/serializers/serialize.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
2+
import vtk
23

4+
from .cache import remove_from_cache
35
from .registry import class_name, SERIALIZERS
46
from .widgets import handle_widget
57

@@ -9,11 +11,17 @@
911

1012
# Keep track of which warnings have been printed
1113
NO_SERIALIZER_FOR_INSTANCE = {}
14+
DELETE_CALLBACKS = []
1215

1316

1417
def serialize(parent, instance, instance_id, context, depth):
1518
instance_type = class_name(instance)
1619
serializer = SERIALIZERS[instance_type] if instance_type in SERIALIZERS else None
20+
if instance_id not in DELETE_CALLBACKS:
21+
instance.AddObserver(
22+
vtk.vtkCommand.DeleteEvent, lambda *a, **k: remove(instance_id)
23+
)
24+
DELETE_CALLBACKS.append(instance_id)
1725

1826
if serializer:
1927
return serializer(parent, instance, instance_id, context, depth)
@@ -26,5 +34,10 @@ def serialize(parent, instance, instance_id, context, depth):
2634
return None
2735

2836

37+
def remove(instance_id):
38+
remove_from_cache(instance_id)
39+
DELETE_CALLBACKS.remove(instance_id)
40+
41+
2942
def serialize_widget(dict_out, widget):
3043
handle_widget(dict_out, widget)

0 commit comments

Comments
 (0)