CachedValuesManager - simple example

is there any chance someone could post a basic (the most basic possible, barebones) complete working example of

set & get cached value with CachedValuesManager?

Thank you very much!

0

You can find usage examples in IntelliJ Community, do they help? What exactly are your questions about it?

0

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?

0

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:

  @NotNull
  public PsiMethod[] getConstructors() {
    final PsiMethod[] constructors = myConstructorsCache.getValue();
    return constructors != null ? constructors : PsiMethod.EMPTY_ARRAY;
  }

0

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?

0

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.

0

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).

0

An instance of a custom class tied to a project is simply a project service.


0

.. 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? ?:|

0

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/

0

Ta, Dmitry!! this certainly helps.

I will now keep quiet for a while :-P

:D

0

请先登录再写评论。