-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-service.ts
More file actions
52 lines (51 loc) · 1.77 KB
/
cache-service.ts
File metadata and controls
52 lines (51 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
export class CacheService{
cacheName="cache";
cacheSplitOp='@';
private cacheItems:{key:string,value:string}[]=[];
setCacheItem(id:string,key:string,value:string){
localStorage.setItem(this.cacheKeyGenerator(key,id),value);
}
getCacheItemByID(id:string,key:string):string{
return localStorage.getItem(key+":"+id.toString());
}
compareCacheItem(CurrentId:number,CurrentKey:string,value:string):boolean{
return localStorage.getItem(CurrentKey+this.cacheSplitOp+CurrentId.toString())==value;
}
getAllCacheByKey(key:string):any[]{
this.cacheItems=[];
for (let i = 0; i < localStorage.length; i++) {
if(localStorage.key(i).split(this.cacheSplitOp)[0]==key){
this.cacheItems.push({key:localStorage.key(i),value:localStorage.getItem(localStorage.key(i))});
}
}
return this.cacheItems;
}
clearCache(key:string){
for (let i = 0; i < localStorage.length; i++) {
if(localStorage.key(i).split(this.cacheSplitOp)[0]==key){
localStorage.removeItem(localStorage.key(i));
}
}
}
deleteItem(key:string,id:string):boolean{
let fullKey:string=this.cacheKeyGenerator(key,id);
if(localStorage.getItem(fullKey)){
localStorage.removeItem(fullKey);
return true;
}else{
return false;
}
}
cacheKeyGenerator(key:string,id:string):string{
let keyStringArr:string[]=[];
let keyString:string="";//order: [cache-name]:[key]:[id]
keyStringArr.push(this.cacheName);
keyStringArr.push(key);
keyStringArr.push(id.toString());
for (let index = 0; index < keyStringArr.length-1; index++) {
keyString += keyStringArr[index]+this.cacheSplitOp;//generate key with split operator
}
keyString += keyStringArr[keyStringArr.length-1];//for last item
return keyString;
}
}