angularJS 应用程序中的缓存问题
Caching issue in angularJS application
我有一个管理面板,它是一个纯 angularJS 应用程序,它使用 REST api 进行数据操作。 REST api 是使用 SlimAPI 框架和 Laravel 的 Eloquent ORM 构建的。
我在管理面板中遇到缓存问题。即使向系统添加新内容,它也不会显示在列表页面中,除非我清除缓存并刷新它。
我不能要求我的客户每次向系统添加新内容时都清除缓存。我的问题是:
- 有没有办法自动刷新缓存?
- 发生这个问题是因为 ORM 还是因为 angularJS?
我之所以感到困惑,是因为当我查看网络控制台时,来自 API 的响应也没有刷新。它会有旧数据。我怀疑 angular JS 是否在不向 API.
发送请求的情况下使用缓存的响应
我在我的本地机器上没有遇到这个问题,这只发生在测试和生产服务器上,我们无权进行调试。
在jQuery AJAX 调用中,我们可以指定是否缓存响应以供将来使用。 angularJS中有类似这样的设置吗?
响应Headers 放置在测试和生产应用程序中:
Remote Address:**:**:**:**
Request URL:http://********/admin/articles/getall
Request Method:GET
Status Code:200 OK (from cache)
回复Headers
Cache-Control:max-age=2592000
Content-Length:1343
Content-Type:application/json; charset=utf-8
在我的本地机器上响应Headers
Remote Address:[::1]:80
Request URL:http://localhost/admin/articles/getall
Request Method:GET
Status Code:200 OK
回复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
请求Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:X-token a$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:a$aba34954ce887ad829384u3jHt43CvxcvFMOW/sqAYgI6DgF8OydG
除非您声明,angular 不会缓存您的 $http
请求,因此很可能这些值正在被您的服务器缓存。
您可以做几件事:
- 将 header 传递给服务器以忽略缓存(假设您的服务器将对此 header 做出适当的反应)
- 向您的 $http 请求添加一个随机参数,以便服务器将您的请求解释为一个新请求
关于这个问题的第 1 和第 2 个答案:Angular IE Caching issue for $http
在我的 PHP 代码中添加了以下 header 以强制清除缓存:
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");//Dont cache
header("Pragma: no-cache");//Dont cache
header("Expires: " . date('D, d M Y H:i:s'));
还在 angularJS 中添加了以下代码:
您可以添加一个拦截器来生成唯一请求url。您也可以删除 console.log 个电话
myModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('noCacheInterceptor');
}]).factory('noCacheInterceptor', function () {
return {
request: function (config) {
console.log(config.method);
console.log(config.url);
if(config.method=='GET'){
var separator = config.url.indexOf('?') === -1 ? '?' : '&';
config.url = config.url+separator+'noCache=' + new Date().getTime();
}
console.log(config.method);
console.log(config.url);
return config;
}
};
});
我有一个管理面板,它是一个纯 angularJS 应用程序,它使用 REST api 进行数据操作。 REST api 是使用 SlimAPI 框架和 Laravel 的 Eloquent ORM 构建的。
我在管理面板中遇到缓存问题。即使向系统添加新内容,它也不会显示在列表页面中,除非我清除缓存并刷新它。
我不能要求我的客户每次向系统添加新内容时都清除缓存。我的问题是:
- 有没有办法自动刷新缓存?
- 发生这个问题是因为 ORM 还是因为 angularJS?
我之所以感到困惑,是因为当我查看网络控制台时,来自 API 的响应也没有刷新。它会有旧数据。我怀疑 angular JS 是否在不向 API.
发送请求的情况下使用缓存的响应我在我的本地机器上没有遇到这个问题,这只发生在测试和生产服务器上,我们无权进行调试。
在jQuery AJAX 调用中,我们可以指定是否缓存响应以供将来使用。 angularJS中有类似这样的设置吗?
响应Headers 放置在测试和生产应用程序中:
Remote Address:**:**:**:**
Request URL:http://********/admin/articles/getall
Request Method:GET
Status Code:200 OK (from cache)
回复Headers
Cache-Control:max-age=2592000
Content-Length:1343
Content-Type:application/json; charset=utf-8
在我的本地机器上响应Headers
Remote Address:[::1]:80
Request URL:http://localhost/admin/articles/getall
Request Method:GET
Status Code:200 OK
回复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
请求Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:X-token a$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:a$aba34954ce887ad829384u3jHt43CvxcvFMOW/sqAYgI6DgF8OydG
除非您声明,angular 不会缓存您的 $http
请求,因此很可能这些值正在被您的服务器缓存。
您可以做几件事:
- 将 header 传递给服务器以忽略缓存(假设您的服务器将对此 header 做出适当的反应)
- 向您的 $http 请求添加一个随机参数,以便服务器将您的请求解释为一个新请求
关于这个问题的第 1 和第 2 个答案:Angular IE Caching issue for $http
在我的 PHP 代码中添加了以下 header 以强制清除缓存:
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");//Dont cache
header("Pragma: no-cache");//Dont cache
header("Expires: " . date('D, d M Y H:i:s'));
还在 angularJS 中添加了以下代码:
您可以添加一个拦截器来生成唯一请求url。您也可以删除 console.log 个电话
myModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('noCacheInterceptor');
}]).factory('noCacheInterceptor', function () {
return {
request: function (config) {
console.log(config.method);
console.log(config.url);
if(config.method=='GET'){
var separator = config.url.indexOf('?') === -1 ? '?' : '&';
config.url = config.url+separator+'noCache=' + new Date().getTime();
}
console.log(config.method);
console.log(config.url);
return config;
}
};
});