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 java.io.Serializable; 023import java.util.Set; 024import java.util.concurrent.TimeUnit; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028 029import com.google.common.cache.Cache; 030import com.google.common.cache.CacheBuilder; 031 032/** 033 * Default in memory implementation for cache management based on guava 034 * 035 * @since 6.0 036 */ 037public class InMemoryCacheImpl extends AbstractCache { 038 039 public InMemoryCacheImpl(CacheDescriptor desc) { 040 super(desc); 041 CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); 042 builder = builder.expireAfterWrite(desc.ttl, TimeUnit.MINUTES); 043 if (desc.options.containsKey("concurrencyLevel")) { 044 builder = builder.concurrencyLevel(Integer.valueOf(desc.options.get("concurrencyLevel")).intValue()); 045 } 046 if (desc.options.containsKey("maxSize")) { 047 builder = builder.maximumSize(Integer.valueOf(desc.options.get("maxSize")).intValue()); 048 } 049 cache = builder.build(); 050 } 051 052 protected static final Log log = LogFactory.getLog(InMemoryCacheImpl.class); 053 054 protected final Cache<String, Serializable> cache; 055 056 /** 057 * Get the instance cache 058 * 059 * @return the Guava instance cache used in this nuxeo cache 060 * @since 6.0 061 */ 062 public Cache<String, Serializable> getGuavaCache() { 063 return cache; 064 } 065 066 @Override 067 public Serializable get(String key) { 068 if (key == null) { 069 return null; 070 } else { 071 return cache.getIfPresent(key); 072 } 073 } 074 075 @Override 076 public Set<String> keySet() { 077 return cache.asMap().keySet(); 078 } 079 080 @Override 081 public void invalidate(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 cache.invalidateAll(); 092 } 093 094 @Override 095 public void put(String key, Serializable value) { 096 if (key != null && value != null) { 097 cache.put(key, value); 098 } else { 099 log.warn(String.format("Can't put a null key nor a null value in the cache '%s'!", name)); 100 } 101 } 102 103 @Override 104 public boolean hasEntry(String key) { 105 return cache.asMap().containsKey(key); 106 } 107 108 @Override 109 public long getSize() { 110 return cache.size(); 111 } 112 113}