javascript - How to cache results for autosuggest component? -


i have ui autosuggest component performs ajax request user types. example, if user types mel, response be:

{   suggestions: [{     id: 18,     suggestion: 'melbourne'   }, {     id: 7,     suggestion: 'east melbourne'   }, {     id: 123,     suggestion: 'north melbourne'   }] } 

the ui component implements client side caching. so, if user clicks b (results melb retrieved), , backspace, browser has results mel in memory, available. in other words, every client makes @ 1 ajax call every given input.

now, i'd add server side caching on top of this. so, if 1 client performs ajax call mel, , let's there heavy computation going on prepare response, other clients getting results without executing heavy computation again.

i have hash of queries , results, i'm not sure optimal way achieve (memory concerns). there ~20000 suggestions in data set.

what best way implement server side caching?

you implement simple cache lru (least used) discard algorithm. basically, set few thresholds (for example: 100,000 items, 1 gb) , discard least used item (i.e., item in cache last accessed longer ago of other ones). works pretty well, , i'm sure can use an existing node.js package out there.

if you're going building service has multiple frontend servers, might easier , simpler set memcached on server (or put on frontend server if have relatively low load on server). it's got extremely simple tcp/ip protocol , there's memcached clients available node.js.

memcached easy set , scale long time. keeping cache on separate servers has potential benefit of speeding requests frontend instances, ones have not received particular request before.

no matter choose do, recommend keeping caching out of process serves requests. makes easy kill cache if have caching issues or need free memory reason.


Comments