本文整理汇总了Java中com.google.common.cache.LoadingCache.put方法的典型用法代码示例。如果您正苦于以下问题:Java LoadingCache.put方法的具体用法?Java LoadingCache.put怎么用?Java LoadingCache.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.cache.LoadingCache
的用法示例。
在下文中一共展示了LoadingCache.put方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.google.common.cache.LoadingCache; //导入方法依赖的package包/类
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws ExecutionException, InterruptedException {
LoadingCache<String, String> cache = null;
cache = CacheBuilder.newBuilder()
// 设置并发级别为200,并发级别是指可以同时写缓存的线程数
.concurrencyLevel(200)
// 设置写缓存后1分钟过期
.expireAfterWrite(1, TimeUnit.SECONDS).initialCapacity(10).maximumSize(100)
// 设置要统计缓存的命中率
.recordStats()
// 设置缓存的移除通知
.removalListener(new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> notification) {
System.out.println(notification.getKey() + " was removed, cause by " + notification.getCause());
}
}).build(new CacheLoader<String, String>() {
// build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存
@Override
public String load(String appIdSecret) throws Exception {
return "";
}
});
cache.put("key1", "value1");
System.out.println(cache.get("key1"));
Thread.sleep(2000);
System.out.println(cache.get("key1"));
}
示例2: put
import com.google.common.cache.LoadingCache; //导入方法依赖的package包/类
@Override
public synchronized void put(@NonNull String key, @NonNull V value)
{
checkNotNull(key);
checkNotNull(value);
LoadingCache<String, Optional<ExpiringValue<V>>> c = cache.getUnchecked(CurrentInstitution.get());
// Do nothing if the value hasn't changed
Optional<ExpiringValue<V>> opExVal = c.getIfPresent(key);
if( opExVal != null && opExVal.isPresent() )
{
V oldValue = opExVal.get().getValue();
if( oldValue != null && oldValue.equals(value) )
{
return;
}
}
// Update the DB state if it's clustered
if( zookeeperService.isCluster() )
{
dao.put(cacheId, key, new Date(System.currentTimeMillis() + ttlUnit.toMillis(ttl)),
PluginAwareObjectOutputStream.toBytes(value));
}
// Invalidate other servers caches
invalidateOthers(key);
// Update our local cache
c.put(key, Optional.of(ExpiringValue.expireAfter(value, ttl, ttlUnit)));
}
示例3: test_001
import com.google.common.cache.LoadingCache; //导入方法依赖的package包/类
@Test
public void test_001() throws ExecutionException {
LoadingCache<String,Object> failedCache = CacheBuilder.newBuilder().
softValues().maximumSize(10000)
.build(new CacheLoader<String, Object>() {
@Override
public Object load(String s) throws Exception {
return new AtomicInteger(0);
}
});
failedCache.put("00",((AtomicInteger)failedCache.get("00")).incrementAndGet());
System.out.println(failedCache.get("00"));
}
示例4: getMetadata
import com.google.common.cache.LoadingCache; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void getMetadata(LoadingCache<String, Map<String, String>> metadata, File f)
{
// You don't even need to touch power lines to die. DON'T DIE!
if( Check.isEmpty(exifToolPath) || !Files.exists(Paths.get(exifToolPath)) || !f.exists() )
{
return;
}
List<String> commandOpts = Lists.newArrayList();
commandOpts.add(exifToolPath);
commandOpts.add("-g"); // Group (EXIF, XMP etc)
commandOpts.add("-j"); // JSON output
commandOpts.add("-q"); // Quiet processing
commandOpts.add("-sort"); // Alphabetical sort
commandOpts.add("-struct"); // Expand structs (ewww)
commandOpts.add("-u"); // Unsupported tags
// Remove unsafe tags e.g Directory/Permissions etc
for( String tag : removeTags )
{
commandOpts.add("-x");
commandOpts.add(tag);
}
// File to process
commandOpts.add(f.getAbsolutePath());
ObjectMapper om = new ObjectMapper();
List<Map<String, Object>> outputList = Lists.newArrayList();
try
{
ExecResult exec = ExecUtils.exec(commandOpts);
String stdout = exec.getStdout();
Map<String, Object> jsonMap = Maps.newHashMap();
flatten(om.readTree(stdout).get(0), jsonMap, false);
outputList.add(jsonMap);
}
catch( IOException e )
{
throw Throwables.propagate(e);
}
// This is a bit dodgical... but seems to be a necessary evil
for( final Entry<String, Object> objectMap : outputList.get(0).entrySet() )
{
if( objectMap.getValue() instanceof Map )
{
metadata.put(objectMap.getKey(),
Maps.transformValues(((Map<String, Object>) objectMap.getValue()), Functions.toStringFunction()));
}
}
}