Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"name": "Paritosh Patel",
"email": "13868399+paritosh64ce@users.noreply.github.com",
"url": "https://simplyfyingtechblog.wordpress.com"
},
{
"name": "Alexandre Moore",
"url": "https://github.com/alexandremoore"
}
],
"repository": {
Expand Down
14 changes: 14 additions & 0 deletions src/services/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const DEFAULT_ENABLED_STORAGE = CacheStoragesEnum.MEMORY;

@Injectable()
export class CacheService {
/**
* The number of data items stored in the storage
*/
public get length(): number {
return this._storage ? this._storage.length : 0 ;
}

/**
* Default cache options
Expand Down Expand Up @@ -147,6 +153,14 @@ export class CacheService {
this._prefix = prefix;
}

/**
* Get the name of the nth key in the storage
* @param index
*/
public key(index: number): string | null {
return this._storage.key(index);
}

/**
* Validate cache storage
*/
Expand Down
10 changes: 10 additions & 0 deletions src/services/storage/cache-storage-abstract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {StorageValueInterface} from '../../interfaces/storage-value.interface';
* Abstract cache storage
*/
export abstract class CacheStorageAbstract {
/**
* The number of data items stored in the storage
*/
public abstract readonly length: number;

/**
* Get item from storage
Expand Down Expand Up @@ -40,4 +44,10 @@ export abstract class CacheStorageAbstract {
*/
public abstract isEnabled(): boolean;

/**
* Get the name of the nth key in the storage
* @param index
*/
public abstract key(index: number): string | null;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {StorageValueInterface} from '../../../interfaces/storage-value.interface
*/
@Injectable()
export class CacheLocalStorage extends CacheStorageAbstract {
public get length(): number {
return localStorage ? localStorage.length : 0 ;
}

public getItem(key: string) {
const value = localStorage.getItem(key);
Expand Down Expand Up @@ -44,4 +47,8 @@ export class CacheLocalStorage extends CacheStorageAbstract {
return false;
}
}

public key(index: number): string | null {
return localStorage.key(index);
}
}
8 changes: 8 additions & 0 deletions src/services/storage/memory/cache-memory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class CacheMemoryStorage extends CacheStorageAbstract {

private _data: {[key: string]: any} = {};

public get length(): number {
return (this._data && Object.keys(this._data)) ? Object.keys(this._data).length : 0 ;
}

public getItem(key: string) {
return this._data[key] ? this._data[key] : null;
}
Expand All @@ -35,4 +39,8 @@ export class CacheMemoryStorage extends CacheStorageAbstract {
public isEnabled() {
return true;
}

public key(index: number): string | null {
return (this._data && Object.keys(this._data)) ? Object.keys(this._data)[index] : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {StorageValueInterface} from '../../../interfaces/storage-value.interface
*/
@Injectable()
export class CacheSessionStorage extends CacheStorageAbstract {
public get length(): number {
return sessionStorage ? sessionStorage.length : 0 ;
}

public getItem(key: string) {
let value = sessionStorage.getItem(key);
Expand Down Expand Up @@ -44,4 +47,8 @@ export class CacheSessionStorage extends CacheStorageAbstract {
return false;
}
}

public key(index: number): string | null {
return sessionStorage.key(index);
}
}