nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

Cache option not working #397

Open marzack87 opened 4 years ago

marzack87 commented 4 years ago

Hi,

I'm trying to use typeorm cache using the crud decorator:

@Crud({
  model: {
    type: Order
  },
  query: {
    join: {
      catalog: {
        eager: true
      },
      products: {
        eager: true
      }
    },
    cache: 10000
  }
})

And simple typeorm configuration:

{
  type: 'mysql',
  ...
  cache: true
}

but no cache is created.

Exploring typeorm-crud.service.js code, I've noticed that cache is implemented like this (line 179):

builder.cache(builder.getQueryAndParameters(), options.query.cache);

but in this way the whole SQL query is used as identifier, but its db field is a VARCHAR(255), and so no record is created inside cache's table

boskiv commented 4 years ago

I have similar: orm.config.ts:

export const withCache: TypeOrmModuleOptions = {
    type: 'postgres',
    url: process.env.DATABASE_URL || 'postgres://localhost:5432/api_dev',
    synchronize: true,
    migrationsRun: true,
    logging: true,
    cache: {
        type: 'redis',
        options: {
            url: process.env.REDIS_URL || 'redis://localhost:6379',

        },
    },
    autoLoadEntities: true
};

user.controller.ts

@Crud({
  model: {
    type: User,
  },
  params: {
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  query: {
    alwaysPaginate: true,
    join: {
      profile: {
        eager: true
      },
    },
    cache: 20000,
  },
})
@ApiTags('users')
@Controller('users')
export class UsersController implements CrudController<User> {
  constructor(public service: UsersService) { }

}

I see a connection to redis:

tcp        0      0 172.22.0.2:6379         172.22.0.1:49322        ESTABLISHED 

I have made some GET/POST quieries But no KEYS in Redis appears

127.0.0.1:6379> SELECT 0
OK
127.0.0.1:6379> KEYS *
(empty list or set)
cuni0716 commented 4 years ago

@zMotivat0r could you provide some info here please?

hamusuk commented 3 years ago

same issue here (redis)

builder.getQueryAndParameters() can't be used as identifier

elvinmeza commented 3 years ago

Make sure you have your database up to date using either synchronize or migrations, also consider extending the default duration time (1s)

rewiko commented 3 years ago

Hello,

I got the same issue and I think it has never worked. The code is designed to give an array as a key to cache TypeOrm function, the latter needs a string.

2 options are possible the first one would be to concatenate the getQueryAndParameters array or let TypeOrm generates the key.

PR link