-
Notifications
You must be signed in to change notification settings - Fork 12
Caching
Yossi Kolesnicov edited this page Jul 25, 2018
·
1 revision
We'll define an Entity with caching enabled:
// todo-item.entity.ts
import { Entity, EntityModelBase } from "@microsoft/paris";
@Entity({
singularName: "Todo Item",
pluralName: "Todo Items",
endpoint: "todo/items",
cache: {
time: 60000 // 1 minute
}
})
export class TodoItem extends EntityModelBase{
@EntityField()
text:string;
@EntityField()
time:Date;
}When getting a new TodoItem, Paris will fetch it from the backend:
import { Paris } from "@microsoft/paris";
const paris = new Paris();
// HTTP GET /api/todo/items/1:
paris.getItemById(TodoItem, 1).subscribe((todoItem:TodoItem) => {
console.log("Todo item with ID 1: ", todoItem);
// The next getItemById will be retrieved for cache:
paris.getItemById(TodoItem, 1).subscribe((todoItem:TodoItem) => {
console.log("I'm cached!");
});
// However, if 60000 ms have passed, the cached item is invalidated, and an HTTP request is again performed:
paris.getItemById(TodoItem, 1).subscribe((todoItem:TodoItem) => {
console.log("Again returned from backend!");
});
});Caching can also be done for all items and not time-limited:
cache: trueOr a max number of items to cache may be specified:
cache: {
max: 10
}Or a combination of max and time:
cache: {
time: 60000,
max: 10
}