@@ -1913,13 +1913,80 @@ function RemoteFunctions(config = {}) {
19131913 } ;
19141914
19151915 // image ribbon gallery cache, to store the last query and its results
1916- // then next time we can load it from cache itself instead of making a new API call
1916+ const CACHE_EXPIRY_TIME = 168 * 60 * 60 * 1000 ; // 7 days, might need to revise this...
1917+ const CACHE_MAX_IMAGES = 50 ; // max number of images that we store in the localStorage
19171918 const _imageGalleryCache = {
1918- currentQuery : null ,
1919- allImages : [ ] ,
1920- totalPages : 1 ,
1921- currentPage : 1 ,
1922- maxImages : 50
1919+ get currentQuery ( ) {
1920+ const data = this . _getFromStorage ( ) ;
1921+ return data ? data . currentQuery : null ;
1922+ } ,
1923+ set currentQuery ( val ) {
1924+ this . _updateStorage ( { currentQuery : val } ) ;
1925+ } ,
1926+
1927+ get allImages ( ) {
1928+ const data = this . _getFromStorage ( ) ;
1929+ return data ? data . allImages : [ ] ;
1930+ } ,
1931+ set allImages ( val ) {
1932+ this . _updateStorage ( { allImages : val } ) ;
1933+ } ,
1934+
1935+ get totalPages ( ) {
1936+ const data = this . _getFromStorage ( ) ;
1937+ return data ? data . totalPages : 1 ;
1938+ } ,
1939+ set totalPages ( val ) {
1940+ this . _updateStorage ( { totalPages : val } ) ;
1941+ } ,
1942+
1943+ get currentPage ( ) {
1944+ const data = this . _getFromStorage ( ) ;
1945+ return data ? data . currentPage : 1 ;
1946+ } ,
1947+ set currentPage ( val ) {
1948+ this . _updateStorage ( { currentPage : val } ) ;
1949+ } ,
1950+
1951+
1952+ _getFromStorage ( ) {
1953+ try {
1954+ const data = window . localStorage . getItem ( 'imageGalleryCache' ) ;
1955+ if ( ! data ) { return null ; }
1956+
1957+ const parsed = JSON . parse ( data ) ;
1958+
1959+ if ( Date . now ( ) > parsed . expires ) {
1960+ window . localStorage . removeItem ( 'imageGalleryCache' ) ;
1961+ return null ;
1962+ }
1963+
1964+ return parsed ;
1965+ } catch ( error ) {
1966+ return null ;
1967+ }
1968+ } ,
1969+
1970+ _updateStorage ( updates ) {
1971+ try {
1972+ const current = this . _getFromStorage ( ) || { } ;
1973+ const newData = {
1974+ ...current ,
1975+ ...updates ,
1976+ expires : Date . now ( ) + CACHE_EXPIRY_TIME
1977+ } ;
1978+ window . localStorage . setItem ( 'imageGalleryCache' , JSON . stringify ( newData ) ) ;
1979+ } catch ( error ) {
1980+ if ( error . name === 'QuotaExceededError' ) {
1981+ try {
1982+ window . localStorage . removeItem ( 'imageGalleryCache' ) ;
1983+ window . localStorage . setItem ( 'imageGalleryCache' , JSON . stringify ( updates ) ) ;
1984+ } catch ( retryError ) {
1985+ console . error ( 'Failed to save image cache even after clearing:' , retryError ) ;
1986+ }
1987+ }
1988+ }
1989+ }
19231990 } ;
19241991
19251992 /**
@@ -2346,7 +2413,7 @@ function RemoteFunctions(config = {}) {
23462413 } ,
23472414
23482415 _fetchFromAPI : function ( searchQuery , page , append ) {
2349- // when we fetch from API, we clear the cache and then store a fresh copy
2416+ // when we fetch from API, we clear the previous query from local storage and then store a fresh copy
23502417 if ( searchQuery !== _imageGalleryCache . currentQuery ) {
23512418 this . _clearCache ( ) ;
23522419 }
@@ -2405,11 +2472,11 @@ function RemoteFunctions(config = {}) {
24052472 _imageGalleryCache . currentPage = this . currentPage ;
24062473
24072474 if ( append ) {
2408- // Append new results to existing cache
2409- const newImages = _imageGalleryCache . allImages . concat ( data . results ) ;
2475+ const currentImages = _imageGalleryCache . allImages ;
2476+ const newImages = currentImages . concat ( data . results ) ;
24102477
2411- if ( newImages . length > _imageGalleryCache . maxImages ) { // max = 50
2412- _imageGalleryCache . allImages = newImages . slice ( - _imageGalleryCache . maxImages ) ;
2478+ if ( newImages . length > CACHE_MAX_IMAGES ) {
2479+ _imageGalleryCache . allImages = newImages . slice ( - CACHE_MAX_IMAGES ) ;
24132480 } else {
24142481 _imageGalleryCache . allImages = newImages ;
24152482 }
@@ -2420,11 +2487,11 @@ function RemoteFunctions(config = {}) {
24202487 } ,
24212488
24222489 _clearCache : function ( ) {
2423- // clear current cache when switching to new query
2424- _imageGalleryCache . currentQuery = null ;
2425- _imageGalleryCache . allImages = [ ] ;
2426- _imageGalleryCache . totalPages = 1 ;
2427- _imageGalleryCache . currentPage = 1 ;
2490+ try {
2491+ window . localStorage . removeItem ( 'imageGalleryCache' ) ;
2492+ } catch ( error ) {
2493+ console . error ( 'Failed to clear image cache:' , error ) ;
2494+ }
24282495 } ,
24292496
24302497 _updateSearchInput : function ( searchQuery ) {
@@ -2437,7 +2504,6 @@ function RemoteFunctions(config = {}) {
24372504 } ,
24382505
24392506 _loadFromCache : function ( searchQuery ) {
2440- // Check if we can load from cache for this query
24412507 if ( searchQuery === _imageGalleryCache . currentQuery && _imageGalleryCache . allImages . length > 0 ) {
24422508 this . allImages = _imageGalleryCache . allImages ;
24432509 this . totalPages = _imageGalleryCache . totalPages ;
@@ -2446,13 +2512,12 @@ function RemoteFunctions(config = {}) {
24462512 this . _renderImages ( this . allImages , false ) ;
24472513 this . _updateNavButtons ( ) ;
24482514 this . _updateSearchInput ( searchQuery ) ;
2449- return true ; // Successfully loaded from cache
2515+ return true ;
24502516 }
2451- return false ; // unable to load from cache
2517+ return false ;
24522518 } ,
24532519
24542520 _loadPageFromCache : function ( searchQuery , page ) {
2455- // check if this page is in cache
24562521 if ( searchQuery === _imageGalleryCache . currentQuery && page <= Math . ceil ( _imageGalleryCache . allImages . length / 10 ) ) {
24572522 const startIdx = ( page - 1 ) * 10 ;
24582523 const endIdx = startIdx + 10 ;
@@ -2465,7 +2530,7 @@ function RemoteFunctions(config = {}) {
24652530 this . _updateNavButtons ( ) ;
24662531 this . _isLoadingMore = false ;
24672532 this . _hideLoadingMore ( ) ;
2468- return true ; // Successfully loaded page from cache
2533+ return true ;
24692534 }
24702535 }
24712536 return false ;
0 commit comments