Welcome Guest, Not a member yet? Register   Sign In
Problem with AJAX and CI cache enabled: The response content is only displayed the first time
#1

[eluser]luismartin[/eluser]
I'm using the native CI output cache for the website of a manufacturing company. It has lots of product pages in which there's a section with a few tabs. When the user clicks on any tab, its content is loaded asynchronously (using jQuery) and then displayed. In this case the data returned from the server are HTML snippets and these are directly appended into the tab content.

Everything works fine on first load. The next ones nothing is displayed, until the cached file expires of course.

The cache for the ajax requests generates small documents which are not valid HTML, and they only have content like this string:

Code:
1337845520TS--->

The only thing that changes is the number. I'm totally puzzled and I don't know what to do apart from disabling the cache.

#2

[eluser]InsiteFX[/eluser]
Do not cache ajax pages!

#3

[eluser]luismartin[/eluser]
Why? Some of my ajax requests need a considerable amount of PHP processing. It would be good to spare it for multiple ajax requests for the same content.
#4

[eluser]InsiteFX[/eluser]
Then send a no-cache header for that routine!

This is a give or take!
#5

[eluser]luismartin[/eluser]
I was talking about server cache (CodeIgniter cache which is commonly stored in application/cache). By mentioning the no-cache header I understand you mean browser cache. This would be interesting also, but I want to prevent extra processing from multiple requests from many different users, to which the server cache has been made for.

I've temporarily disabled this cache for the requests coming from ajax, but there must be some way to properly cache (server-side) ajax requests with CI.
#6

[eluser]CroNiX[/eluser]
That text you see at the beginning of cached files is the expiration timestamp for that file. When CI retrieves a cached file it uses that value to determine if the cache is still valid and if it is it strips the timestamp out and sends it out to the browser. Personally, I don't know why they didn't just use the files last modified time and not bother with putting the timestamp within the file, but since they did, you have to work around it for ajax doing what you did by building a method that wouldn't be cached to serve your ajax requests from. Or, roll your own cache system. It's pretty easy.
#7

[eluser]luismartin[/eluser]
Hey Cronix! Thanks for your explanation. I see. Yes, that timestamp within the cached file is redundant. So, when there is a first ajax request for content X, CI creates the corresponding cache file, adding the timestamp at the beginning, but the output is not dumped into it. So in the next requests for content X (I guess whether they're ajax or not) there's no content to display. Blank.

This issue has led me to another question:
Why CI behaves this way when the request is asynchronous? Why doesn't CI dump any output html into the cached file? I'm using views to generate the output, which in turn is delivered to the controller, and this one sends the headers and echoes the output.
#8

[eluser]CroNiX[/eluser]
That doesn't seem right. When the cache file is generated it should add the timestamp AND the content at the same time. It shouldn't take a second request to add the content. Are you sure that method is working properly and outputting the correct data? Try it with caching off? (remember you still have to delete the cached files after you turn caching off because if they are still within the cache time they will be served until they expire, another thing I am not fond of).
#9

[eluser]luismartin[/eluser]
No, I think I didn't explain myself right. I didn't mean to say that's the way CI should do, but the way CI seems to be strangely doing (to not dump the output into the file at the first request, when the cache file is created).

When the cache is turned off and deleted everything works perfectly. So, what else to do?




Theme © iAndrew 2016 - Forum software by © MyBB