org.vader.common.spring.cache.SerializableCacheProxy.java Source code

Java tutorial

Introduction

Here is the source code for org.vader.common.spring.cache.SerializableCacheProxy.java

Source

/*
 * Copyright 2014. Vadim Baranov
 *
 *    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 org.vader.common.spring.cache;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.core.serializer.DefaultDeserializer;
import org.springframework.core.serializer.DefaultSerializer;
import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author Vadim Baranov
 */
public class SerializableCacheProxy implements Cache {
    private static final Logger LOG = LoggerFactory.getLogger(SerializableCacheProxy.class);
    private static final int INIT_STEAM_SIZE = 512;

    private final Cache cache;
    private Serializer<Object> serializer = new DefaultSerializer();
    private Deserializer<Object> deserializer = new DefaultDeserializer();

    /**
     * Constructor.
     *
     * @param cache wrapped cache
     */
    public SerializableCacheProxy(Cache cache) {
        Validate.notNull(cache, "Wrapped cache must be not null");
        this.cache = cache;
    }

    public void setSerializer(Serializer<Object> serializer) {
        this.serializer = serializer;
    }

    public void setDeserializer(Deserializer<Object> deserializer) {
        this.deserializer = deserializer;
    }

    @Override
    public String getName() {
        return cache.getName();
    }

    @Override
    public Object getNativeCache() {
        return cache.getNativeCache();
    }

    @Override
    public ValueWrapper get(Object key) {
        final ValueWrapper result = cache.get(key);
        if (result == null) {
            return null;
        }
        if (result.get() == null) {
            return new SimpleValueWrapper(null);
        }
        try {
            return new SimpleValueWrapper(deserialize((byte[]) result.get()));
        } catch (IOException e) {
            LOG.warn("Unable to deserialize cached value", e);
            return null;
        }
    }

    @Override
    public <T> T get(Object key, Class<T> type) {
        final byte[] bytes = cache.get(key, byte[].class);
        try {
            return bytes == null ? null : type.cast(deserialize(bytes));
        } catch (IOException e) {
            LOG.warn("Unable to deserialize cached value", e);
            return null;
        }
    }

    @Override
    public void put(Object key, Object value) {
        try {
            cache.put(key, value == null ? null : serialize(value));
        } catch (IOException e) {
            LOG.warn("Unable to serialize value", e);
            cache.evict(key);
        }
    }

    @Override
    public void evict(Object key) {
        cache.evict(key);
    }

    @Override
    public void clear() {
        cache.clear();
    }

    private Object deserialize(byte[] bytes) throws IOException {
        try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
            return deserializer.deserialize(stream);
        }
    }

    private Object serialize(Object value) throws IOException {
        try (final ByteArrayOutputStream stream = new ByteArrayOutputStream(INIT_STEAM_SIZE)) {
            serializer.serialize(value, stream);
            return stream.toByteArray();
        }
    }
}