Skip to content

Commit 8278419

Browse files
author
Tomas Kirda
committed
Improve caching logic. Make cache key to be combination of the serviceUrl and parameters.
1 parent eb95807 commit 8278419

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

spec/autocompleteBehavior.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,41 @@ describe('Autocomplete', function () {
537537

538538
expect(suggestionData).toBeNull();
539539
});
540+
541+
it('Should use serviceUrl and params as cacheKey', function () {
542+
var input = $('<input />'),
543+
instance,
544+
ajaxExecuted = false,
545+
data = { a: 1, query: 'Jam' },
546+
serviceUrl = '/autocomplete/cached/url',
547+
cacheKey = serviceUrl + '?' + $.param(data);
548+
549+
input.autocomplete({
550+
serviceUrl: serviceUrl,
551+
params: data
552+
});
553+
554+
$.mockjax({
555+
url: serviceUrl,
556+
responseTime: 5,
557+
response: function (settings) {
558+
ajaxExecuted = true;
559+
var query = settings.data.query,
560+
response = {
561+
suggestions: [{ value: 'Jamaica' }, { value: 'Jamaica' }]
562+
};
563+
this.responseText = JSON.stringify(response);
564+
}
565+
});
566+
567+
input.val('Jam');
568+
instance = input.autocomplete();
569+
instance.onValueChange();
570+
571+
waits(10);
572+
573+
runs(function () {
574+
expect(instance.cachedResponse[cacheKey]).toBeTruthy();
575+
});
576+
});
540577
});

src/jquery.autocomplete.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
that.selectedIndex = -1;
9494
that.currentValue = that.element.value;
9595
that.intervalId = 0;
96-
that.cachedResponse = [];
96+
that.cachedResponse = {};
9797
that.onChangeInterval = null;
9898
that.onChange = null;
9999
that.isLocal = false;
@@ -220,7 +220,7 @@
220220
},
221221

222222
clearCache: function () {
223-
this.cachedResponse = [];
223+
this.cachedResponse = {};
224224
this.badQueries = [];
225225
},
226226

@@ -463,32 +463,41 @@
463463
var response,
464464
that = this,
465465
options = that.options,
466-
serviceUrl = options.serviceUrl;
466+
serviceUrl = options.serviceUrl,
467+
data,
468+
cacheKey;
467469

468-
response = that.isLocal ? that.getSuggestionsLocal(q) : that.cachedResponse[q];
470+
options.params[options.paramName] = q;
471+
data = options.ignoreParams ? null : options.params;
472+
473+
if (that.isLocal) {
474+
response = that.getSuggestionsLocal(q);
475+
} else {
476+
if ($.isFunction(serviceUrl)) {
477+
serviceUrl = serviceUrl.call(that.element, q);
478+
}
479+
cacheKey = serviceUrl + '?' + $.param(data || {});
480+
response = that.cachedResponse[cacheKey];
481+
}
469482

470483
if (response && $.isArray(response.suggestions)) {
471484
that.suggestions = response.suggestions;
472485
that.suggest();
473486
} else if (!that.isBadQuery(q)) {
474-
options.params[options.paramName] = q;
475487
if (options.onSearchStart.call(that.element, options.params) === false) {
476488
return;
477489
}
478-
if ($.isFunction(options.serviceUrl)) {
479-
serviceUrl = options.serviceUrl.call(that.element, q);
480-
}
481490
if (that.currentRequest) {
482491
that.currentRequest.abort();
483492
}
484493
that.currentRequest = $.ajax({
485494
url: serviceUrl,
486-
data: options.ignoreParams ? null : options.params,
495+
data: data,
487496
type: options.type,
488497
dataType: options.dataType
489498
}).done(function (data) {
490499
that.currentRequest = null;
491-
that.processResponse(data, q);
500+
that.processResponse(data, q, cacheKey);
492501
options.onSearchComplete.call(that.element, q);
493502
}).fail(function (jqXHR, textStatus, errorThrown) {
494503
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
@@ -619,7 +628,7 @@
619628
return suggestions;
620629
},
621630

622-
processResponse: function (response, originalQuery) {
631+
processResponse: function (response, originalQuery, cacheKey) {
623632
var that = this,
624633
options = that.options,
625634
result = options.transformResult(response, originalQuery);
@@ -628,9 +637,9 @@
628637

629638
// Cache results if cache is not disabled:
630639
if (!options.noCache) {
631-
that.cachedResponse[result[options.paramName]] = result;
640+
that.cachedResponse[cacheKey] = result;
632641
if (result.suggestions.length === 0) {
633-
that.badQueries.push(result[options.paramName]);
642+
that.badQueries.push(cacheKey);
634643
}
635644
}
636645

0 commit comments

Comments
 (0)