Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Prevent Corona from caching my network requests ON GET
Started by diego_martinez93 Mar 25 2019 12:55 PM

- - - - -
7 replies to this topic

Best Answer Rob Miracle , 25 March 2019 - 02:22 PM

Add a key-value pair to the end of your URL that has a random number or increasing sequential number that makes the URL unique.

 

For instance:

"https://mysite.com/some/URL?cb=" .. tostring(math.random(9999999))

If your URL already has key-value pairs, it would be more like:

"https://mysite.com/some/URL?someParam=fred&someOtherParam=barney&cb=" .. tostring(math.random(9999999))

BTW:  cb is short for "cache busting". You could use anything like "george=jetson=", but cb is short, easy to remember and somewhat "standard"

 

Most good cache busting schemes will use a more unique value than a random number which could possibly generate the same URL. Random numbers used for simplicity.

You can also set various HTTP headers that can control cache as well, though it's mostly something the backend manages. Caching is a good thing as it prevents you from downloading something you already have. It prevents you overwhelming their services and you should try and honor their caching requests. Unfortunately servers are frequently mis-configured.

 

Rob

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

diego_martinez93

[GLOBAL: userInfoPane.html]
diego_martinez93
  • Observer

  • 14 posts
  • Corona SDK

hi, i have a problem with mi request GET and cache. Any ideas on how to disable caching properly?. thanks



[TOPIC: post.html]
#2

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Corona Geek

  • 1,130 posts
  • Corona SDK

I am fairly sure Corona is not caching anything unless it is instructed from the service or you are re-using the same object. So please post more of your code so we can figure out if it is on the lua side and check your server to make sure that it isn't set to cache data (the server part I have no idea how to fix).


  • diego_martinez93 likes this

[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,646 posts
  • Enterprise

  Best Answer

Add a key-value pair to the end of your URL that has a random number or increasing sequential number that makes the URL unique.

 

For instance:

"https://mysite.com/some/URL?cb=" .. tostring(math.random(9999999))

If your URL already has key-value pairs, it would be more like:

"https://mysite.com/some/URL?someParam=fred&someOtherParam=barney&cb=" .. tostring(math.random(9999999))

BTW:  cb is short for "cache busting". You could use anything like "george=jetson=", but cb is short, easy to remember and somewhat "standard"

 

Most good cache busting schemes will use a more unique value than a random number which could possibly generate the same URL. Random numbers used for simplicity.

You can also set various HTTP headers that can control cache as well, though it's mostly something the backend manages. Caching is a good thing as it prevents you from downloading something you already have. It prevents you overwhelming their services and you should try and honor their caching requests. Unfortunately servers are frequently mis-configured.

 

Rob


  • diego_martinez93 likes this

[TOPIC: post.html]
#4

diego_martinez93

[GLOBAL: userInfoPane.html]
diego_martinez93
  • Observer

  • 14 posts
  • Corona SDK

  function db.download_lotNumber()

    local URL = IP.."/app1Api/LotNumber"
    local function networkListener( event )
        if (event.isError) then
        else
            decodedData = json.decode(event.response)
            if(decodedData.error ~= nil) then
            else
                for k,v in pairs(decodedData) do
                end
            end
        end
    end
    local headers = {}
    headers["Cache-Control"] = "no-cache"
    local params = {}
    params.headers = headers
    network.request(URL, "GET",networkListener,params)
end


[TOPIC: post.html]
#5

diego_martinez93

[GLOBAL: userInfoPane.html]
diego_martinez93
  • Observer

  • 14 posts
  • Corona SDK

can i use headers ["Cache-Control"] = "no-cache" ?



[TOPIC: post.html]
#6

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Corona Geek

  • 1,130 posts
  • Corona SDK

that depends on what the server is doing on the other side with that header. Did you try what Rob suggested?



[TOPIC: post.html]
#7

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 716 posts
  • Corona SDK

If you want to avoid caching and similar things, couldn't you just use POST instead of GET?

https://www.diffen.com/difference/GET-vs-POST-HTTP-Requests



[TOPIC: post.html]
#8

diego_martinez93

[GLOBAL: userInfoPane.html]
diego_martinez93
  • Observer

  • 14 posts
  • Corona SDK

Hi. I'm using the ROB suggestion. I'm doing the tests to check if it works correctly.




[topic_controls]
[/topic_controls]