私の要件は、サーバーから毎回要求するのではなく、ブラウザレベルでいくつかのメタデータを再利用することです。私の休暇の終点は春のブートを使って書かれています。私は自分のレスポンスにCache-controlとmax-ageのヘッダを追加しました。私は最大年齢を10秒に設定しました。私の理解によれば、私たちはcssやjsファイルのような固定資産に対してこれを行うことができます。オブジェクト(レスポンス)で行うことは可能ですか?スプリングブートコントローラでキャッシュ制御を使用して応答をキャッシュすることはできますか?
@CrossOrigin
@RequestMapping(value = "/retrieve/{name}", method = RequestMethod.GET)
public ResponseEntity<Course> getCourses(@PathVariable String name) {
Course course = new Course();
HttpHeaders headers = new HttpHeaders();
try {
System.out.println("Call is here.");
course = courseService.getCourse(name);
headers.add("Cache-control", "private");
headers.add("max-age", "10");
} catch (Exception e) {
return new ResponseEntity<>(course, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(course, headers, HttpStatus.OK);
}
フロントエンドレベルでは依然としてリクエストはサーバーに送信され、新しい応答を返します。クライアント側のフレームワークとしてangualrを使用しています。レスポンスヘッダーに記載されているmax-ageの値を入力した後で、応答データをクリアします。
これは、関連する角度成分です。
app.component('searchCourseComponent', {
bindings: {
name: '<',
},
templateUrl: 'components/course-search/course-search.component.html',
controller: function (localStorageService, CourseService) {
var vm = this;
vm.searchCourseByName = function (name) {
if (localStorageService.isSupported) {
console.log("localStrage is supporting in this browser");
var course = CourseService.searchCourse(name).then(function (response) {
localStorageService.set("course", response.data);
});
}
else {
console.log("Local storage is not supporting");
}
};
}
});
私は、私の角張ったアプリケーションで同じことをしました。しかし、それはブラウザに値を保存することができます。しかし、私は再び要求を送信すると、サーバーに行きます。キャッシュコピーを使用しません。それが問題だ。 –
よりわかりやすいように角度コードを共有できます –
私はコードを追加しました。 –