001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Maxime Hilaire
018 *
019 */
020package org.nuxeo.ecm.core.cache;
021
022import static org.nuxeo.ecm.core.cache.CacheDescriptor.OPTION_CONCURRENCY_LEVEL;
023import static org.nuxeo.ecm.core.cache.CacheDescriptor.OPTION_MAX_SIZE;
024
025import java.io.Serializable;
026import java.util.Map;
027import java.util.Set;
028import java.util.concurrent.TimeUnit;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033import com.google.common.cache.Cache;
034import com.google.common.cache.CacheBuilder;
035
036/**
037 * Default in memory implementation for cache management based on guava
038 *
039 * @since 6.0
040 */
041public class InMemoryCacheImpl extends AbstractCache {
042
043    private static final Log log = LogFactory.getLog(InMemoryCacheImpl.class);
044
045    protected final Cache<String, Serializable> cache;
046
047    public InMemoryCacheImpl(CacheDescriptor desc) {
048        super(desc);
049        CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
050        builder = builder.expireAfterWrite(desc.getTTL(), TimeUnit.MINUTES);
051        Map<String, String> options = desc.options;
052        if (options.containsKey(OPTION_CONCURRENCY_LEVEL)) {
053            builder = builder.concurrencyLevel(Integer.parseInt(options.get(OPTION_CONCURRENCY_LEVEL)));
054        }
055        if (options.containsKey(OPTION_MAX_SIZE)) {
056            builder = builder.maximumSize(Integer.parseInt(options.get(OPTION_MAX_SIZE)));
057        }
058        cache = builder.build();
059    }
060
061    @Override
062    public Serializable get(String key) {
063        if (key == null) {
064            return null;
065        } else {
066            return cache.getIfPresent(key);
067        }
068    }
069
070    @Override
071    public Set<String> keySet() {
072        return cache.asMap().keySet();
073    }
074
075    @Override
076    public void invalidate(String key) {
077        invalidateLocal(key);
078    }
079
080    @Override
081    public void invalidateLocal(String key) {
082        if (key != null) {
083            cache.invalidate(key);
084        } else {
085            log.warn(String.format("Can't invalidate a null key for the cache '%s'!", name));
086        }
087    }
088
089    @Override
090    public void invalidateAll() {
091        invalidateLocalAll();
092    }
093
094    @Override
095    public void invalidateLocalAll() {
096        cache.invalidateAll();
097    }
098
099    @Override
100    public void put(String key, Serializable value) {
101        putLocal(key, value);
102    }
103
104    @Override
105    public void putLocal(String key, Serializable value) {
106        if (key != null && value != null) {
107            cache.put(key, value);
108        } else {
109            log.warn(String.format("Can't put a null key nor a null value in the cache '%s'!", name));
110        }
111    }
112
113    @Override
114    public boolean hasEntry(String key) {
115        return cache.asMap().containsKey(key);
116    }
117
118    @Override
119    public long getSize() {
120        return cache.size();
121    }
122
123}