Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache option not working #397

Open
marzack87 opened this issue Jan 22, 2020 · 5 comments · May be fixed by #688
Open

Cache option not working #397

marzack87 opened this issue Jan 22, 2020 · 5 comments · May be fixed by #688

Comments

@marzack87
Copy link

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
Copy link

boskiv commented Feb 14, 2020

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
Copy link

cuni0716 commented May 18, 2020

@zMotivat0r could you provide some info here please?

@hamusuk
Copy link

hamusuk commented Oct 27, 2020

same issue here (redis)

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

@elvinmeza
Copy link

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

@rewiko rewiko linked a pull request Apr 2, 2021 that will close this issue
@rewiko
Copy link
Collaborator

rewiko commented Apr 2, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants