Skip to content

Releases: anchan828/nest-cache

v3.0.0

20 Jul 17:02
Compare
Choose a tag to compare

Features

Added a new CacheMiddleware

You can add middleware that is executed just before calling the cache method. It can be used as an interceptor to process the cache key or the value to be stored, or to define dependencies on the cache to manipulate other caches under certain conditions.

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

  public async update(userId: number, age: number): Promise<void> {
    await this.cacheService.set(`users/${userId}`, age, 10, {
      /**
       * You can pass information to be processed under specific conditions used in middleware.
       */
      source: { userId },
    });
  }
}

@CacheMiddleware({
  /**
   * The priority of the middleware. The higher the number, the low the priority.
   */
  priority: 1,
})
class TestCacheMiddleware implements ICacheMiddleware {
  constructor(private readonly cacheService: CacheService) {}
  /**
   * If you want to set a hook for set, implement the set method.
   */
  async set(context: CacheContext<"set">): Promise<void> {
    /**
     * Change data
     */
    context.key = `version-1/${context.key}`;
    context.value = { data: context.value };
    context.ttl = 1000;

    /**
     * Get the source passed from the set method
     */
    const source = context.getSource<{ userId: number }>();

    /**
     * Manage other caches under certain conditions
     */
    if (source?.userId === 1) {
      this.cacheService.delete("another-cache-key");
    }
  }

  /**
   * You can define middleware for most methods.
   */
  // ttl?(context: CacheContext<"ttl">): Promise<void>;
  // delete?(context: CacheContext<"delete">): Promise<void>;
  // mget?(context: CacheContext<"mget">): Promise<void>;
  // mset?(context: CacheContext<"mset">): Promise<void>;
  // mdel?(context: CacheContext<"mdel">): Promise<void>;
  // hget?(context: CacheContext<"hget">): Promise<void>;
  // hset?(context: CacheContext<"hset">): Promise<void>;
  // hdel?(context: CacheContext<"hdel">): Promise<void>;
  // hgetall?(context: CacheContext<"hgetall">): Promise<void>;
  // hkeys?(context: CacheContext<"hkeys">): Promise<void>;
}

@Module({
  imports: [CacheModule.register()],
  prividers: [
    /**
     * Register middleware
     */
    TestCacheMiddleware,
    ExampleService,
  ],
})
export class AppModule {}

Breaking changes

  • Drop the method with variable-length arguments
    • mget
    • mdel
    • hdel
- mget(...keys: string[]): Promise<Record<string, T>>
+ mget(keys: string[]): Promise<Record<string, T>> 

v2.0.0

06 Oct 07:13
Compare
Choose a tag to compare

Use cache-manager v5

v1.2.0

26 Jul 02:25
Compare
Choose a tag to compare

Please clear all caches before applying this package version.

  • @anchan828/nest-cache-manager-ioredis package uses MessagePack for efficient serialization/deserialization.
    • The most obvious is to serialize/deserialize a Date object with JSON. JSON.parse does not support Date object, so you need to implement the receiver yourself. Checking and parsing properties one by one as strings is an inefficient and very time-consuming process.

v1.1.0

21 Jul 07:19
Compare
Choose a tag to compare

Add redis-based hash commands

hget<T>(key: string, field: string): Promise<T | undefined>;
hset<T>(key: string, field: string, value: T): Promise<void>;
hdel(key: string, ...fields: string[]): Promise<void>;
hgetall(key: string): Promise<Record<string, any>>;
hkeys(key: string): Promise<string[]>;

These are available in packages @anchan828/nest-cache-manager-memory and @anchan828/nest-cache-manager-ioredis.

Add new package @anchan828/nest-cache-manager-memory

Wrapped memory store for cache-manager.
I have created a dedicated memory store as we will add our own commands in the future as described above.

Remove cacheVersion from options

These were not working properly.

CacheModule.register({
-  cacheVersion: "v1",
}),