kr.debop4j.core.cache.ConcurrentHashMapCacheRepository.java Source code

Java tutorial

Introduction

Here is the source code for kr.debop4j.core.cache.ConcurrentHashMapCacheRepository.java

Source

/*
 * Copyright 2011-2013 the original author or authors.
 *
 * 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.
 */

package kr.debop4j.core.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import kr.debop4j.core.parallelism.AsyncTool;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static kr.debop4j.core.Guard.shouldNotBeWhiteSpace;

/**
 * {@link java.util.concurrent.ConcurrentHashMap}? ?   ? ? .
 *
 * @author ? ( sunghyouk.bae@gmail.com )
 * @since 12. 9. 12
 */
@Slf4j
@SuppressWarnings("unchecked")
public class ConcurrentHashMapCacheRepository extends CacheRepositoryBase {

    private final Cache<String, Object> cache;

    /**
     * Instantiates a new Concurrent hash map cache repository.
     *
     * @param validFor the valid for
     */
    public ConcurrentHashMapCacheRepository(long validFor) {
        if (validFor > 0)
            setExpiry(validFor);

        CacheBuilder builder = CacheBuilder.newBuilder().concurrencyLevel(4);

        if (validFor > 0)
            builder.expireAfterAccess(validFor, TimeUnit.MINUTES);

        cache = builder.build();
    }

    /**
     * ? 
     *
     * @return ?  .
     */
    public Map<String, Object> getCache() {
        return this.cache.asMap();
    }

    /**
     * ??  ? ? .
     *
     * @param key ? 
     * @return ? ,  null 
     */
    @Override
    public Object get(final String key) {
        shouldNotBeWhiteSpace(key, "key");
        return cache.getIfPresent(key);
    }

    /**
     * Gets async.
     *
     * @param key the key
     * @return the async
     */
    public Future<Object> getAsync(final String key) {
        return AsyncTool.startNew(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return cache.getIfPresent(key);
            }
        });
    }

    /**
     * ?? ? .
     *
     * @param key      ? 
     * @param value    ? 
     * @param validFor ?   ( : minutes), 0 ??  ? .
     */
    @Override
    public void set(final String key, final Object value, final long validFor) {
        shouldNotBeWhiteSpace(key, "key");
        cache.put(key, value);
    }

    /**
     *  ? ? ? .
     *
     * @param key ? 
     */
    @Override
    public void remove(final String key) {
        shouldNotBeWhiteSpace(key, "key");
        cache.invalidate(key);
    }

    /**
     * ?  ? .
     *
     * @param keys ?  
     */
    @Override
    public void removeAll(final String... keys) {
        cache.invalidateAll(Arrays.asList(keys));
    }

    /**
     * ?  ? .
     *
     * @param keys ?  
     */
    @Override
    public void removeAll(final Iterable<String> keys) {
        cache.invalidateAll(keys);
    }

    /**
     *  ? ? ?   .
     *
     * @param key ? 
     * @return ?   
     */
    @Override
    public boolean exists(final String key) {
        shouldNotBeWhiteSpace(key, "key");
        return cache.getIfPresent(key) != null;
    }

    /** ??  ? . */
    @Override
    public synchronized void clear() {
        cache.cleanUp();
    }
}