private static Cache<String, CacheObject> buildCache(
     /*int reloadAfterAccess, */ int reloadAfterWrite) {
   CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().softValues();
   // if(reloadAfterAccess > 0)
   //     builder.expireAfterAccess(reloadAfterAccess, TimeUnit.SECONDS);
   if (reloadAfterWrite > 0) builder.expireAfterWrite(reloadAfterWrite, TimeUnit.SECONDS);
   return builder.build();
 }
示例#2
0
 public void testTimeToLive_negative() {
   CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>();
   try {
     builder.expireAfterWrite(-1, SECONDS);
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
示例#3
0
 public void testTimeToLive_setTwice() {
   CacheBuilder<Object, Object> builder =
       new CacheBuilder<Object, Object>().expireAfterWrite(3600, SECONDS);
   try {
     // even to the same value is not allowed
     builder.expireAfterWrite(3600, SECONDS);
     fail();
   } catch (IllegalStateException expected) {
   }
 }
  <K, V> void configureCacheBuilder(
      CompleteConfiguration<K, V> completeConfiguration,
      CacheBuilder<Object, Object> cacheBuilder) {
    ExpiryPolicy expiryPolicy = completeConfiguration.getExpiryPolicyFactory().create();

    Duration expiryForAccess = expiryPolicy.getExpiryForAccess();
    if (expiryForAccess != null && !expiryForAccess.isEternal()) {
      cacheBuilder.expireAfterAccess(
          expiryForAccess.getDurationAmount(), expiryForAccess.getTimeUnit());
    }
    Duration expiryForUpdate = expiryPolicy.getExpiryForUpdate();
    if (expiryForUpdate != null && !expiryForUpdate.isEternal()) {
      cacheBuilder.expireAfterWrite(
          expiryForUpdate.getDurationAmount(), expiryForUpdate.getTimeUnit());
    }
  }
示例#5
0
 public com.google.common.cache.CacheBuilder<Object, Object> expireAfterWrite(
     long duration, TimeUnit unit) {
   return builder.expireAfterWrite(duration, unit);
 }
 @Override
 public void applyExpiration(final CacheBuilder<?, ?> builder, final Period period) {
   builder.expireAfterWrite(period.duration, period.durationUnit);
 }