Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

Caching issue in angularJS application

I've an admin panel which is a pure angularJS application, which uses REST api for data manipulation. REST api is built using SlimAPI framework and Laravel's Eloquent ORM.

I'm facing caching issue in admin panel. Even though if a add new content to the system, it'll not show up in the list page until and unless I clear the cache and refresh it.

I cannot ask my client to clear the cache everytime when they add new content to the system. My questions are:

  1. Is there any way to refresh the cache automatically?
  2. Is this issue happening because of ORM or because of angularJS?

I've this confusion because, when I look into network console, the response from API is also not refreshed one. It will have old data. I doubt whether angular JS using the cached responses without sending request to API.

I'm not facing this issue on my local machine, this is happening only on test & production servers, which we don't have access to debug.

In jQuery AJAX calls, we can specify whether to cache the responses and use it for future. Is there any setting similar to this in angularJS?

Response Headers Placed in the testing and production application:

Remote Address:**:**:**:**
Request URL:http://********/admin/articles/getall
Request Method:GET
Status Code:200 OK (from cache)

Response Headers

Cache-Control:max-age=2592000
Content-Length:1343
Content-Type:application/json; charset=utf-8

Response Headers in my local machine

Remote Address:[::1]:80
Request URL:http://localhost/admin/articles/getall
Request Method:GET
Status Code:200 OK

Response Headers

view source
Connection:Keep-Alive
Content-Length:10201
Content-Type:application/json; charset=utf-8
Date:Thu, 18 Jun 2015 06:01:11 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.9 (Win64) PHP/5.5.12
Status-Line:HTTP/1.1 200 OK
X-Powered-By:PHP/5.5.12

Request Headers

view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:X-token $2a$10$aba34954ce887ad829384u3jHt43CvxcvFMOW/sqAYgI6DgF8OydG
Connection:keep-alive
Host:localhost
Referer:http://localhost/admin/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
X-token:$2a$10$aba34954ce887ad829384u3jHt43CvxcvFMOW/sqAYgI6DgF8OydG
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unless you state it, angular will not cache your $http requests, so most likely the values are being cached by your server.

There are a couple of things you can do:

  1. Pass a header to the server to ignore the cache (assuming your server will react to that header appropriately)
  2. Add a random parameter to your $http request so that the server interprets your request as a new one

Answers number 1 and 2 on this question: Angular IE Caching issue for $http


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...