0.11.8 • Published 13 days ago

@anchan828/nest-cache-dependency v0.11.8

Weekly downloads
559
License
MIT
Repository
github
Last release
13 days ago

@anchan828/nest-cache-dependency

npm NPM

A cache dependency module for Nest framework (node.js) https://nestjs.com/

Description

nest-cache-dependency consider dependencies. Delete those that depend on the deletion.

TODO: Write more documentation.

Installation

$ npm i --save @anchan828/nest-cache-dependency

Quick Start

  • Import module
@Module({
  imports: [CacheDependencyModule.register()],
})
export class AppModule {}

Use with controller

@Controller()
@UseInterceptors(CacheDependencyInterceptor)
export class ExampleController {
  constructor(private readonly service: ExampleService) {}

  @Get("users/:userId/items")
  @CacheKey("users/:userId/items")
  @CacheDependency<Item[]>((cacheKey: string, items: Item[], graph: CacheDependencyGraph) => {
    for (const item of items) {
      // e.g. users/1/items/2
      graph.addNode(`${cacheKey}/${item.id}`, item);
      graph.addDependency(`${cacheKey}/${item.id}`, cacheKey);
    }
  })
  public getItems(): Item[] {
    return this.service.getItems();
  }

  @Get("users/:userId/items/:itemId")
  @CacheKey("users/:userId/items/:itemId")
  public getItem(@Param("userId", ParseIntPipe) userId: number, @Param("itemId", ParseIntPipe) itemId: number): Item {
    return this.service.getItem(userId, itemId);
  }

  @Delete("users/:userId/items/:itemId")
  @ClearCacheDependencies("users/:userId/items/:itemId")
  public deleteItem(
    @Param("userId", ParseIntPipe) userId: number,
    @Param("itemId", ParseIntPipe) itemId: number,
  ): void {
    this.service.deleteItem(userId, itemId);
  }
}

Use with service

@Injectable()
export class ExampleService {
  constructor(private readonly cacheService: CacheDependencyService) {}

  private items: Item[] = Array(5)
    .fill(0)
    .map((_, index) => ({ id: index, name: `Item ${index}` }));

  public async getItems(userId: number): Promise<Item[]> {
    const cacheKey = `users/${userId}/items`;

    const cache = await this.cacheService.getCache<Item[]>(cacheKey);

    if (cache) {
      return cache;
    }

    await this.cacheService.createCacheDependencies((graph: CacheDependencyGraph) => {
      graph.addNode(cacheKey, this.items);

      for (const item of this.items) {
        graph.addNode(`${cacheKey}/${item.id}`, item);
        graph.addDependency(`${cacheKey}/${item.id}`, cacheKey);
      }
    });

    return this.items;
  }

  public async getItem(userId: number, itemId: number): Promise<Item> {
    const cacheKey = `users/${userId}/items/${itemId}`;

    const cache = await this.cacheService.getCache<Item>(cacheKey);

    if (cache) {
      return cache;
    }

    const item = this.items.find((item) => item.id === itemId);
    await this.cacheService.setCache(cacheKey, item);

    return item;
  }

  public async deleteItem(userId: number, itemId: number): Promise<void> {
    this.items = this.items.filter((item) => item.id !== itemId);
    await this.cacheService.clearCacheDependencies(`users/${userId}/items/${itemId}`);
  }
}

Versioning

Sometimes you don't want to use the old cache when you update your application.

You can set prefix string to key as version.

@Module({
  imports: [
    CacheDependencyModule.register({
      cacheDependencyVersion: "v1",
    }),
  ],
})
export class AppModule {}

If you set v1 and call cacheDependencyService.set("users/1", user), cache manager will save value to v1:users/1 key, not users/1

Using Pub/Sub

You can use Pub/Sub pattern of Redis. This works well when it is a combination of in-memory cache and multiple instances. A message will be sent to all instances when the cache is explicitly deleted.

@Module({
  imports: [
    CacheDependencyModule.register({
      pubsub: { host: "localhost" },
    }),
  ],
})
export class AppModule {}

Notes

@CacheKey supports route-parser. You can use :name pattern as cache key

CacheDependencyGraph uses dependency-graph

License

MIT

0.11.8

13 days ago

0.11.5

1 month ago

0.11.6

26 days ago

0.11.7

20 days ago

0.10.1

2 months ago

0.10.2

2 months ago

0.10.3

2 months ago

0.10.4

2 months ago

0.10.5

2 months ago

0.10.6

2 months ago

0.9.20

2 months ago

0.9.21

2 months ago

0.10.0

2 months ago

0.9.22

2 months ago

0.9.13

3 months ago

0.9.14

2 months ago

0.9.15

2 months ago

0.9.16

2 months ago

0.9.17

2 months ago

0.9.18

2 months ago

0.9.19

2 months ago

0.11.0

2 months ago

0.11.1

2 months ago

0.11.2

2 months ago

0.11.3

1 month ago

0.11.4

1 month ago

0.9.8

3 months ago

0.9.7

3 months ago

0.9.9

3 months ago

0.9.4

3 months ago

0.9.3

3 months ago

0.9.6

3 months ago

0.9.5

3 months ago

0.9.12

3 months ago

0.9.10

3 months ago

0.9.11

3 months ago

0.9.2

3 months ago

0.9.1

3 months ago

0.8.4

3 months ago

0.7.0

3 months ago

0.8.1

3 months ago

0.8.0

3 months ago

0.8.3

3 months ago

0.8.2

3 months ago

0.6.21

4 months ago

0.6.20

4 months ago

0.6.23

4 months ago

0.6.22

4 months ago

0.6.24

3 months ago

0.6.18

5 months ago

0.6.17

5 months ago

0.6.19

4 months ago

0.6.14

6 months ago

0.6.13

6 months ago

0.6.16

5 months ago

0.6.15

5 months ago

0.9.0

3 months ago

0.6.9

7 months ago

0.6.10

6 months ago

0.6.12

6 months ago

0.6.11

6 months ago

0.6.7

7 months ago

0.6.8

7 months ago

0.6.6

7 months ago

0.6.5

7 months ago

0.6.4

8 months ago

0.6.3

8 months ago

0.6.2

8 months ago

0.6.1

8 months ago

0.6.0

8 months ago

0.5.11

8 months ago

0.5.10

9 months ago

0.5.9

9 months ago

0.5.8

9 months ago

0.5.7

9 months ago

0.5.6

9 months ago

0.5.5

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.5.0

10 months ago

0.4.8

10 months ago

0.4.7

10 months ago

0.4.6

10 months ago

0.4.5

10 months ago

0.4.4

10 months ago

0.4.3

10 months ago

0.4.2

10 months ago

0.4.1

11 months ago

0.4.0

11 months ago

0.3.42

11 months ago

0.3.41

11 months ago

0.3.40

11 months ago

0.3.39

12 months ago

0.3.38

12 months ago

0.3.37

12 months ago

0.3.36

1 year ago

0.3.35

1 year ago

0.3.33

1 year ago

0.3.32

1 year ago

0.3.31

1 year ago

0.3.30

1 year ago

0.3.29

1 year ago

0.3.28

1 year ago

0.3.26

1 year ago

0.3.25

1 year ago

0.3.24

1 year ago

0.3.23

1 year ago

0.3.22

1 year ago

0.3.21

1 year ago

0.3.20

1 year ago

0.3.19

1 year ago

0.3.18

1 year ago

0.3.17

1 year ago

0.3.16

1 year ago

0.3.15

1 year ago

0.3.14

1 year ago

0.3.13

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.3.9

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.66

2 years ago

0.1.65

2 years ago

0.1.64

2 years ago

0.1.63

2 years ago

0.1.62

2 years ago

0.1.61

2 years ago

0.1.59

2 years ago

0.1.58

2 years ago

0.1.57

2 years ago

0.1.56

2 years ago

0.1.55

2 years ago

0.1.54

2 years ago

0.1.53

2 years ago

0.1.52

2 years ago

0.1.51

2 years ago

0.1.50

2 years ago

0.1.49

2 years ago

0.1.48

2 years ago

0.1.47

2 years ago

0.1.46

2 years ago

0.1.45

2 years ago

0.1.44

2 years ago

0.1.43

2 years ago

0.1.42

2 years ago

0.1.41

2 years ago

0.1.40

2 years ago

0.1.39

2 years ago

0.1.38

2 years ago

0.1.37

2 years ago

0.1.36

2 years ago

0.1.35

2 years ago

0.1.34

2 years ago

0.1.33

2 years ago

0.1.32

2 years ago

0.1.31

2 years ago

0.1.30

2 years ago

0.1.29

2 years ago

0.1.28

2 years ago

0.1.27

2 years ago

0.1.26

2 years ago

0.1.25

2 years ago

0.1.24

2 years ago

0.1.23

2 years ago

0.1.22

2 years ago

0.1.21

2 years ago

0.1.18

3 years ago

0.1.17

3 years ago

0.1.16

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago