javascriptcachingtypeahead.jsttlbloodhound

Typeahead Bloodhoud doesn't clear the cache after TTL has timed out


I've got the following setup for Typeahead's engine Bloodhound. ALthough I've set TTL to 15 seconds, the refresh doesn't occur. I'm waiting far longer than that and do a new search but the hint method on the server doesn't get executed.

var users = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: {
    url: "/Worker/HintUser",
    cache: true,
    ttl: 15000
  },
  remote: {
    wildcard: '',
    rateLimitWait: 500,
    url: "/Worker/HintUser",
    transform: function (response) {
      return $.map(response.results, function (element) {
        return { value: element }
      });
    }
  }
});

$("#remoteFetch").typeahead(
{ hint: true, highlight: true, minLength: 1 },
{ name: 'users', source: users });

Is it a known problem with the cache? I get it to work as expected when I set the cache to false. But then it makes a call for every search defeating the point of having local to begin with.

What can I do to make Bloodhound cache the values in a few seconds and then release it into oblivion fetching new, freshy values?


Solution

  • This might be late, but i figured out how to fix this. Apparently in the typeahead js file, the authors explicitly state to use strict which is now in force. So when working with numbers or in this case milliseconds, the PersistentStorage.isExpired function needed to be modified a bit. Also important to bare in mind that the ttl is set by adding whatever time you set to the value returned by new Date().getTime() which is represented in milliseconds

    // This is the original 
    
                isExpired: function(key) {
                    var ttl = decode(this.ls.getItem(this._ttlKey(key)));
                    return _.isNumber(ttl) && now() > ttl ? true : false;
                }
    
    // And this is my modification
    
                isExpired: function( key ) {
                    var endTime = parseInt( decode(
                        this.ls.getItem(
                            this._ttlKey( key )
                        ), 10 )
                    );
    
                    var currentTime = parseInt( now(), 10 ),
                        expired = ( ( _.isNumber( endTime ) ) && ( endTime < currentTime ) ) ? true : false;
    
                    return expired;
                }