DEV Community

Discussion on: RxJS: Caching Observables with a Decorator

Collapse
 
sysmat profile image
Sysmat

For me this doesn't work(@microphi/cache) it fetch only one id ok

Collapse
 
davidecavaliere profile image
Davide Cavaliere

If you share your code maybe I can help.

Collapse
 
sysmat profile image
Sysmat
  • service.ts:
@Cache({
    ttl: 250
  })
  getUserNetIdById(id: number): Observable<string> {
    return this.http.get<AaiUser>(`${userPath}/${id}`, getHttpJwtOptions()).pipe(map(u => u.netId));
  }
Enter fullscreen mode Exit fullscreen mode
  • pipe.ts:
@Pipe({
  name: 'getUserById'
})
export class GetUserByIdPipe implements PipeTransform {

  constructor(
    private readonly userService: UserService
  ) { }

  transform(id: number): Observable<string> {
    return this.userService.getUserNetIdById(id);
  }
}
Enter fullscreen mode Exit fullscreen mode
  • template.html
<tr *ngFor="let docSpec of docSpecs">
<td>
      <small>
            {{docSpec.user}}
            {{docSpec.user | getUserById | async}}
       </small>
</td>
</tr>
Enter fullscreen mode Exit fullscreen mode

- wrong result in the template:

and for userId=1, 17 I get some result but should be different

- img :

Regards, Tomaž

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
sysmat profile image
Sysmat

hm I can't upload image

Thread Thread
 
davidecavaliere profile image
Davide Cavaliere

Maybe a silly question but if you remove the @Cache decorator, do you actually get what you expect?
Is there anyway you can share the project so that i can run it?

Thread Thread
 
sysmat profile image
Sysmat • Edited
  • yes of cource it is in production
  • I implement simple in memory object as cache
  • no it use a lot of services
Thread Thread
 
davidecavaliere profile image
Davide Cavaliere

If you can please open an issue here with the steps to reproduce the problem.