Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is really two questions. Im doing a project for the university for storing baseball players statitics, but from baseball data I have to calculate the score by year for the player who is beign displayed. The background is, lets say 10, 000 users hit the player "Alex Rodriguez", the application have to calculate 10, 000 the A-Rod stats by years intead of just read it from some where is temporal saved. Here I go:

  1. What is the best method for caching this type of data? Do I have to used the same database, and some temporal values on the same database, or create a Web Service for that?

  2. What reading about web caching so you recommend?

share|improve this question
    
Great article about key-based cache expiration here: 37signals.com/svn/posts/… –  Keith Palmer Apr 10 '12 at 13:53

3 Answers 3

up vote 5 down vote accepted

Insanity: doing the same thing over and over again and expecting different results.

Albert Einstein

Calculate it once store the result in the database.

Only purge this result if the underlying data changes.

When a user requests the statistics of a given user, fist check the cache. If one exists return this immediately, otherwise do the calculation and store it.

share|improve this answer
    
I like this approach. But I wonder If Im not violating the databases first rule: "If can be calculate, shouldnt be stored". I really dont know how you guys say it in USA. –  Jhonnytunes Apr 10 '12 at 15:39
    
If it takes more time to calculate then querying the database for stored result, store it. –  Hakan Deryal Apr 10 '12 at 15:53
2  
@Jhonnytunes Not storing calculated data is a general guideline, but there are instances when it's wrong. If the data doesn't change often or takes a significant time to build it's usually better to store the calculations. –  briddums Apr 10 '12 at 17:00
    
They are correct. Within BI and Caching systems it's quite common to store calculated data and duplicate rows/columns to avoid queries (better performance). Normalization is a guideline for better databases but you have to break it sometimes for better performance and others. Keep in mind that it's broken for a particular reason and not because of bad design. –  LordALMMa Apr 10 '12 at 20:21
  1. You could keep a cache in memory, if it's small enough. Otherwise, you can store your cache in the same data base you use to keep the primary data.

    You don't have to create a web service, unless you're using web services to access the primary data in your data base.

  2. You can start reading about data base caching here.

share|improve this answer
    
2.You can start reading about data base caching here. >>Essential. –  Jhonnytunes Apr 10 '12 at 15:36

There are lots of options depending on the technologies you are using.

My first thought is that you could pre-calculate all the stats up front and store them in the database. If the database is under your control, then you can easily add to the database. If it is 3rd party, maybe you should create your own database which uses the 3rd party one as source data.

If the system will be updated over time, you could have a nightly process that recalculated all the stats each night. This way, the data will always be uptodate.

With this, your database essentially replaces the need for in application cache.

I'm not sure if you need a web service for this. That's an architectural decision that has little to do with your question about caching data somewhere in your app (at least to me it doesn't).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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