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?


4 Answers 4


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.

  • 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. Apr 10, 2012 at 15:39
  • If it takes more time to calculate then querying the database for stored result, store it. Apr 10, 2012 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, 2012 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. Apr 10, 2012 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.

  • 2.You can start reading about data base caching here. >>Essential. Apr 10, 2012 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).


First of all it is better to store the calculated (cache) results in memcache, redis or another in memory key/value storages. In memory caches blazing fast and well suited for as a cache storage.

My (and all over the developers' and Django developers) solution is Python decorators which hooks a method and runs before and/or changes behavior of hooked method.

I developed a hash method takes method name and parameters for calculating the hash for key. After that this decorator takes the result of this method and stores the calculated result as a value of key.

Since the decorator runs before the method, always checks memcache for the value corresponding the hash of parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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