mirror of https://github.com/Squidex/squidex.git
12 changed files with 38 additions and 302 deletions
@ -1,64 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { LocalCacheService, LocalCacheServiceFactory } from './../'; |
|||
|
|||
describe('LocalCache', () => { |
|||
it('should instantiate from factory', () => { |
|||
const localCacheService = LocalCacheServiceFactory(); |
|||
|
|||
expect(localCacheService).toBeDefined(); |
|||
}); |
|||
|
|||
it('should instantiate', () => { |
|||
const localCacheService = new LocalCacheService(); |
|||
|
|||
expect(localCacheService).toBeDefined(); |
|||
}); |
|||
|
|||
it('should get and store item in cache', () => { |
|||
const localCacheService = new LocalCacheService(); |
|||
|
|||
const value = {}; |
|||
|
|||
localCacheService.set('key', value); |
|||
|
|||
expect(localCacheService.get('key')).toBe(value); |
|||
}); |
|||
|
|||
it('should not retrieve item if cleared', () => { |
|||
const localCacheService = new LocalCacheService(); |
|||
|
|||
const value = {}; |
|||
|
|||
localCacheService.set('key', value); |
|||
localCacheService.clear(true); |
|||
|
|||
expect(localCacheService.get('key')).toBeUndefined(); |
|||
}); |
|||
|
|||
it('should not retrieve item if removed', () => { |
|||
const localCacheService = new LocalCacheService(); |
|||
|
|||
const value = {}; |
|||
|
|||
localCacheService.set('key', value); |
|||
localCacheService.remove('key'); |
|||
|
|||
expect(localCacheService.get('key')).toBeUndefined(); |
|||
}); |
|||
|
|||
it('should not retrieve item if expired', () => { |
|||
const localCacheService = new LocalCacheService(); |
|||
|
|||
const value = {}; |
|||
|
|||
localCacheService.set('key', value); |
|||
|
|||
expect(localCacheService.get('key', new Date().getTime() + 400)).toBeUndefined(); |
|||
}); |
|||
}); |
|||
@ -1,61 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
|
|||
interface Entry { value: any; expires: number; } |
|||
|
|||
export const LocalCacheServiceFactory = () => { |
|||
return new LocalCacheService(); |
|||
}; |
|||
|
|||
@Injectable() |
|||
export class LocalCacheService { |
|||
private readonly entries: { [key: string]: Entry } = {}; |
|||
|
|||
public clear(force: boolean) { |
|||
const now = new Date().getTime(); |
|||
|
|||
for (let key in this.entries) { |
|||
if (this.entries.hasOwnProperty(key)) { |
|||
const entry = this.entries[key]; |
|||
|
|||
if (force || LocalCacheService.isExpired(now, entry)) { |
|||
delete this.entries[key]; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public get<T>(key: string, now?: number): T | undefined { |
|||
const entry = this.entries[key]; |
|||
|
|||
if (entry) { |
|||
now = now || new Date().getTime(); |
|||
|
|||
if (!LocalCacheService.isExpired(now, entry)) { |
|||
delete this.entries[key]; |
|||
|
|||
return <T> entry.value; |
|||
} |
|||
} |
|||
|
|||
return undefined; |
|||
} |
|||
|
|||
public set<T>(key: string, value: T, expiresIn = 100) { |
|||
this.entries[key] = { value, expires: new Date().getTime() + expiresIn }; |
|||
} |
|||
|
|||
public remove(key: string) { |
|||
delete this.entries[key]; |
|||
} |
|||
|
|||
private static isExpired(now: number, entry: Entry): boolean { |
|||
return entry.expires < now; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue