-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KeyGenerator #46
Comments
I do not understand. If you use The cacheName is specified by @Override
public String generateCacheName(Method method) {
String cacheName = cacheNameMap.get(method);
if (cacheName == null) {
final StringBuilder sb = new StringBuilder();
String className = method.getDeclaringClass().getName();
sb.append(ClassUtil.getShortClassName(removeHiddenPackage(hiddenPackages, className)));
sb.append('.');
sb.append(method.getName());
sb.append('(');
for(Class<?> c : method.getParameterTypes()){
getDescriptor(sb, c , hiddenPackages);
}
sb.append(')');
String str = sb.toString();
cacheNameMap.put(method, str);
return str;
} else {
return cacheName;
}
} |
If you have 2 beans implements same method in same interface, and want to seperate them in different |
This is exactly what i needed. Could you possibly tell me which class in Jetcache the generateCacheName is declared inside? I think the issue i have is related to method.getDeclaringClass().getName() as the superclass methods i'm annotating are creating the key using the superclass name instead of the subclass.
When calling the method test from Subclass i'm expecting the cache name to be Subclass.test() but instead it's SuperClass.test(). |
The However, you can seperate public abstract class SuperClass {
public void test() {
System.out.println("Hello world");
}
}
public class Subclass1 extends SuperClass {
@Cache(name="c1")
public void test() {
super.test();
}
}
public class Subclass2 extends SuperClass {
@Cache(name="c2")
public void test() {
super.test();
}
} Annotations with same |
Hey @areyouok thank you for all the help. The issue is I didn't want to resort to the solution you posted above as i'd have to write boiler plate code for multiple sub classes. Would there be a way to achieve my solution in a more efficient way such as letting the abstract class @Cache annotation figure out the key using the subclass name? |
I think you need fork jetcache and modify these methods or class:
|
I've created #47 to solve this problem. It now takes the target class which the method was executed from as the className value. |
I had implement it on master branch. You can build it youself or use 2.5.0-SNAPSHOT: <repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories> To supply you own NameGenerator, use this if you are using spring boot:
Without spring boot, invoke If no problem occurs I will release 2.5.0 in next few days. |
Hi @areyouok thank you very much! I'll give this a test and get back with any issues! |
@areyouok I've tested the changes and it's working as expected. I've also created #51 as i want to reuse most of the DefaultCacheGenerator functionality and need access to the fields. Other than that everything is perfect! Thank you. For anyone with the same issue here's a full example with @areyouok solution.
|
PR 51 is merged and 2.5.0 is released. |
Hi,
I'm moving over from Spring cache to Jetcache but can't seem to figure out the best way to specify a global key generator.
In spring cache this was achieved by the code below:
The reason i need this is because i have some subclasses which need to have a different cache name hence the usage of method.getDeclaringClass().getSimpleName().
Can you maybe tell me what the easiest way to achieve this in Jetcache would be?
Thanks
The text was updated successfully, but these errors were encountered: