ehcache3 配置类
时间: 2024-05-16 21:12:43 浏览: 205
以下是 Ehcache3 的基本配置类示例:
```java
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.spi.service.StatisticsService;
import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration;
import org.ehcache.impl.config.persistence.DefaultPersistenceConfiguration;
import org.ehcache.impl.config.persistence.FilesystemPersistenceConfiguration;
import org.ehcache.impl.config.persistence.PersistenceConfiguration;
import org.ehcache.impl.config.serializer.DefaultSerializationProviderConfiguration;
import org.ehcache.impl.config.serializer.DefaultSerializerConfiguration;
import org.ehcache.spi.serialization.Serializer;
import org.ehcache.spi.serialization.SerializerException;
import org.ehcache.spi.serialization.SerializerProvider;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
public class EhCacheConfig {
public static final String CACHE_NAME = "myCache";
public static final int CACHE_SIZE = 1000;
public static ConfigurationBuilder getConfigurationBuilder() {
ConfigurationBuilder configurationBuilder = ConfigurationBuilder.newConfigurationBuilder()
.withDefaultCache(CACHE_NAME)
.withCache(CACHE_NAME,
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(CACHE_SIZE, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.GB)
.disk(10, MemoryUnit.GB, true)
)
);
// 持久化配置
PersistenceConfiguration persistenceConfiguration = new FilesystemPersistenceConfiguration(
new File("cache-data")
);
configurationBuilder.addService(persistenceConfiguration);
// 设置序列化器
configurationBuilder.addService(DefaultSerializationProviderConfiguration.class, new DefaultSerializationProviderConfiguration()
.addSerializerFor(Long.class, new LongSerializer())
.addSerializerFor(String.class, new StringSerializer())
);
// 设置统计服务
configurationBuilder.addService(StatisticsService.class);
return configurationBuilder;
}
// 自定义 Long 序列化器
public static class LongSerializer implements Serializer<Long> {
@Override
public ByteBuffer serialize(Long object) throws SerializerException {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(object);
buffer.flip();
return buffer;
}
@Override
public Long read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
return binary.getLong();
}
@Override
public boolean equals(Long object, ByteBuffer binary) throws ClassNotFoundException, SerializerException {
return object.equals(read(binary));
}
}
// 自定义 String 序列化器
public static class StringSerializer implements Serializer<String> {
@Override
public ByteBuffer serialize(String object) throws SerializerException {
return ByteBuffer.wrap(object.getBytes());
}
@Override
public String read(ByteBuffer binary) throws ClassNotFoundException, SerializerException {
byte[] bytes = new byte[binary.remaining()];
binary.get(bytes);
return new String(bytes);
}
@Override
public boolean equals(String object, ByteBuffer binary) throws ClassNotFoundException, SerializerException {
return object.equals(read(binary));
}
}
}
```
在此示例中,我们定义了一个名为 `myCache` 的缓存,并设置了最大堆大小为 `CACHE_SIZE`,最大内存大小为 1GB,最大磁盘大小为 10GB。此外,我们还设置了持久化配置,序列化器和统计服务。在此示例中,我们自定义了 `Long` 和 `String` 的序列化器。这些序列化器将被用于将数据编组和解组,以便将其存储到磁盘或从磁盘中检索。
阅读全文