0

I use guava cache in my appliation which was written in a hibernate platform. The problem is my cache doesn't work well when loading the data. It doesn't update quickly when I delete or add a new data.

    private void initializeCache()
      {
        CacheLoader<String, List<~My Data Type~>> cacheLoader = new CacheLoader<String, List<~My Data Type~>>()
        {
          @Override
          public List<~My Data Type~> load(String key) throws Exception
          {
            return loadingCache.get(key);
          }
        };

    this.loadingCache = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build(cacheLoader);
  }

and this is the way I load data to the server side,

 public List<~My Data Type~> findByIds(final List<String> ids) throws SpiderException
  {
    final List<~My Data Type~> datas = new ArrayList<>();
    if (loadingCache.asMap().isEmpty())
    {
      final List<~My Data Type~> loaded = loadById(ids);
      if (loaded != null && !loaded.isEmpty())
      {
        loadingCache.putAll(
            loaded.stream().collect(Collectors.groupingBy(~My Data Type~::getId, Collectors.toList())));
      }
    }

    final ImmutableMap<String, List<~My Data Type~>> allPresent = loadingCache.getAllPresent(ids);
    for (Entry<String, List<~My Data Type~>> myData : allPresent.entrySet())
    {
      datas.addAll(myData .getValue());
    }

    return datas;
  }

Also I don't use the cache in data saving or deletion. Can some one explain me what is the reson for this issue? Is it a problem of guava cache?

chamzz.dot
  • 607
  • 2
  • 12
  • 24

1 Answers1

0

There are some Interruption in the Guava cahce.Just check

this documentation