question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method level @Cache annotations for Service

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Only cacheing implemented for controllers. missing auto cache invalidation

Expected behavior

proposed example:

@CacheConfig("pets")
class PetService {
    @Cacheable 
    findById(id: Long): Pet {
    }

    @CachePut
    save(pet: Pet) {
    }

    @CacheInvalidate
    delete(pet: Pet) {
    }
}

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

we like to control caching and cache invalidation for CRUD Services automatically, key can be derived from args e.g., https://github.com/havsar/node-ts-cache/blob/master/src/CacheDecorator.ts

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
iam4xcommented, Jul 22, 2019

Here is a simple @Cache decorator with lru-cache :

import LRU from 'lru-cache';

const cache = new LRU();

export function Cache({ key, ttl }: { key: string; ttl: number }) {
  return function(
    _target: Record<string, any>,
    _propertyKey: string,
    descriptor: PropertyDescriptor
  ) {
    const method = descriptor.value;
    descriptor.value = async function(...args: any[]) {
      if (cache.get(key)) return cache.get(key);

      const result = await method.apply(this, args);
      cache.set(key, result, ttl);

      return result;
    };
  };
}

usage:

@Cache({ key: 'admin_menu_count', ttl: 1000 * 60 * 5 })
private async getCachedCount() {
	[...]
}
0reactions
lock[bot]commented, Nov 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Guide To Caching in Spring - Baeldung
To enable caching, Spring makes good use of annotations, much like enabling any other configuration level feature in the framework.
Read more >
Spring Boot Caching - Javatpoint
It is a method level annotation. It is used when we want to remove stale or unused data from the cache. It requires...
Read more >
29. Cache Abstraction - Spring
For caching declaration, the abstraction provides two Java annotations: @Cacheable and @CacheEvict which allow methods to trigger cache population or cache ...
Read more >
Spring Boot Caching 101 - Auth0
This method-level annotation lets Spring Boot know that the return value of the annotated method can be cached. Each time a method marked ......
Read more >
Implementing a Cache with Spring Boot - Reflectoring
The method get() is annotated with @Cachable . This annotation starts the powerful Spring cache support. The data in the cache is stored...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found