All cached values created through CachedValuesManager are per-project. Therefore, you can't use either a static method or a singleton to work with CachedValuesManager.
Generally speaking, caching stuff in static fields in an IntelliJ IDEA plugin is a bad idea because it's very likely to cause memory leaks.
You can find usage examples in IntelliJ Community, do they help? What exactly are your questions about it?
Hi Yann,
I am looking for use example which shows how to correctly use CachedValuesManager: get & set.
Just hope to save some valuable time for me and hopefully others for productive coding and testing instead of poking around to uncertain result.
I searched, looked at the examples and still don't understand how to use CachedValuesManager.
Could you help, please?
here is an example:
https://github.com/joewalnes/idea-community/blob/master/java/java-impl/src/com/intellij/psi/impl/source/ClassInnerStuffCache.java
set:
private void buildCaches() {
final CachedValuesManager manager = CachedValuesManager.getManager(myClass.getProject());
final Object[] dependencies = {PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT, myTreeChangeTracker};
myConstructorsCache = manager.createCachedValue(new CachedValueProvider<PsiMethod[]>() {
public Result<PsiMethod[]> compute() {
return Result.create(PsiImplUtil.getConstructors(myClass), dependencies);
}
}, false);
get:
public PsiMethod[] getConstructors() {
final PsiMethod[] constructors = myConstructorsCache.getValue();
return constructors != null ? constructors : PsiMethod.EMPTY_ARRAY;
}
my more specific question is:
myConstructorsCache is a private [field / property?] in ClassInnerStuffCache so it seems to be tied to its instance.
is it possible to set / get cache via static methods?
or should I use a singleton?
All cached values created through CachedValuesManager are per-project. Therefore, you can't use either a static method or a singleton to work with CachedValuesManager.
Generally speaking, caching stuff in static fields in an IntelliJ IDEA plugin is a bad idea because it's very likely to cause memory leaks.
Cheers Dmitry. this helps.
Is there a way to cache values on per-project basis?
Speaking more broadly, how could I tie (set & get) custom class instances to a project?
This is probably another topic. I am not sure how to phrase is correctly. At least here there is some context (working with cached values).
An instance of a custom class tied to a project is simply a project service.
.. so an example of
public static <T> void set(String key, T instance){
...
}
public static <T> T get(String key){
...
}
.. or non-static:
public <T> void set(Project project, String key, T instance){
...
}
public <T> T get(Project project, String key){
...
}
would be...?
https://devnet.jetbrains.com/message/5544391#5544391
is that it? ?:|
class MyService {
public static MyService getInstance() { return ServiceManager.getService(MyService.class); }
private HashMap<String, Object> myValues = new HashMap<String, Object>();
public static <T> void set(String key, T instance) {
getInstance().myValues.put(key, instance);
}
public static <T> T get(String key) {
retrurn (T) getInstance().myValues.get(key);
}
}
A project service would be similar/
Ta, Dmitry!! this certainly helps.
I will now keep quiet for a while :-P
:D