Force a cache refresh

907 views
Skip to first unread message

Erick Fleming

unread,
Aug 10, 2011, 12:10:28 PM8/10/11
to ampl...@googlegroups.com
Is there a way to invalidate the cache upon subsequent requests?

amplify.request.define("someData", "ajax", {
    url: "http://example.com/api/somedata",
    dataType: "json",
    type:"GET",
    cache: "persist"
});

// something like this:

var refreshCache = true;

amplify.request('someData', function (data) {
   
}, refreshCache);

Andrew Wirick

unread,
Aug 10, 2011, 2:22:19 PM8/10/11
to ampl...@googlegroups.com
Howdy Erick,

In your scenario, I would recommend implementing a custom cache type that invalidates the cache based upon whatever criteria you have. You can easily use amplify.store within this custom cache type to do the actual storage for you.  There are a few tricks to it - I've thrown together and example - http://jsfiddle.net/vn8bn/

You'll probably want to run this with the show only view and take a look at the console:

Cheers,
Andrew

Andrew Wirick

unread,
Aug 10, 2011, 2:29:27 PM8/10/11
to ampl...@googlegroups.com
Sorry, had a syntax error - try http://jsfiddle.net/vn8bn/1/ and http://jsfiddle.net/vn8bn/1/show/

Best,

appendTo()

Andrew Wirick // Senior Trainer 
1-877-578-3794
@amWirick


1-877-JQUERY-HELP
1-303-747-6007
http://appendTo.com

 

Scott González

unread,
Aug 10, 2011, 3:00:42 PM8/10/11
to ampl...@googlegroups.com
This will also clear the cache for a specific request using the built in cache: "persist":

function clearRequestCache( resourceId ) {
    var prefix = "request-" + resourceId,
        length = prefix.length,
        type = amplify.request.resources[ resourceId ]

    $.each( amplify.store(), function( key ) {
        if ( key.substring( 0, length ) === prefix ) {
            amplify.store( key, null );
        }
    });
}

It's worth noting that this only works with cache: "persist" and won't work with the other named caches or the in-memory cache, which is why Andrew suggests building a new cache type that is meant to invalid caches.

Scott González

unread,
Aug 10, 2011, 3:48:58 PM8/10/11
to ampl...@googlegroups.com
Umm...just delete that type variable. I was trying something out and stopped when I realized it wouldn't work.


2011/8/10 Scott González <scott.g...@gmail.com>

Erick Fleming

unread,
Aug 10, 2011, 7:37:38 PM8/10/11
to ampl...@googlegroups.com
Andrew and Scott,

Both solutions were very usefull to me.  I ended up implmenting the custom cache solution, which keeps track of a list of resources that have been invalidated (ie. due to some update process).  Then when the user refreshes or navigates to a related view all works as expected.

Thanks again for the help. 

Brian Soulriser

unread,
Feb 17, 2015, 1:25:02 PM2/17/15
to ampl...@googlegroups.com
Thanks for this Scott, your solution clued me in on what I needed to implement a static method for destroying previously generated persistent caches. Cheers!
Reply all
Reply to author
Forward
0 new messages