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
- Request: http://server.com/product?q=gre&format=json
- Server generates memcache key ALL.Product.Name.gre.json
- Memcache query fails
- Generate SQL query Select ID, Name, Price From Product Where Name Like 'gre%'
- Format result into Json
- Save result into memcache
- Return result to browser
and next time
- Request: http://server.com/product?q=gre&format=json
- Server generates memcache key ALL.Product.Name.gre.json
- Memcache query succeeds
- 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 ".")