mirror of https://github.com/Squidex/squidex.git
20 changed files with 293 additions and 181 deletions
@ -0,0 +1,53 @@ |
|||||
|
/* |
||||
|
* 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 get and store item in cache', () => { |
||||
|
const localCacheService = new LocalCacheService(); |
||||
|
|
||||
|
const value = {}; |
||||
|
|
||||
|
localCacheService.set('key', value); |
||||
|
localCacheService.clear(true); |
||||
|
|
||||
|
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(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,54 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
interface Entry { value: any, expires: number }; |
||||
|
|
||||
|
export const LocalCacheServiceFactory = () => { |
||||
|
return new LocalCacheService(); |
||||
|
}; |
||||
|
|
||||
|
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 { |
||||
|
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 }; |
||||
|
} |
||||
|
|
||||
|
private static isExpired(now: number, entry: Entry): boolean { |
||||
|
return entry.expires < now; |
||||
|
} |
||||
|
} |
||||
@ -1,24 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
export class RoutingCache { |
|
||||
private readonly entries: { [key: string]: { value: any, inserted: number } } = {}; |
|
||||
|
|
||||
public getValue<T>(key: string) { |
|
||||
let entry = this.entries[key]; |
|
||||
|
|
||||
if (entry && (new Date().getTime() - entry.inserted) < 100) { |
|
||||
return <T> entry.value; |
|
||||
} else { |
|
||||
return undefined; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public set<T>(key: string, value: T) { |
|
||||
this.entries[key] = { value, inserted: new Date().getTime() }; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue