Java Code Examples for com.google.common.cache.LoadingCache#put()

The following examples show how to use com.google.common.cache.LoadingCache#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnitTest.java    From albert with MIT License 6 votes vote down vote up
@Test
public void TestLoadingCache() throws Exception{
    LoadingCache<String,String> cahceBuilder=CacheBuilder
    .newBuilder()
    .build(new CacheLoader<String, String>(){
        @Override
        public String load(String key) throws Exception {        
            String strProValue="hello "+key+"!";                
            return strProValue;
        }
        
    });        
     
    System.out.println("jerry value:"+cahceBuilder.apply("jerry"));
    System.out.println("jerry value:"+cahceBuilder.get("jerry"));
    System.out.println("peida value:"+cahceBuilder.get("peida"));
    System.out.println("peida value:"+cahceBuilder.apply("peida"));
    System.out.println("lisa value:"+cahceBuilder.apply("lisa"));
    cahceBuilder.put("harry", "ssdded");
    System.out.println("harry value:"+cahceBuilder.get("harry"));
}
 
Example 2
Source File: GlobalCache.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("try")
public void loadSource(final File file) throws IOException {

  try (TelemetryUtils.ScopedSpan scope =
      TelemetryUtils.startScopedSpan("GlobalCache.loadSource")) {

    TelemetryUtils.ScopedSpan.addAnnotation(
        TelemetryUtils.annotationBuilder().put("file", file.getPath()).build("args"));

    final LoadingCache<File, Source> sourceCache = this.getSourceCache();
    Project project = this.projectSupplier.get();
    final File projectRoot = project.getProjectRoot();
    JavaSourceLoader loader = sourceLoaders.get(projectRoot);
    sourceCache.put(file, loader.load(file));
  }
}
 
Example 3
Source File: CaffeinatedGuavaTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testReload_interrupted() {
  try {
    LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(
        Caffeine.newBuilder().executor(MoreExecutors.directExecutor()),
        new CacheLoader<Integer, Integer>() {
          @Override public Integer load(Integer key) throws Exception {
            throw new InterruptedException();
          }
        });
    cache.put(1, 1);
    cache.refresh(1);
  } catch (CacheLoaderException e) {
    assertTrue(Thread.currentThread().isInterrupted());
  }
}
 
Example 4
Source File: CaffeinatedGuavaTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testReload_throwable() {
  try {
    LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(
        Caffeine.newBuilder().executor(MoreExecutors.directExecutor()),
        new CacheLoader<Integer, Integer>() {
          @Override public Integer load(Integer key) throws Exception {
            throw new Exception();
          }
        });
    cache.put(1, 1);
    cache.refresh(1);
  } catch (CacheLoaderException e) {
    assertTrue(Thread.currentThread().isInterrupted());
  }
}
 
Example 5
Source File: GlobalCache.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
public void replaceSource(final Source source) {
  final LoadingCache<File, Source> sourceCache = this.getSourceCache();
  sourceCache.put(source.getFile(), source);
}