org.cache2k.benchmark.thirdparty.GuavaCacheFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.cache2k.benchmark.thirdparty.GuavaCacheFactory.java

Source

package org.cache2k.benchmark.thirdparty;

/*
 * #%L
 * thirdparty
 * %%
 * Copyright (C) 2013 - 2016 headissue GmbH, Munich
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.cache2k.benchmark.BenchmarkCache;
import org.cache2k.benchmark.BenchmarkCacheFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Jens Wilke; created: 2013-12-08
 */
public class GuavaCacheFactory extends BenchmarkCacheFactory {

    @Override
    public BenchmarkCache<Integer, Integer> create(int _maxElements) {
        MyBenchmarkCacheAdapter c = new MyBenchmarkCacheAdapter();
        c.size = _maxElements;
        CacheBuilder cb = CacheBuilder.newBuilder().maximumSize(_maxElements);
        if (withExpiry) {
            cb.expireAfterWrite(5 * 60, TimeUnit.SECONDS);
        }
        c.cache = cb.build();
        return c;
    }

    static class MyBenchmarkCacheAdapter extends BenchmarkCache<Integer, Integer> {

        int size;
        Cache<Integer, Integer> cache;

        @Override
        public int getCacheSize() {
            return size;
        }

        @Override
        public Integer getIfPresent(final Integer key) {
            return cache.getIfPresent(key);
        }

        @Override
        public void put(final Integer key, final Integer value) {
            cache.put(key, value);
        }

        @Override
        public void destroy() {
            cache.cleanUp();
        }

    }

}