jeancroy / FuzzySearch

:mag: Fast autocomplete suggestion engine using approximate string matching
MIT License
194 stars 32 forks source link

intelligently caching the JSON data #36

Closed halukkaramete closed 2 years ago

halukkaramete commented 2 years ago

For setting the source and initializing the fuzzyhound, we do this.


function setsource(url, keys, output) {

    $.getJSON(url).then(function (response) {
        fuzzyhound.setOptions({
            source: response,
            keys: keys,
            output_limit: 200,  // Return up to N result, 0 to disable
            //output_map: output,
            output_map: function(root){ final_item = removeTags(root.item); console.log (final_item); return final_item; },    
        })
    });
}

```Let's assume our url is this 

`blah.blah.com/blah.js 
`
And we have also this:  

`$.ajaxSetup({cache: true});
`
This is great for efficiency but, what if the blah.js is changed on the server, ( say a spelling error has been fixed in it ), how do you make sure people who already cached the blah.js earlier in their browser gets the latest and greatest blah.js? 

Would the following trick work?

`url = 'blah.blah.com/blah.js?version=' .getFileModificationDate("... /blah.js");
`
So whenever the file changes, I can change the version number on the URL of the setSource, either programmatically in an unattended fashion or manually. 

In that case, would  "$.ajaxSetup({cache: true}); " still be serving the older file? 

Or is there a better way to address this intelligent caching?
jeancroy commented 2 years ago

The ?version= is the right idea. I usually put a hash of the content as the version instead of a date. That way you know if it truly changed. Computing hash from file io can be expensive so you can have a cache of that on the server.

I think cache: true will still respect downloading new version. (New url = new file)

halukkaramete commented 2 years ago

I can confirm that attaching the version DEFINITELY works and cache:true does not cause any problem. cache: true still respects the version number. If the version number did not change, then cache:true allows you to read content from cache. If the number has changed, I do notice the delay of downloading the requested content.

setsource("https://... blah.blah.js?version=<?php echo blp_getFileModifDate('/...blah...blah.js');?>"); is the best way to handle it.

function blp_getFileModifDate($filePath) {
            // you can hit this with the  f:// start or / start. 
            // it can resolve the file name either way. 

            if ( blp_startsWith($filePath,'/')) {
                $filePath = YOUR_SERVER_ROOTPATH_HERE_WITHOUT_THE_FINAL_SLASH . $filePath;
                if ( !file_exists($filePath)) {
                    return 0;
                }
            } else {
                if ( !file_exists($filePath)) {
                    return 0;
                }
            }
            // if we are here, that means the file name has been resolved and found. 
            return filemtime($filePath);

        }

The next improvement would be to deploy some sort of the gzip'ping on the fly, which I will explore next. And this SO may have the answer: https://stackoverflow.com/questions/6478304/zip-a-json-object-before-sending-in-php