@@ -232,6 +232,8 @@ impl App {
232232 self . stop_object_watch ( ) ;
233233 self . stop_log_stream ( ) ;
234234 self . stop_port_forward ( ) ;
235+ self . object_cache . clear ( ) ;
236+ self . object_cache_order . clear ( ) ;
235237 self . detail . exec_target = None ;
236238 self . detail . port_forward_target = None ;
237239 self . loading = true ;
@@ -275,9 +277,20 @@ impl App {
275277 self . stop_object_watch ( ) ;
276278 self . object_load_token = self . object_load_token . saturating_add ( 1 ) ;
277279 let token = self . object_load_token ;
280+ let cache_key = object_cache_key ( & context, & resource, namespace. clone ( ) ) ;
278281 self . loading = true ;
279- self . objects . clear ( ) ;
280- self . status = tr_format ( "Loading {resource}..." , & [ ( "{resource}" , resource. label ( ) ) ] ) ;
282+ if let Some ( objects) = self . cached_objects ( & cache_key) {
283+ let count = objects. len ( ) ;
284+ self . objects = objects;
285+ self . set_object_status ( count) ;
286+ self . status = tr_format (
287+ "Refreshing {resource}..." ,
288+ & [ ( "{resource}" , resource. label ( ) ) ] ,
289+ ) ;
290+ } else {
291+ self . objects . clear ( ) ;
292+ self . status = tr_format ( "Loading {resource}..." , & [ ( "{resource}" , resource. label ( ) ) ] ) ;
293+ }
281294 self . sync_status ( ) ;
282295 self . rebuild_object_list ( ) ;
283296 sender. oneshot_command (
@@ -682,6 +695,47 @@ impl App {
682695 . splice ( 0 , self . object_store . n_items ( ) , & items) ;
683696 }
684697
698+ pub ( super ) fn current_object_cache_key ( & self ) -> Option < ObjectCacheKey > {
699+ let context = self . selected_context . as_ref ( ) ?;
700+ let resource = self . selected_resource_kind ( ) ?;
701+ let namespace = resource
702+ . is_namespaced ( )
703+ . then ( || self . selected_namespace . clone ( ) ) ;
704+ Some ( object_cache_key ( context, resource, namespace) )
705+ }
706+
707+ pub ( super ) fn cached_objects ( & mut self , key : & ObjectCacheKey ) -> Option < Vec < ObjectSummary > > {
708+ let objects = self . object_cache . get ( key) ?. clone ( ) ;
709+ self . touch_object_cache_key ( key. clone ( ) ) ;
710+ Some ( objects)
711+ }
712+
713+ pub ( super ) fn cache_current_objects ( & mut self ) {
714+ if let Some ( key) = self . current_object_cache_key ( ) {
715+ self . cache_objects ( key, self . objects . clone ( ) ) ;
716+ }
717+ }
718+
719+ pub ( super ) fn cache_objects ( & mut self , key : ObjectCacheKey , objects : Vec < ObjectSummary > ) {
720+ self . object_cache . insert ( key. clone ( ) , objects) ;
721+ self . touch_object_cache_key ( key) ;
722+ while self . object_cache_order . len ( ) > OBJECT_CACHE_LIMIT {
723+ if let Some ( oldest) = self . object_cache_order . pop_front ( ) {
724+ self . object_cache . remove ( & oldest) ;
725+ }
726+ }
727+ }
728+
729+ pub ( super ) fn touch_object_cache_key ( & mut self , key : ObjectCacheKey ) {
730+ self . object_cache_order . retain ( |existing| existing != & key) ;
731+ self . object_cache_order . push_back ( key) ;
732+ }
733+
734+ pub ( super ) fn clear_object_cache ( & mut self ) {
735+ self . object_cache . clear ( ) ;
736+ self . object_cache_order . clear ( ) ;
737+ }
738+
685739 /// Merges one watch event into `objects` without sorting or repainting;
686740 /// both are deferred to `flush_object_list_refresh` so an event burst
687741 /// costs one refresh instead of thousands.
@@ -941,6 +995,21 @@ impl App {
941995 }
942996}
943997
998+ fn object_cache_key (
999+ context : & str ,
1000+ resource : & ResourceKind ,
1001+ namespace : Option < String > ,
1002+ ) -> ObjectCacheKey {
1003+ ObjectCacheKey {
1004+ context : context. to_owned ( ) ,
1005+ group : resource. group . clone ( ) ,
1006+ version : resource. version . clone ( ) ,
1007+ kind : resource. kind . clone ( ) ,
1008+ plural : resource. plural . clone ( ) ,
1009+ namespace,
1010+ }
1011+ }
1012+
9441013/// The object at a view position, resolved against the sorted model the
9451014/// `ColumnView` actually displays (positions differ from the backing store
9461015/// whenever a header-click sort is active).
0 commit comments