Database Query Caching

1 view
Skip to first unread message

Tony Zakula

unread,
Sep 15, 2010, 9:57:18 AM9/15/10
to mynajs-general
Per the example in the changelog below, if we are say comparing this to Java ORM caching like Hibernate, would this be similar? It seems so.  I just want to hear your thoughts.  Since most applications that start to scale need some kind of data caching, was just curious on this.

Thanks!

Tony Z 


Queries can also be cached by including a "cache" option with the same parameters as Myna.Cache()

//cache employee information for 12 hours
var qry=new Myna.Query({
    ds:"hr_ds",
    sql:"select * from employees",
    cache:{
        refreshInterval:Date.getInterval(Date.HOUR,12)
    }
})     

Mark Porter

unread,
Sep 15, 2010, 11:37:35 AM9/15/10
to mynajs-...@googlegroups.com
What the cache option does is store the result of the query in server memory for the interval indicated. Any subsequent calls for this SQL with the same parameters will return the cached result. If you change the parameters, a new cache is created. Any similarity to hibernate is just in the fact that the result is cached in memory. 

Does that help? There are many cache options described in http://www.mynajs.org/shared/docs/js/libOO/files/Cache-sjs.html You can use any of those properties except "code" which is generated by Myna.Query


--
You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
To post to this group, send email to mynajs-...@googlegroups.com.
To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.

Tony Zakula

unread,
Sep 15, 2010, 3:19:26 PM9/15/10
to mynajs-...@googlegroups.com
Okay,  that does clarify some.  I was trying to compare whether this caching system would be as efficient as a Java native ORM like hibernate.  I see they work differently.    Not that you could not use some strategy to keep from going to the database.  Depending on the system, hibernate cab chew up a lot of ram to keep from going to the database.  The age old answer applies, which is the right tool,  it all depends.  :-)

Thanks!

Mark Porter

unread,
Sep 15, 2010, 4:04:43 PM9/15/10
to mynajs-...@googlegroups.com
Efficient how? In both cases you skip a trip to the DB if the value is in the cache.

ORM caching is different than query caching. I am looking into ORM caching as well. The trick with ORM caching is knowing when someone else has modified an object. This is impossible if people are hitting the DB directly and changing tables. It is also challenging if you are running multiple app servers as they need to co-ordinate cache flushing. With Myna's recent cluster updates I think it might be possible for app servers to broadcast that they are updating an object and all members of the cluster could flush the cache of that object.

----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

Tony Zakula

unread,
Sep 15, 2010, 4:22:28 PM9/15/10
to mynajs-...@googlegroups.com
Okay, I am not an expert, but it was my impression that if you turn on hibernates second level cache that it will cache all the data.  So lets say you have a product table with 500 products and a hibernate entity object that represents that, the data of the table would be cached.  Each web page would only be displaying say maybe ten products with a  query, but there would be no trips to the database.  With query caching, you would have fifty query results cached.  Right?

Interestingly enough, many people say that the whole caching thing doesn't need to be done because the databases are so optimized now.  I also know several projects that are using Lucene for searches and displaying, so the product data is really coming straight from the Lucene index.  However, the index has to be rebuilt whenever there is an update to the original data.

Mark Porter

unread,
Sep 16, 2010, 10:05:50 AM9/16/10
to mynajs-...@googlegroups.com
Ok I was not aware of that. Caching whole tables seems a little silly, but you could do that in Myna by using in-memory H2 databases and copying whatever you need to be that fast into there. Personally, I don't cache anything. We have over 500k dynamic requests a day without any significant load. If a report or something seems to take a long time we either optimize queries or create de-normalized reporting tables. In the future, we are considering the use of NoSQL databases like Cassandra that can be distributed among the servers themselves so that  there is no network issues or single-point-of-failure for data. 

Where I see caching being more useful is in output fragment caching. If you have a web page or  a piece of a web page, that updates hourly but gets 1000 hits an hour, then caching the content pays a nice dividend without too much memory cost. If you are doing social websites where other's content shows up on your page, it makes sense to to cache that page until there is an update rather than run 10 queries and build the content every time the page is hit. 

----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org


Tony Zakula

unread,
Sep 16, 2010, 12:57:28 PM9/16/10
to mynajs-...@googlegroups.com
Yes, there are many ways to do things and everything has changed so quickly including databases.  I was just kind of curious.  Many people consider Hibernate the holy grail, but I think the game has changed quite a bit.  Although when people say they use Hibernate, it lends a certain credibility to your app since it is considered the standard. That being said, It does require quite a bit of extra work.  Thank you for the explanation.
Reply all
Reply to author
Forward
0 new messages