[PWA] 18. Clean the photo cache

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.