Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

caching in memory on server

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 ?

like image 872
zaharpopov Avatar asked Mar 19 '10 15:03

zaharpopov


1 Answers

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

  1. Request: http://server.com/product?q=gre&format=json
  2. Server generates memcache key ALL.Product.Name.gre.json
  3. Memcache query fails
  4. Generate SQL query Select ID, Name, Price From Product Where Name Like 'gre%'
  5. Format result into Json
  6. Save result into memcache
  7. Return result to browser

and next time

  1. Request: http://server.com/product?q=gre&format=json
  2. Server generates memcache key ALL.Product.Name.gre.json
  3. Memcache query succeeds
  4. Return result to browser

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 ".")

like image 178
TFD Avatar answered Sep 30 '22 02:09

TFD