Skip to content

Commit

Permalink
Merge pull request #99 from angelnikolov/update-remove
Browse files Browse the repository at this point in the history
Added update and remove methods to storage strategies.
  • Loading branch information
angelnikolov authored Mar 6, 2021
2 parents 5561ed9 + 518b1e6 commit 56160de
Show file tree
Hide file tree
Showing 67 changed files with 339 additions and 82 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,16 @@ It's also really easy to implement your own caching strategy, by extending the I
export abstract class IStorageStrategy {
abstract getAll(cacheKey: string): Array<ICachePair<any>>;
abstract add(entity: ICachePair<any>, cacheKey: string): void;
/**
* @deprecated Use update instead.
*/
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string): void;
/**
* @deprecated Use remove instead.
*/
abstract removeAtIndex(index: number, cacheKey: string): void;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract removeAll(cacheKey: string): void;
}
```
Expand Down
6 changes: 3 additions & 3 deletions cacheable.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
/**
* cache duration has expired - remove it from the cachePairs array
*/
storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
storageStrategy.remove ? storageStrategy.remove(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;
} else if (cacheConfig.slidingExpiration || GlobalCacheConfig.slidingExpiration) {
/**
* renew cache duration
*/
_foundCachePair.created = new Date();
storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
storageStrategy.update ? storageStrategy.update(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
((cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) &&
(cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) < cachePairs.length + 1)
) {
storageStrategy.removeAtIndex(0, cacheKey);
storageStrategy.remove ? storageStrategy.remove(0, cachePairs[0], cacheKey) : storageStrategy.removeAtIndex(0, cacheKey);
}
storageStrategy.add({
parameters: cacheParameters,
Expand Down
24 changes: 20 additions & 4 deletions common/DOMStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IStorageStrategy } from './IStorageStrategy';
import { ICachePair, GlobalCacheConfig } from '.';
import {IStorageStrategy} from './IStorageStrategy';
import {ICachePair, GlobalCacheConfig} from '.';
/**
* @deprecated Use {@link LocalStorageStrategy} instead.
*/
Expand Down Expand Up @@ -43,6 +43,14 @@ export class DOMStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}

remove(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey].length) {
allCachedData[cacheKey].splice(index, 1);
}
this.storeRawData(allCachedData);
}

updateAtIndex(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey][index]) {
Expand All @@ -51,6 +59,14 @@ export class DOMStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}

update(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey][index]) {
allCachedData[cacheKey][index] = entity;
}
this.storeRawData(allCachedData);
}

removeAll(cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey].length) {
Expand All @@ -59,7 +75,7 @@ export class DOMStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}

private getRawData(): { [key: string]: Array<ICachePair<any>> } {
private getRawData(): {[key: string]: Array<ICachePair<any>>} {
const data = localStorage.getItem(this.masterCacheKey);
try {
return JSON.parse(data) || {};
Expand All @@ -68,7 +84,7 @@ export class DOMStorageStrategy extends IStorageStrategy {
}
}

private storeRawData(data: { [key: string]: Array<ICachePair<any>> }): void {
private storeRawData(data: {[key: string]: Array<ICachePair<any>>}): void {
localStorage.setItem(this.masterCacheKey, JSON.stringify(data));
}
}
10 changes: 9 additions & 1 deletion common/IAsyncStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { ICachePair } from '.';
import {ICachePair} from '.';

export abstract class IAsyncStorageStrategy {
abstract getAll(cacheKey: string): Array<ICachePair<any>> | Promise<Array<ICachePair<any>>>;
abstract add(entity: ICachePair<any>, cacheKey: string): void | Promise<void>;
/**
* @deprecated Use update instead.
*/
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string): void | Promise<void>;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string): Promise<void>;
/**
* @deprecated Use remove instead.
*/
abstract removeAtIndex(index: number, cacheKey: string): void | Promise<void>;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string): Promise<void>;
abstract removeAll(cacheKey: string): void | Promise<void>;
abstract addMany(entities: ICachePair<any>[], cacheKey: string): Promise<void>;
}
10 changes: 9 additions & 1 deletion common/IStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { ICachePair } from '.';
import {ICachePair} from '.';

export abstract class IStorageStrategy {
abstract getAll(cacheKey: string): Array<ICachePair<any>>;
abstract add(entity: ICachePair<any>, cacheKey: string): void;
/**
* @deprecated Use update instead.
*/
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string): void;
/**
* @deprecated Use remove instead.
*/
abstract removeAtIndex(index: number, cacheKey: string): void;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract removeAll(cacheKey: string): void;
abstract addMany(entities: ICachePair<any>[], cacheKey: string): void;
}
11 changes: 10 additions & 1 deletion common/InMemoryStorageStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class InMemoryStorageStrategy extends IStorageStrategy {
add(cachePair: ICachePair<any>) {
this.cachePairs.push(cachePair)
};

addMany(cachePairs: ICachePair<any>[]) {
this.cachePairs = cachePairs;
};
Expand All @@ -17,6 +17,11 @@ export class InMemoryStorageStrategy extends IStorageStrategy {
Object.assign(updatee, entity);
}

update(index: number, entity: ICachePair<any>) {
const updatee = this.cachePairs[index];
Object.assign(updatee, entity);
}

getAll() {
return this.cachePairs;
};
Expand All @@ -25,6 +30,10 @@ export class InMemoryStorageStrategy extends IStorageStrategy {
this.cachePairs.splice(index, 1);
}

remove(index: number) {
this.cachePairs.splice(index, 1);
}

removeAll() {
this.cachePairs.length = 0;
}
Expand Down
25 changes: 21 additions & 4 deletions common/LocalStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IStorageStrategy } from './IStorageStrategy';
import { ICachePair, GlobalCacheConfig } from '.';
import {IStorageStrategy} from './IStorageStrategy';
import {ICachePair, GlobalCacheConfig} from '.';
export class LocalStorageStrategy extends IStorageStrategy {
private masterCacheKey: string = GlobalCacheConfig.globalCacheKey;
constructor() {
Expand Down Expand Up @@ -40,6 +40,15 @@ export class LocalStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}


remove(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey].length) {
allCachedData[cacheKey].splice(index, 1);
}
this.storeRawData(allCachedData);
}

updateAtIndex(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey][index]) {
Expand All @@ -48,6 +57,14 @@ export class LocalStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}

update(index: number, entity: any, cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey][index]) {
allCachedData[cacheKey][index] = entity;
}
this.storeRawData(allCachedData);
}

removeAll(cacheKey: string) {
const allCachedData = this.getRawData();
if (allCachedData[cacheKey] && allCachedData[cacheKey].length) {
Expand All @@ -56,7 +73,7 @@ export class LocalStorageStrategy extends IStorageStrategy {
this.storeRawData(allCachedData);
}

private getRawData(): { [key: string]: Array<ICachePair<any>> } {
private getRawData(): {[key: string]: Array<ICachePair<any>>} {
const data = localStorage.getItem(this.masterCacheKey);
try {
return JSON.parse(data) || {};
Expand All @@ -65,7 +82,7 @@ export class LocalStorageStrategy extends IStorageStrategy {
}
}

private storeRawData(data: { [key: string]: Array<ICachePair<any>> }): void {
private storeRawData(data: {[key: string]: Array<ICachePair<any>>}): void {
localStorage.setItem(this.masterCacheKey, JSON.stringify(data));
}
}
6 changes: 3 additions & 3 deletions dist/cjs/cacheable.decorator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cjs/cacheable.decorator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 56160de

Please sign in to comment.