Upgraded Ehcache to version 3.4.0

This commit is contained in:
Oleg Kalnichevski 2017-10-19 15:49:57 +02:00
parent f215fdcd32
commit 26024a3b8e
6 changed files with 99 additions and 47 deletions

View File

@ -56,8 +56,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<groupId>org.ehcache.modules</groupId>
<artifactId>ehcache-api</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>

View File

@ -46,7 +46,7 @@ import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.message.BasicHeader;
/**
* Rebuilds an {@link HttpResponse} from a {@link net.sf.ehcache.CacheEntry}
* Rebuilds an {@link HttpResponse} from a {@link HttpCacheEntry}
*
* @since 4.1
*/

View File

@ -0,0 +1,53 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.impl.cache;
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
/**
* @since 5.0
*/
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class NoopCacheEntrySerializer implements HttpCacheEntrySerializer<HttpCacheStorageEntry> {
public static final NoopCacheEntrySerializer INSTANCE = new NoopCacheEntrySerializer();
@Override
public HttpCacheStorageEntry serialize(final HttpCacheStorageEntry cacheEntry) throws ResourceIOException {
return cacheEntry;
}
@Override
public HttpCacheStorageEntry deserialize(final HttpCacheStorageEntry cacheEntry) throws ResourceIOException {
return cacheEntry;
}
}

View File

@ -27,14 +27,14 @@
package org.apache.hc.client5.http.impl.cache.ehcache;
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.impl.cache.AbstractBinaryCacheStorage;
import org.apache.hc.client5.http.impl.cache.CacheConfig;
import org.apache.hc.client5.http.impl.cache.AbstractSerializingCacheStorage;
import org.apache.hc.client5.http.impl.cache.ByteArrayCacheEntrySerializer;
import org.apache.hc.client5.http.impl.cache.CacheConfig;
import org.apache.hc.client5.http.impl.cache.NoopCacheEntrySerializer;
import org.apache.hc.core5.util.Args;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.ehcache.Cache;
/**
* <p>This class is a storage backend for cache entries that uses the
@ -53,19 +53,30 @@ import net.sf.ehcache.Element;
* itself.</p>
* @since 4.1
*/
public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage<Element> {
private final Ehcache cache;
public class EhcacheHttpCacheStorage<T> extends AbstractSerializingCacheStorage<T, T> {
/**
* Constructs a storage backend using the provided Ehcache
* with default configuration options.
* @param cache where to store cached origin responses
* Creates cache that stores {@link HttpCacheStorageEntry}s without direct serialization.
*
* @since 5.0
*/
public EhcacheHttpCacheStorage(final Ehcache cache){
this(cache, CacheConfig.DEFAULT, ByteArrayCacheEntrySerializer.INSTANCE);
public static EhcacheHttpCacheStorage<HttpCacheStorageEntry> createObjectCache(
final Cache<String, HttpCacheStorageEntry> cache, final CacheConfig config) {
return new EhcacheHttpCacheStorage<>(cache, config, NoopCacheEntrySerializer.INSTANCE);
}
/**
* Creates cache that stores serialized {@link HttpCacheStorageEntry}s.
*
* @since 5.0
*/
public static EhcacheHttpCacheStorage<byte[]> createSerializedCache(
final Cache<String, byte[]> cache, final CacheConfig config) {
return new EhcacheHttpCacheStorage<>(cache, config, ByteArrayCacheEntrySerializer.INSTANCE);
}
private final Cache<String, T> cache;
/**
* Constructs a storage backend using the provided Ehcache
* with the given configuration options, but using an alternative
@ -77,9 +88,9 @@ public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage<Element>
* @param serializer alternative serialization mechanism
*/
public EhcacheHttpCacheStorage(
final Ehcache cache,
final Cache<String, T> cache,
final CacheConfig config,
final HttpCacheEntrySerializer<byte[]> serializer) {
final HttpCacheEntrySerializer<T> serializer) {
super((config != null ? config : CacheConfig.DEFAULT).getMaxUpdateRetries(), serializer);
this.cache = Args.notNull(cache, "Ehcache");
}
@ -90,41 +101,29 @@ public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage<Element>
}
@Override
protected void store(final String storageKey, final byte[] storageObject) throws ResourceIOException {
cache.put(new Element(storageKey, storageKey));
}
private byte[] castAsByteArray(final Object storageObject) throws ResourceIOException {
if (storageObject == null) {
return null;
}
if (storageObject instanceof byte[]) {
return (byte[]) storageObject;
} else {
throw new ResourceIOException("Unexpected cache content: " + storageObject.getClass());
}
protected void store(final String storageKey, final T storageObject) throws ResourceIOException {
cache.put(storageKey, storageObject);
}
@Override
protected byte[] restore(final String storageKey) throws ResourceIOException {
final Element element = cache.get(storageKey);
return element != null ? castAsByteArray(element.getObjectValue()) : null;
}
@Override
protected Element getForUpdateCAS(final String storageKey) throws ResourceIOException {
protected T restore(final String storageKey) throws ResourceIOException {
return cache.get(storageKey);
}
@Override
protected byte[] getStorageObject(final Element element) throws ResourceIOException {
return castAsByteArray(element.getObjectValue());
protected T getForUpdateCAS(final String storageKey) throws ResourceIOException {
return cache.get(storageKey);
}
@Override
protected boolean updateCAS(final String storageKey, final Element element, final byte[] storageObject) throws ResourceIOException {
final Element newElement = new Element(storageKey, storageObject);
return cache.replace(element, newElement);
protected T getStorageObject(final T element) throws ResourceIOException {
return element;
}
@Override
protected boolean updateCAS(
final String storageKey, final T oldStorageObject, final T storageObject) throws ResourceIOException {
return cache.replace(storageKey, oldStorageObject, storageObject);
}
@Override

View File

@ -136,7 +136,7 @@
org.osgi.service.cm,
org.apache.logging.log4j;version=${log4j.osgi.import.version},
org.apache.hc.core5.*;version=${httpcore.osgi.import.version},
net.sf.ehcache.*;resolution:=optional,
org.ehcache.*;resolution:=optional,
net.spy.memcached.*;resolution:=optional
</Import-Package>
<Bundle-Activator>org.apache.hc.client5.http.osgi.impl.HttpProxyConfigurationActivator</Bundle-Activator>

View File

@ -71,7 +71,7 @@
<httpcore.version>5.0-alpha4</httpcore.version>
<log4j.version>2.8.2</log4j.version>
<commons-codec.version>1.10</commons-codec.version>
<ehcache.version>2.6.11</ehcache.version>
<ehcache.version>3.4.0</ehcache.version>
<memcached.version>2.12.0</memcached.version>
<slf4j.version>1.7.13</slf4j.version>
<junit.version>4.12</junit.version>
@ -121,8 +121,8 @@
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<groupId>org.ehcache.modules</groupId>
<artifactId>ehcache-api</artifactId>
<version>${ehcache.version}</version>
</dependency>
<dependency>