uk.gov.gchq.gaffer.cache.impl.JcsCache Maven / Gradle / Ivy
/*
* Copyright 2016-2024 Crown Copyright
*
* 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 uk.gov.gchq.gaffer.cache.impl;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.GroupCacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
import org.apache.commons.jcs.engine.control.CompositeCache;
import uk.gov.gchq.gaffer.cache.ICache;
import uk.gov.gchq.gaffer.cache.exception.CacheOperationException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
/**
* Implementation of the {@link ICache} interface, using a JCS {@link GroupCacheAccess}
* object as the cache data store.
*
* @param The object type that acts as the key for the cache
* @param The value that is stored in the cache
*/
public class JcsCache implements ICache {
private final GroupCacheAccess cache;
private final String groupName;
public JcsCache(final CompositeCache cache) throws CacheException {
this(cache.getCacheName(), cache.getCacheAttributes());
}
private JcsCache(final String cacheName, final ICompositeCacheAttributes attr) throws CacheException {
this.groupName = cacheName;
this.cache = JCS.getGroupCacheInstance(cacheName, attr);
}
@Override
public V get(final K key) {
return cache.getFromGroup(key, groupName);
}
@Override
public void put(final K key, final V value) throws CacheOperationException {
if (null == key) {
throw new CacheOperationException("Key must not be null");
}
try {
cache.putInGroup(key, groupName, value);
} catch (final CacheException e) {
throw new CacheOperationException("Failed to add item to cache", e);
}
}
@Override
public void remove(final K key) {
cache.removeFromGroup(key, groupName);
}
@Override
public Collection getAllValues() {
ArrayList values = new ArrayList<>();
getAllKeys().forEach(k -> values.add(get(k)));
return values;
}
@Override
public Set getAllKeys() {
return cache.getGroupKeys(groupName);
}
@Override
public int size() {
return getAllKeys().size();
}
@Override
public void clear() throws CacheOperationException {
try {
cache.clear();
} catch (final CacheException e) {
throw new CacheOperationException("Failed to clear cache", e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy