-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caching and TTL extension #17
Comments
@maurei thoughts? |
import { Resource, ResourceType, Observables } from '@ngx-api-orm/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { Injectable } from '@angular/core';
/**
* Basic implementation of a repository
*/
export abstract class Repository<T extends Resource<Observables>> {
// The time to live for the cache
private ttl: number;
public cachedObjects: T[] = [];
/**
* Internal observable to set when updating the outward cache
*/
private cacheSource = new BehaviorSubject(null);
/**
* subscribe to this if you want to have the latest cached objects from the database
*/
public cache: Observable<T[]> = this.cacheSource.asObservable();
private firstFetch = true;
/**
* The updated objects, to still be synced with the database
*/
private updatedObjects: T[] = [];
private addedObjects: T[] = [];
private deletedObjects: T[] = [];
constructor(private modelType: ResourceType<T>) { }
/**
* Set the time to live for the cache
* @param ttl The time in second
*/
public setTtl(ttl: number) {
this.ttl = ttl;
}
public add(item: T) {
this.addedObjects.push(item);
}
/**
* Get all the items in the cache
*/
public all(): T[] {
if (this.firstFetch) {
this.modelType.fetch().subscribe(e => {
this.cachedObjects = e;
this.emit(this.cachedObjects);
});
}
return this.cachedObjects;
}
/**
*
* @param item item to update
*/
public update(item: T): void {
// see if we actually have this in cache
const index = this.cachedObjects.indexOf(this.cachedObjects.find(t => t.id === item.id));
if (index !== -1) {
this.updatedObjects.push(item);
this.cachedObjects[index] = item;
}
}
/**
* Syncs any changes with the database
*/
public sync(): void {
for (const item of this.addedObjects) {
item.save().subscribe(val => {
console.log(val);
console.log(item.id);
this.addedObjects = this.addedObjects.filter(t => t.id !== item.id);
this.cachedObjects.push(item);
this.emit(this.cachedObjects);
});
}
for (const item of this.updatedObjects) {
item.update().subscribe(() => {
this.updatedObjects.filter(t => t.id !== item.id);
const index = this.cachedObjects.indexOf(this.cachedObjects.find(t => t.id === item.id));
this.cachedObjects[index] = item;
this.addedObjects.filter(t => t.id !== item.id);
this.emit(this.cachedObjects);
});
}
for (const item of this.deletedObjects) {
this.deletedObjects = this.deletedObjects.filter(t => t.id !== item.id);
this.cachedObjects = this.cachedObjects.filter(t => t.id !== item.id);
item.delete().subscribe(() => {
this.emit(this.cachedObjects);
});
}
}
/**
*
* @param callbackfn function to execute
*/
public filter(callbackfn: any): T[] {
return this.cachedObjects.filter(callbackfn);
}
public remove(source: T) {
const index = this.cachedObjects.indexOf(source);
this.deletedObjects.push(source);
this.cachedObjects.splice(index, 1);
}
public removeRange(source: T[]) {
for (let obj of source) {
const index = this.cachedObjects.indexOf(obj);
this.cachedObjects.splice(index, 1);
}
this.deletedObjects.push(...source);
}
public emit(source: T[]) {
this.cacheSource.next(source);
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few feature requests
This can all be condensed into using a repository as an intermediary between the model and any application logic. By using predefined
Repository<TModel>
we can seperate the model (DBAL) and the Caching functinoality from each other (seperation of concerns).With an implementation in a component being
This should make apps more snappy using this package. Maybe related to #13
This usage of the repository pattern would allow for a few things
x
seconds with a full refresh force available per repositoryand a general sense of wellbeing :)
The text was updated successfully, but these errors were encountered: