I want to write web app with client Javascript and back-end server (Python). Client needs data from server frequently in AJAX way. Data in DB, and expensive to load for each request.
However, in desktop app I would just load data from DB once to memory and then access it. In web app - the server code runs each time for request so I can't do it (each run has to load from DB to memory again). How can this work? Can a single process run on server or do I have to use something different here?
An example is like auto-complete here on stackoverflow for tags - how is it implemented in the server for fast caching/loading?
I wonder if a data store like memcached is really a good approach for auto-complete? How would you represent the keys for partial matches ?
Use memcache or similar tool
Each item in the cache has a key and an expiry date and time
You need to make the key useful for your application. A typical key model pattern is Security.Domain.Query.QueryValue for sets, or Security.Domain.ID for individual objects
e.g.
ALL.Product.Q.Red is a set from the Products domain using the query Red for all users
Admin.Product.Q.Blu is a set from the Products domain using the query Blu just for Admin users
ALL.Customer.O.12345 is a single object graph from the Customer domain ID 12345 for all users
You can also add formats to the key if required.
So as your web application makes a request for data for an auto-complete box, the web service handling the call first requests the data from memcache, and if not found or expired, it only then does the expensive database query
e.g. Auto-complete to find products
and next time
The secret is in have a key model that work for all occurrences of memcache use, and doesn't generate duplicate keys for different concerns. Remember to encode delimiters in your query parameters (in the example the ".")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With