Skip to content

Redis Cache

Redis Cache is implemented based on Redis

Create Redis Cache

For example, create a Redis Cache student in the module demo-student, to cache student data

1. Cli Command

bash
$ vona :create:bean cacheRedis student --module=demo-student

2. Menu Command

TIP

Context menu - [Module Path]: Vona Bean/Cache Redis

Redis Cache Definition

typescript
export type TCacheRedisStudentKey = string;
export interface TCacheRedisStudentData { id: string; name: string }

@CacheRedis({
  ttl: 2 * 3600 * 1000,
})
export class CacheRedisStudent
  extends BeanCacheRedisBase<TCacheRedisStudentKey, TCacheRedisStudentData> {}
  • TCacheRedisStudentKey: Defines the type of the cache key
  • TCacheRedisStudentData: Defines the type of the cache data

Redis Cache Parameters

Parameters can be configured for Redis Cache

typescript
@CacheRedis({
  ttl: 2 * 3600 * 1000,
  updateAgeOnGet: true,
  disableInstance: false,
  disableTransactionCompensate: false,
  client: 'cache',
})
class CacheRedisStudent {}
NameTypeDefaultDescription
ttlnumberCache expiration time
updateAgeOnGetbooleantrueWhether to update the ttl when reading from the cache
disableInstancebooleanfalseWhether to disable instance isolation. By default, caches between multiple instances are isolated
disableTransactionCompensatebooleanfalseWhether to disable transaction compensation. Enabling transaction compensation ensures cache data consistency
clientstring'cache'Redis client used for caching

App Config

Redis Cache parameters can be configured in App Config

src/backend/config/config/config.ts

typescript
// onions
config.onions = {
  cacheRedis: {
    'demo-student:student': {
      ttl: 2 * 3600 * 1000,
      updateAgeOnGet: true,
      disableInstance: false,
      disableTransactionCompensate: false,
      client: 'cache',
    },
  },
};

Redis Cache Enable/Disable

You can control enable/disable of Redis Cache

1. Enable

src/backend/config/config/config.ts

diff
// onions
config.onions = {
  cacheRedis: {
    'demo-student:student': {
+     enable: true,
    },
  },
};

2. Meta

Allows Redis Cache to take effect in a specified operating environment

NameTypeDescription
flavorstring|string[]See: Runtime Environments and Flavors
modestring|string[]See: Runtime Environments and Flavors
  • Example
diff
@CacheRedis({
+ meta: {
+   flavor: 'normal',
+   mode: 'dev',
+ },
})
class CacheRedisStudent {}

Using Redis Cache

typescript
class ControllerStudent {
  @Web.get('test')
  async test() {
    const student = { id: '1', name: 'tom' };
    await this.scope.cacheRedis.student.set(student, '1');
    const value = await this.scope.cacheRedis.student.get('1');
    assert.deepEqual(student, value);
  }
}
  • this.scope.cacheRedis.student: Gets the Redis Cache instance through the module scope

Cache method parameters

Take the set method as an example to introduce the parameters of the cache method

typescript
await this.scope.cacheRedis.student.set(student, '1', {
  ttl: 2 * 3600 * 1000,
  disableTransactionCompensate: true,
  db: this.ctx.db,
});
NameTypeDescription
ttlnumberCache expiration time
disableTransactionCompensatebooleanWhether to disable transaction compensation
dbServiceDbThis db object is used when performing transaction compensation. By default, the db object in the context is automatically used
  • db: VonaJS supports multiple databases/datasources, so transaction compensation can be precisely controlled through db

Cache methods

NameDescription
getRead cache
mgetRead multiple caches at once
peekRetrieve cache without updating its TTL
setSet cache
msetSet multiple caches at once
getsetSet new cache and return old value
hasCheck if cache exists
delDelete cache
mdelDelete multiple caches at once
clearClear all caches
expireModify cache expiration time
lookupKeysGet all cache keys

Released under the MIT License.