3

I am using Drupal 6, and question is regarding autocomplete. As a part of performance, I am caching the results of AJAX calls, so that if the same values are passed, we dont have to fire ajax call again, and should get the result instantly.

Scenario I have 2 autocomplete fields, one coming from Drupal taxonomy module, for tags and the other is a custom autocomplete field added via form alter, fetching some other information from custom table.

While I was checking on the Drupal default autocomplete field for tags, I saw that the if I insert the same text, the ajax is not fired. Like I insert "I", I get some values, change it to "j", I get some other values, If I change it back to "I", NO ajax is fired. However, for my custom autocomplete field, i am able to see 3 ajax calls.

Can someone please put some light as to how drupal does it, so that even i can implement the same on our custom field as well. I am using drupal 6, but if you are aware of how it is done in Drupal 7 as well, it would be a great help.

2
  • Did you tried with caching disabled? Jan 19, 2013 at 4:17
  • @subhojit777 If you are stating the caching in the Performance section, Yes this is disabled. Is there any other form of caching as well, apart from this, like caching for AJAX ... Jan 19, 2013 at 7:52

1 Answer 1

2

The basic idea behind this is to simply cache the AJAX response of an autocomplete query keyed by the query string itself. Eg.

q = 'I',
cache miss
cache['I'] => [ 'India', 'Indonesia', 'Italy', 'Iceland' ]
[ 'India', 'Indonesia', 'Italy', 'Iceland' ]

q = 'In'
cache miss
cache['In'] => [ 'India', 'Indonesia' ]

q = 'I'
cache hit
[ 'India', 'Indonesia', 'Italy', 'Iceland' ]

If you inspect misc/autocomplete.js in Drupal 7, you'd notice that there is a Drupal.ACDB (Autocomplete Database for those curious) object. What's more interesting is the search() method implementation in the same object. You'd notice that given a query string, first the in-memory cache object is looked up first before an AJAX request is made. This is something that you might want to emulate too.

4
  • Thanks @Amarnath Ravikumar This helped me in understanding how this works. But do you have any idea, how do we implement the same, Even after using the Form API, I dont see a way how can we achieve this. Jan 19, 2013 at 14:30
  • It depends on how you have implemented your custom autocomplete widget, I think. Is it using a Javascript autocomplete plugin like jQuery UI, the Drupal #autocomplete_path key with a text field or using custom Javascript code? Jan 19, 2013 at 15:40
  • I am using the default FormAPI, Using the standards which are followed in Drupal. Infact, I also tried copy pasting the code written in core, but no luck, still the ajax gets fired multiple times. Jan 20, 2013 at 3:46
  • Has any one overcome this issue of autocomplete caching ?Can any one point me to right direction with this drupal.stackexchange.com/questions/146624/…
    – Ram
    Feb 10, 2015 at 7:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.