Skip to content
This repository was archived by the owner on Aug 18, 2018. It is now read-only.

Commit b7b3198

Browse files
committed
store spared from httpstorage
1 parent b2b2bf4 commit b7b3198

9 files changed

Lines changed: 104 additions & 91 deletions

File tree

src/library/interfaces/service.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface IService {
1010
all(params?: IParamsCollection | Function, success?: Function, error?: Function): ICollection;
1111
delete (id: String, params?: IParamsResource | Function, success?: Function, error?: Function): void;
1212
getService<T extends IService> ():T;
13-
clearMemoryCache? (): boolean;
13+
clearCacheMemory? (): boolean;
1414
new?<T extends IResource>(): T;
15-
memorycache: ICache;
15+
cachememory: ICache;
1616
}

src/library/resource.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class Resource extends ParentResourceService implements IResource {
127127
path.applyParams(this.getService(), params);
128128
this.id && path.appendPath(this.id);
129129

130-
let resource = this.getService().memorycache.getOrCreateResource(this.type, this.id);
130+
let resource = this.getService().cachememory.getOrCreateResource(this.type, this.id);
131131
// Converter.getOrCreateResource(this.type, this.id);
132132

133133
let promise = Core.injectedServices.JsonapiHttp.exec(
@@ -141,7 +141,7 @@ export class Resource extends ParentResourceService implements IResource {
141141

142142
// foce reload cache (for example, we add a new element)
143143
if (!this.id) {
144-
this.getService().memorycache.clearAllCollections();
144+
this.getService().cachememory.clearAllCollections();
145145
}
146146

147147
// is a resource?
@@ -152,19 +152,19 @@ export class Resource extends ParentResourceService implements IResource {
152152
Si lo guardo en la caché, luego no queda bindeado con la vista
153153
Usar {{ $ctrl.service.getCachedResources() | json }}, agregar uno nuevo, editar
154154
*/
155-
// this.getService().memorycache.setResource(this);
155+
// this.getService().cachememory.setResource(this);
156156
} else if (angular.isArray(success.data.data)) {
157157
console.warn('Server return a collection when we save()', success.data.data);
158158

159159
/*
160160
we request the service again, because server maybe are giving
161161
us another type of resource (getService(resource.type))
162162
*/
163-
let tempororay_collection = this.getService().memorycache.getOrCreateCollection('justAnUpdate');
163+
let tempororay_collection = this.getService().cachememory.getOrCreateCollection('justAnUpdate');
164164
Converter.build(success.data, tempororay_collection);
165165
angular.forEach(tempororay_collection, (resource_value: IResource, key: string) => {
166-
let res = Converter.getService(resource_value.type).memorycache.resources[resource_value.id];
167-
Converter.getService(resource_value.type).memorycache.setResource(resource_value);
166+
let res = Converter.getService(resource_value.type).cachememory.resources[resource_value.id];
167+
Converter.getService(resource_value.type).cachememory.setResource(resource_value);
168168
res.id = res.id + 'x';
169169
});
170170

src/library/service.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { PathBuilder } from './services/path-builder';
77
import { UrlParamsBuilder } from './services/url-params-builder';
88
import { Converter } from './services/converter';
99
import { LocalFilter } from './services/localfilter';
10-
import { MemoryCache } from './services/memorycache';
10+
import { CacheMemory } from './services/cachememory';
1111

1212
import { IService, ISchema, IResource, ICollection, IExecParams, ICache, IParamsCollection, IParamsResource } from './interfaces';
1313

1414
export class Service extends ParentResourceService implements IService {
1515
public schema: ISchema;
16-
public memorycache: ICache;
16+
public cachememory: ICache;
1717
public type: string;
1818

1919
private path: string; // without slashes
@@ -28,7 +28,7 @@ export class Service extends ParentResourceService implements IService {
2828
throw 'Error: you are trying register --> ' + this.type + ' <-- before inject JsonapiCore somewhere, almost one time.';
2929
}
3030
// only when service is registered, not cloned object
31-
this.memorycache = new MemoryCache();
31+
this.cachememory = new CacheMemory();
3232
this.schema = angular.extend({}, Base.Schema, this.schema);
3333
return Core.me._register(this);
3434
}
@@ -79,17 +79,17 @@ export class Service extends ParentResourceService implements IService {
7979
path.appendPath(id);
8080

8181
// cache
82-
let resource = this.getService().memorycache.getOrCreateResource(this.type, id, true);
82+
let resource = this.getService().cachememory.getOrCreateResource(this.type, id, true);
8383
resource.is_loading = true;
8484
// exit if ttl is not expired
8585
let temporal_ttl = params.ttl ? params.ttl : 0;
86-
if (this.getService().memorycache.isResourceLive(id, temporal_ttl)) {
86+
if (this.getService().cachememory.isResourceLive(id, temporal_ttl)) {
8787
// we create a promise because we need return collection before
8888
// run success client function
8989
var deferred = Core.injectedServices.$q.defer();
9090
deferred.resolve(fc_success);
9191
deferred.promise.then(fc_success => {
92-
this.runFc(fc_success, 'memorycache');
92+
this.runFc(fc_success, 'cachememory');
9393
});
9494
resource.is_loading = false;
9595
return resource;
@@ -102,7 +102,7 @@ export class Service extends ParentResourceService implements IService {
102102
success => {
103103
Converter.build(success.data, resource);
104104
resource.is_loading = false;
105-
this.getService().memorycache.setResource(resource);
105+
this.getService().cachememory.setResource(resource);
106106
this.runFc(fc_success, success);
107107
},
108108
error => {
@@ -134,13 +134,13 @@ export class Service extends ParentResourceService implements IService {
134134

135135
// make request
136136
// if we remove this, dont work the same .all on same time (ej: <component /><component /><component />)
137-
let tempororay_collection = this.getService().memorycache.getOrCreateCollection(path.getForCache(), true);
137+
let tempororay_collection = this.getService().cachememory.getOrCreateCollection(path.getForCache(), true);
138138

139139
// MEMORY_CACHE
140140
let temporal_ttl = params.ttl ? params.ttl : this.schema.ttl;
141-
if (temporal_ttl >= 0 && this.getService().memorycache.isCollectionExist(path.getForCache())) {
141+
if (temporal_ttl >= 0 && this.getService().cachememory.isCollectionExist(path.getForCache())) {
142142
// get cached data and merge with temporal collection
143-
tempororay_collection.$source = 'memorycache';
143+
tempororay_collection.$source = 'cachememory';
144144

145145
// check smartfiltertype, and set on localfilter
146146
if (params.smartfilter && this.smartfiltertype === 'localfilter') {
@@ -152,13 +152,13 @@ export class Service extends ParentResourceService implements IService {
152152
tempororay_collection = localfilter.filterCollection(tempororay_collection, params.localfilter);
153153

154154
// exit if ttl is not expired
155-
if (this.getService().memorycache.isCollectionLive(path.getForCache(), temporal_ttl)) {
155+
if (this.getService().cachememory.isCollectionLive(path.getForCache(), temporal_ttl)) {
156156
// we create a promise because we need return collection before
157157
// run success client function
158158
var deferred = Core.injectedServices.$q.defer();
159159
deferred.resolve(fc_success);
160160
deferred.promise.then(fc_success => {
161-
this.runFc(fc_success, 'memorycache');
161+
this.runFc(fc_success, 'cachememory');
162162
});
163163
return tempororay_collection;
164164
}
@@ -207,7 +207,7 @@ export class Service extends ParentResourceService implements IService {
207207

208208
Converter.build(success.data, tempororay_collection);
209209

210-
this.getService().memorycache.setCollection(path.getForCache(), tempororay_collection);
210+
this.getService().cachememory.setCollection(path.getForCache(), tempororay_collection);
211211

212212
if (params.storage_ttl > 0) {
213213
Core.injectedServices.JsonapiHttpStorage.save(path.getForCache(), success.data);
@@ -247,7 +247,7 @@ export class Service extends ParentResourceService implements IService {
247247
.delete(path.get())
248248
.then(
249249
success => {
250-
this.getService().memorycache.removeResource(id);
250+
this.getService().cachememory.removeResource(id);
251251
this.runFc(fc_success, success);
252252
},
253253
error => {
@@ -263,7 +263,7 @@ export class Service extends ParentResourceService implements IService {
263263
return <T>Converter.getService(this.type);
264264
}
265265

266-
public clearMemoryCache(): boolean {
267-
return this.getService().memorycache.clearAllCollections();
266+
public clearCacheMemory(): boolean {
267+
return this.getService().cachememory.clearAllCollections();
268268
}
269269
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Base } from './base';
77
import { Converter } from './converter';
88
import { ResourceFunctions } from './resource-functions';
99

10-
export class MemoryCache implements ICache {
10+
export class CacheMemory implements ICache {
1111
private collections = {};
1212
private collections_lastupdate = {};
1313
public resources = {};
@@ -48,15 +48,15 @@ export class MemoryCache implements ICache {
4848
}
4949

5050
public getOrCreateResource(type: string, id: string, use_store = false): IResource {
51-
if (Converter.getService(type).memorycache && id in Converter.getService(type).memorycache.resources) {
52-
return Converter.getService(type).memorycache.getResource(id);
51+
if (Converter.getService(type).cachememory && id in Converter.getService(type).cachememory.resources) {
52+
return Converter.getService(type).cachememory.getResource(id);
5353
} else {
5454
let resource = Converter.getService(type).new();
5555
resource.id = id;
5656

5757
if (id && use_store) {
58-
console.log('pido al store');
59-
Converter.getService(type).memorycache.getResourceFromStore(resource);
58+
console.log('pido al cachestore');
59+
Converter.getService(type).cachememory.getResourceFromStore(resource);
6060
}
6161

6262
return resource;
@@ -97,30 +97,30 @@ export class MemoryCache implements ICache {
9797
// -------- STORE ---------------------------------
9898

9999
public getResourceFromStore(resource: IResource): void {
100-
let promise = Core.injectedServices.JsonapiHttpStorage.getObjet(resource.type + '.' + resource.id);
100+
let promise = Core.injectedServices.JsonapiCacheStore.getObjet(resource.type + '.' + resource.id);
101101
promise.then (
102102
success => {
103103
if (success) {
104-
console.log('recibí del store, actualizo');
104+
console.log('recibí del cachestore, actualizo');
105105
Converter.build({ data: success }, resource);
106106
}
107107
}
108108
);
109109
}
110110

111111
private saveResourceStore(resource: IResource) {
112-
Core.injectedServices.JsonapiHttpStorage.saveObject(
112+
Core.injectedServices.JsonapiCacheStore.saveObject(
113113
resource.type + '.' + resource.id,
114114
resource.toObject().data
115115
);
116116
}
117117

118118
private getCollectionFromStore(url:string, collection: ICollection): void {
119-
let promise = Core.injectedServices.JsonapiHttpStorage.getObjet('collection.' + url);
119+
let promise = Core.injectedServices.JsonapiCacheStore.getObjet('collection.' + url);
120120
promise.then (
121121
success => {
122122
if (success) {
123-
collection.$source = 'store';
123+
collection.$source = 'cachestore';
124124
angular.forEach(success.data, (dataresource: IDataResource) => {
125125
collection[dataresource.id] = this.getOrCreateResource(dataresource.type, dataresource.id, true);
126126
});
@@ -134,7 +134,7 @@ export class MemoryCache implements ICache {
134134
angular.forEach(collection, (resource: IResource) => {
135135
tmp.data[resource.id] = { id: resource.id, type: resource.type };
136136
});
137-
Core.injectedServices.JsonapiHttpStorage.saveObject(
137+
Core.injectedServices.JsonapiCacheStore.saveObject(
138138
'collection.' + url,
139139
tmp
140140
);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as angular from 'angular';
2+
3+
export class CacheStore {
4+
private globalstore;
5+
private allstore;
6+
7+
/** @ngInject */
8+
public constructor(
9+
protected $localForage,
10+
protected $q
11+
) {
12+
this.globalstore = $localForage.createInstance({ name: 'jsonapiglobal' });
13+
this.allstore = $localForage.createInstance({ name: 'allstore' });
14+
this.checkIfIsTimeToClean();
15+
}
16+
17+
private checkIfIsTimeToClean() {
18+
// check if is time to check cachestore
19+
this.globalstore.getItem('_lastclean_time').then(success => {
20+
if (success) {
21+
if (Date.now() >= (success.time + 12 * 3600 * 1000)) {
22+
// is time to check cachestore!
23+
this.globalstore.setItem('_lastclean_time', { time: Date.now() });
24+
this.checkAndDeleteOldElements();
25+
}
26+
} else {
27+
this.globalstore.setItem('_lastclean_time', { time: Date.now() });
28+
}
29+
});
30+
}
31+
32+
private checkAndDeleteOldElements() {
33+
this.allstore.keys().then(success => {
34+
angular.forEach(success, (key) => {
35+
// recorremos cada item y vemos si es tiempo de removerlo
36+
this.allstore.getItem(key).then(success2 => {
37+
// es tiempo de removerlo?
38+
if (Date.now() >= (success2._lastupdate_time + 12 * 3600 * 1000)) {
39+
// removemos!!
40+
this.allstore.removeItem(key);
41+
}
42+
});
43+
});
44+
});
45+
}
46+
47+
public getObjet(key: string): any /* Promise<void> */ {
48+
return this.allstore.getItem('jsonapi.' + key);
49+
}
50+
51+
public getObjets(keys: Array<string>): any /* Promise<void> */ {
52+
return this.allstore.getItem('jsonapi.' + keys[0]);
53+
}
54+
55+
public saveObject(key: string, value: Object): void {
56+
value['_lastupdate_time'] = Date.now();
57+
this.allstore.setItem('jsonapi.' + key, value);
58+
}
59+
}
60+
angular.module('Jsonapi.services').service('JsonapiCacheStore', CacheStore);

src/library/services/converter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export class Converter {
7070
}
7171

7272
let resource: IResource;
73-
if (data.id in Converter.getService(data.type).memorycache.resources) {
74-
resource = Converter.getService(data.type).memorycache.resources[data.id];
73+
if (data.id in Converter.getService(data.type).cachememory.resources) {
74+
resource = Converter.getService(data.type).cachememory.resources[data.id];
7575
} else {
76-
resource = Converter.getService(data.type).memorycache.getOrCreateResource(data.type, data.id);
76+
resource = Converter.getService(data.type).cachememory.getOrCreateResource(data.type, data.id);
7777
}
7878

7979
resource.attributes = data.attributes ? data.attributes : {};
@@ -115,7 +115,7 @@ export class Converter {
115115
for (let dataresource of collection_data_from.data) {
116116
if (!(dataresource.id in collection_dest)) {
117117
collection_dest[dataresource.id] =
118-
Converter.getService(dataresource.type).memorycache.getOrCreateResource(dataresource.type, dataresource.id);
118+
Converter.getService(dataresource.type).cachememory.getOrCreateResource(dataresource.type, dataresource.id);
119119
}
120120
Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources);
121121
new_ids[dataresource.id] = dataresource.id;

src/library/services/core-services.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as angular from 'angular';
22
import './http.service';
33
import './http-storage.service';
4+
import './cachestore.service';
45

56
export class CoreServices {
67

@@ -9,7 +10,8 @@ export class CoreServices {
910
protected JsonapiHttp,
1011
protected rsJsonapiConfig,
1112
protected $q,
12-
protected JsonapiHttpStorage
13+
protected JsonapiHttpStorage,
14+
protected JsonapiCacheStore
1315
) {
1416

1517
}

0 commit comments

Comments
 (0)