We cannot let photo always keep caching new data without clean the old data. If message is no display on the page anymore, we want to clean it. and also every 5 mins we want to clean the photo data.
Exportdefault functionIndexcontroller (container) { This. _container =container; This. _postsview =NewPostsview ( This. _container); This. _toastsview =NewToastsview ( This. _container); This. _lostconnectiontoast =NULL; This. _dbpromise =OpenDatabase (); This. _registerserviceworker (); This._cleanimagecache (); varIndexcontroller = This; setinterval ( function () {Indexcontroller._cleanimagecache (); }, 1000 * 60 * 5); This. _showcachedmessages (). Then (function() {indexcontroller._opensocket (); });}
_leanimagecache ():
- First, go to IDB, get all the writtrs from the DB and get the photos which we want to keep
- Then open the photo cache and check the URL of exists in the list, if not then delete it.
Indexcontroller.prototype._cleanimagecache =function () { return This. _dbpromise.then (function(db) {if(!DB)return; //todo:open the ' wittr ' object store, get all the messages, //gather all the photo URLs. // //Open the ' Wittr-content-imgs ' cache, and delete any entry //That you no longer need. varPhotostokeep = []; vartx = Db.transaction (' Wittrs '); returnTx.objectstore (' Wittrs '). GetAll (). Then (function(Messages) {Messages.foreach (function(message) {if(Message.photo) {Photostokeep.push (Message.photo); } }); returnCaches.open (' Wittr-content-imgs '); }). Then (function(cache) {returnCache.keys (). Then (function(requests) {Requests.foreach (function(Request) {varURL =NewURL (Request.url); if(!photostokeep.includes (Url.pathname)) {Cache.Delete(Request); } }) }) }); });};
[PWA] 18. Clean the photo cache