Recent Weblogs

Links I like

Ajax.Application.Cache

Speed is always a concern in application development, how fast can you load that data. By making smaller requests we can prioritize the data that we want to load. The idea of ajax is always having the freshest data but what if you're data model doesn't require constant server requests, and you're ready to sacrifice perfectly updated content for a more usable application. This comes in especially handy when dealing with large data sets that rarely change.

Cache Identical Requests

An ajax request is very similar to any HTTP request, you have a destination, being the URL, and you have an object of key value pairs that you send via the GET or POST methods. This object must be serialized into a string before being sent to the server, the notation im sure everyone is familiar with, "key=value&key2=value2" this is often refered to as the query string. The query string will be used as a key in our ajax cache hash!

Cache in the Hash

Javascripts "Object" is very handy because we can set any property name we want in this object, we can even set a property using the query string. This is where you may have to catch up with some ideas from the Ajax.Application blog to proceed logically in this one. As we have all of our ajax requests going through sendRequest function we can extend this function and add logic to determine whether another server request will be neccessary

To Send or not to Send

We can test for the existance of this request in the cache hash by using the query string we're about to send to the sendRequest function, if it does exist then send the value of that property in the cache hash to the callback function sent to the sendRequest function by the implementation. If the property doesn't have a value then we send the request to the server and upon receiving that request we register that query string in the cache hash to have the value of the ajax response object sent to the onComplete functions from the Ajax.Request object used in the Ajax.Application.Base class.

Wheres the constructor?

Once again i've avoided an initialize function for this class although it was very tempting due to the nature of having a cache. I have utilized some singleton style functions to handle getting and setting the hash so that it isn't necessary, i figured this would be beneficial for the implementation not to have to deal with constructing its parent.

The Code

/**
 * @author Matthew Foster
 * @date   April 24th 2007
 * @purpose    This class is an extension of the base class with added functionality to allow for transparent caching.  The idea is to save the query string as a key in a hash object
 *             and if that query string has already been sent for it will already have an ajax response and it will simply pass that immediately to the call back function
 */
Object.extend(Object.extend(Ajax.Application.Cache.prototype, Ajax.Application.Base.prototype),
                    {
                        getCache : function(){
                            
                            if(!this.requestCache)
                                this.requestCache = {};
                            
                            return this.requestCache;
                        
                        },
                        setCache : function(obj){
                        
                            Object.extend(this.requestCache, obj || {});
                        
                        },
                        sendRequest : function(dto, cb){
                            
                            var cache = this.getCache();
                            
                            var key = this.compileDTO(dto);
                            
                            if(!cache[key]){
                                var cachedCallBack = this.registerCache.bind(this, cb, key);
                                Ajax.Application.Base.prototype.sendRequest.apply(this, [key, cachedCallBack]);
                            }
                            else
                                cb(cache[key]);
                        },
                        registerCache : function(cb, key, eAja){
                            var cache = {};
                            
                            cache[key] = eAja;
                            
                            this.setCache(cache);
                            
                            cb(eAja);
                            
                        }                
                    }
            );        
					
					
					

Comments

March 17, 2008Airport

great

August 02, 2008sakthi priyan

hmm great..

August 23, 2008arog

Thanks bro

August 23, 2008demet akalın resimleri

demet akalın , demet akalın resimleri.

September 18, 2008Güncel driver download sitesi - actual driver download site

Thanks

September 27, 2008Games

Thanks

October 02, 2008çet

thank you very much.

October 29, 2008figurin

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)

November 01, 2008Bondage equipment

Great post sir.
thank you very much.

November 25, 2008türbanlı sikiş pornosu izle

Re: "Can we avoid simplistic analyses and pretending that everything is black-and-white, good-and-evil?"

November 30, 2008essay

This is really awsome post..
thanks for sharing..
regards,
hofi

December 01, 2008rihanna

Great post sir.
thank you very much.

December 02, 2008SSNLocator

Great post sir.
thank you very much.

December 06, 2008nails

great info

January 13, 2009Webhotel

The AJAX developers need to keep in mind some crucial points when they are developing and designing applications. These facts are applicable from small to large applications. Personally I believe every AJAX developer should know all these facts for developing more robust and reliable applications.

January 14, 2009Netlog

Thankss

January 16, 2009airbag tamiri

thanks

January 21, 2009almanya chat

thanks'sss

April 20, 2009Ajgar

Abe kya likha hai kuch samajh nahi aa raha hai...
how its work...???

September 17, 2009Battery

Thanks for this code its really helpful and frnds I have a site for you people who are facing laptop parts, charger, ac adapter problems http://www.notebookchargerhub.com/dell/

September 19, 2011MASONCATHRYN23

It’s not comfortable to do a lot of things at the same time! The essays writers suggest to <a href="http://www.primeessays.com">buy an essay</a> just about this good topic* to save your free time!

October 19, 2011buy essay

All have to get know lots just about this good post, because this is worth to order essay writing and buy term papers at the writing service or it is really possible to buy an essay there!

October 21, 2011home loans

The loans seem to be useful for guys, which want to ground their own career. By the way, it is comfortable to receive a short term loan.

November 01, 2011essay writing help

It's very hard to find an academic creating firm, which is dedicated to high school students' progress. However, it is available to buy custom college essays.

June 03, 2012Research and Writing

Awesome!!!

November 19, 2012John

It is so interesting. I want to know some other information about this site. So please give me this news quickly. I always will be aware of you.

Name
Site
Comment
  CAPTCHA Image
Reload Captcha Image
Captcha