1

Here am using , tiff reader library from client side to convert tiff file to image for user view.User will see only one at a time, provided pagination to see next/prev one, here, while user paginating each time hit server to get the tiff and render it as image on screen. see code :

var costructUrl = 'http://cdn.dmsapp.tk/'+appUrl+'/'+evt+'?authToken='+this.getuserservice.authorizationfun()+'&force=false'; xhr.open('GET', costructUrl);   xhr.responseType = 'arraybuffer';
            xhr.onload = function (e) {
            var pages = [];
            var buffer = xhr.response;
            var tiff = new Tiff({buffer: buffer});
            var len = tiff.countDirectory();
            var currentPage = index;
            if (currentPage < len) getTiff();function getTiff(){
                tiff.setDirectory(currentPage);
                var canvas = tiff.toCanvas();
                var ctx = canvas.getContext('2d');
                pages.push(canvas.toDataURL()); if ((currentPage+1 < len && currentPage < limit)) {
                    currentPage++;
                    getTiff();         // get next page
                }
                }
            }
        };xhr.send();

My question is, Is it possible to store the xhr.responce in cache, to prevent server hit every time. see pagination inside redmark

2
  • 2
    ServiceWorkers. Commented Jan 4, 2017 at 11:42
  • Yes, it's possible on modern browsers with service workers. It's non-trivial, but not hard per se... Commented Jan 4, 2017 at 11:45

1 Answer 1

1

Yes you can handle cache in javascript very easily.

You can Use localStorage to store anything to cache, It uses key value pair to store value and it will be available to all the pages of your domain. The value will persist even if the browser is closed. Unlike sessionStorage which stores value as long as the browser is open, including page reloads and restores.

To write to localStorage use

localStorage['yourKey'] = 'yourValue';

To Read data use

var stored = localStorage['yourKey'];

The value will be visible to all the pages of the same domain from which the value was written.

Yes, You can store any string value to the cache.

If you want to store a object you can use

localStorage['yourKey'] = JSON.stringify(your_obj); and store the value

and later parse the value to get object

your_obj = JSON.parse(localStorage['yourKey']);

For More Information Check Here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.